Created
July 25, 2023 06:18
-
-
Save Sathasivamthirumoorthi/e67ee544da47b9dce7ee2a50ea4573a1 to your computer and use it in GitHub Desktop.
Authrepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task<ServiceResponse<string>> Login(string username, string password) | |
{ | |
var response = new ServiceResponse<string>(); | |
var user = await _dbContext.Users.FirstOrDefaultAsync( | |
u => u.UserName!.ToLower() == username.ToLower() | |
); | |
//If user is null response message as User not found | |
if (user == null) | |
{ | |
response.Success = false; | |
response.Message = "User Not Found"; | |
} | |
//If user is not null verify password and if returns false set response as Incorrect Password | |
else if (!VerifyPasswordHash(password, user.PasswordHash!, user.PasswordSalt!)) | |
{ | |
response.Success = false; | |
response.Message = "Incorrect Password"; | |
} | |
else | |
{ | |
//If the manager is appointed generate JWT Token if not return false | |
if (user.Role == UserRole.Manager) | |
{ | |
var manager = await _dbContext.Managers.FindAsync(user.UserID); | |
if (manager.IsAppointed == false) | |
{ | |
response.Success = false; | |
response.Message = "User Not Found"; | |
return response; | |
} | |
} | |
response.Data = _jwtUtils.GenerateJwtToken(user); | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment