feat(auth): Added authentication
This commit is contained in:
parent
d85d4334f8
commit
5e100c75ed
39 changed files with 704 additions and 86 deletions
67
backend/src/Services/AuthService.cs
Normal file
67
backend/src/Services/AuthService.cs
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
using JellyGlass.Exceptions;
|
||||
using JellyGlass.Models;
|
||||
using JellyGlass.Repositories;
|
||||
|
||||
namespace JellyGlass.Services;
|
||||
|
||||
public class AuthService : IAuthService
|
||||
{
|
||||
private ILoginRepository _loginRepo;
|
||||
private ISessionRepository _sessionRepo;
|
||||
|
||||
public AuthService(ILoginRepository loginRepo, ISessionRepository sessionRepo)
|
||||
{
|
||||
_loginRepo = loginRepo;
|
||||
_sessionRepo = sessionRepo;
|
||||
}
|
||||
|
||||
public async Task<UserSessionDTO> AuthenticateUser(string username, string password)
|
||||
{
|
||||
UserLogin login;
|
||||
|
||||
try
|
||||
{
|
||||
login = await _loginRepo.GetUserLogin(username);
|
||||
}
|
||||
catch (AuthNotFoundException)
|
||||
{
|
||||
throw new LoginFailedException();
|
||||
}
|
||||
|
||||
|
||||
if (BCrypt.Net.BCrypt.Verify(password, login.HashedPassword))
|
||||
{
|
||||
var session = await _sessionRepo.CreateUserSession(login);
|
||||
return new UserSessionDTO(session);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new LoginFailedException();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> IsAuthenticated(string? sessionToken)
|
||||
{
|
||||
if (sessionToken == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var session = await _sessionRepo.GetUserSession(sessionToken);
|
||||
|
||||
if (session == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else if (session.ExpiresOn < DateTime.Now)
|
||||
{
|
||||
await _sessionRepo.DeleteSession(sessionToken);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
await _sessionRepo.RefreshSessionExpiry(sessionToken);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
backend/src/Services/IAuthService.cs
Normal file
9
backend/src/Services/IAuthService.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
using JellyGlass.Models;
|
||||
|
||||
namespace JellyGlass.Services;
|
||||
|
||||
public interface IAuthService
|
||||
{
|
||||
public Task<UserSessionDTO> AuthenticateUser(string username, string password);
|
||||
public Task<bool> IsAuthenticated(string? sessionToken);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue