public async Task <IActionResult> GoogleSingin(string code) { try { var tokenResponse = await GetGoogleTokenAsync(code); if (tokenResponse == null) { return(StatusCode(StatusCodes.Status400BadRequest)); } var userInfo = await GetGoogleUserInfoAsync(tokenResponse.access_token); var googleUser = await databaseService.GetUserByEmailAsync(userInfo.Email); if (googleUser == null) { googleUser = await databaseService.SaveUserAsync(new User(userInfo)); } var response = new AuthenticateResponse(googleUser, jwtTokenHelper.GenerateJwtToken(googleUser)); var baseUrl = $"{httpContextAccessor.HttpContext.Request.Scheme}://{httpContextAccessor.HttpContext.Request.Host.Value}"; return(Redirect($"{baseUrl}/social?accessToken={response.AccessToken}")); } catch (Exception ex) { logger.LogError(ex, ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task <IActionResult> Authenticate(AuthenticateRequest model) { try { if (String.IsNullOrWhiteSpace(model.Email) || String.IsNullOrWhiteSpace(model.Password)) { return(BadRequest(new { message = "Incomplete user authentication information" })); } var user = await databaseService.GetUserByEmailAsync(model.Email); if (user == null) { return(BadRequest(new { message = "User doesn't exist" })); } if (user.Type != AccountType.Local) { return(BadRequest(new { message = $"The email have been registered under your {user.Type} account" })); } if (!Utilities.ValidatePassword(model.Password, user.Password)) { return(BadRequest(new { message = "Incorrect password" })); } var response = new AuthenticateResponse(user, jwtTokenHelper.GenerateJwtToken(user)); return(Ok(response)); } catch (Exception ex) { logger.LogError(ex, ex.Message); return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public async Task <IActionResult> Authenticate([FromBody] UserLoginRequest credential) { var pepper = _configuration.GetValue <string>("Security:Pepper"); var jwtSecret = _configuration.GetValue <string>("Security:JwtSecret"); var jwtExpireAfter = _configuration.GetValue <int>("Security:JwtExpireAfter"); var result = await _userService.Authenticate(credential.Password, credential.Username, pepper); var response = _mapper.Map <UserLoginResponse>(result); if (response.Result == AuthenticationResult.Success) { response.Token = _tokenHelper.GenerateJwtToken(response.UserDetail.Id, jwtSecret, jwtExpireAfter); return(Ok(response)); } return(Ok(response)); }
public async Task <IActionResult> Login(UserForLoginDto userForLoginDto) { var user = await _userManager.FindByEmailAsync(userForLoginDto.Email); if (user == null) { return(Unauthorized()); } var result = await _signInManager.CheckPasswordSignInAsync(user, userForLoginDto.Password, false); if (result.Succeeded) { return(Ok(new { token = await _jwtTokenHelper.GenerateJwtToken(user, _userManager, _config) })); } return(Unauthorized()); }