From a640703f2da8815dc26ad1600a6f206be1624379 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Wed, 1 Jun 2022 22:10:32 +0200 Subject: feat: Initial after clean slate --- server/src/Services/UserService.cs | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 server/src/Services/UserService.cs (limited to 'server/src/Services/UserService.cs') diff --git a/server/src/Services/UserService.cs b/server/src/Services/UserService.cs new file mode 100644 index 0000000..9b531de --- /dev/null +++ b/server/src/Services/UserService.cs @@ -0,0 +1,50 @@ +namespace IOL.GreatOffice.Api.Services; + +public class UserService +{ + private readonly ForgotPasswordService _forgotPasswordService; + + /// + /// Provides methods to perform common operations on user data. + /// + /// + public UserService(ForgotPasswordService forgotPasswordService) { + _forgotPasswordService = forgotPasswordService; + } + + /// + /// Log in a user. + /// + /// + /// + /// + public async Task LogInUser(HttpContext httpContext, User user, bool persist = false) { + var claims = new List { + new(AppClaims.USER_ID, user.Id.ToString()), + new(AppClaims.NAME, user.Username), + }; + + var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme); + var principal = new ClaimsPrincipal(identity); + var authenticationProperties = new AuthenticationProperties { + AllowRefresh = true, + IssuedUtc = DateTimeOffset.UtcNow, + }; + + if (persist) { + authenticationProperties.ExpiresUtc = DateTimeOffset.UtcNow.AddMonths(6); + authenticationProperties.IsPersistent = true; + } + + await httpContext.SignInAsync(principal, authenticationProperties); + await _forgotPasswordService.DeleteRequestsForUserAsync(user.Id); + } + + /// + /// Log out a user. + /// + /// + public async Task LogOutUser(HttpContext httpContext) { + await httpContext.SignOutAsync(); + } +} -- cgit v1.3