1
|
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
|
public LoginResponse? Index(string username, string password)
|
19
|
{
|
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
|
var loginResult = new LoginResponse(true, token, expiration, user.Role);
|
32
|
|
33
|
return loginResult;
|
34
|
}
|
35
|
}
|