public async Task <LoggedUserResponse> FacebookLoginAsync(FacebookLoginRequest facebookLoginRequest) { if (string.IsNullOrEmpty(facebookLoginRequest.FacebookToken)) { throw new Exception("Token is null or empty"); } var facebookUser = await _facebookService.GetUserFromFacebookAsync(facebookLoginRequest.FacebookToken); var applicationUser = await _appDbContext.Users.SingleOrDefaultAsync(user => user.Email == facebookUser.Email); string token; if (applicationUser == null) { var user = await CreateFacebookUser(facebookUser); await _appDbContext.SaveChangesAsync(CancellationToken.None); token = await Task.Run(() => JwtAuthManager.GenerateToken(user)); return(LoggedUser(user, token)); } token = await Task.Run(() => JwtAuthManager.GenerateToken(applicationUser)); return(LoggedUser(applicationUser, token)); }
public async Task <LoggedUserResponse> Registration(RegisterRequest registerRequest) { var base64Encode = PasswordHelper.EncodeAndHash(registerRequest.Password, out var passwordHash); var user = await CreateUser(registerRequest, passwordHash, base64Encode); await _appDbContext.SaveChangesAsync(CancellationToken.None); var token = JwtAuthManager.GenerateToken(user); return(LoggedUser(user, token)); }
public async Task <LoggedUserResponse> Authenticate(LoginRequest loginRequest) { var user = await _appDbContext.Users.FirstOrDefaultAsync(u => u.Email == loginRequest.Email); if (user == null) { return(null); } var passwordHash = HashingHelper.HashUsingPbkdf2(loginRequest.Password, user.PasswordSalt); if (user.Password != passwordHash) { return(null); } var token = JwtAuthManager.GenerateToken(user); return(LoggedUser(user, token)); }