Projekt

Obecné

Profil

Stáhnout (898 Bajtů) Statistiky
| Větev: | Tag: | Revize:
1 c349bab3 Lukáš Vlček
using Core.Authentication;
2
using Core.Entities;
3
using Models.Authentication;
4
5
namespace Core.Services;
6
7
public class AuthService : IAuthService
8
{
9
    private readonly IUserService userService;
10
    private readonly IJwtUtils jwtUtils;
11
12
    public AuthService(IUserService userService, IJwtUtils jwtUtils)
13
    {
14
        this.userService = userService;
15
        this.jwtUtils = jwtUtils;
16
    }
17
18 7a32656e Jaroslav Hrubý
    public LoginResponse? Login(string username, string password)
19 c349bab3 Lukáš Vlček
    {
20
        var user = userService.CheckUsernamePassword(username, password);
21
22
        if (user == null)
23
        {
24
            // user with given credentials not found
25
            return null;
26
        }
27
28
        var expiration = DateTime.Now.AddHours(12);
29
        var token = jwtUtils.GenerateJwtToken(user, expiration);
30
31 f0fde45c Vojtěch Bartička
        var loginResult = new LoginResponse(true, token, expiration, user.Role);
32 c349bab3 Lukáš Vlček
33
        return loginResult;
34
    }
35
}