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();
}
}