private TokenResponse GetTokenResponse(ApplicationCore.Entities.Admin user, bool isPersistent) { var expiresAtUtc = DateTime.UtcNow.AddDays(7); var token = _tokenBuilder.BuildToken(user, expiresAtUtc, isPersistent); _logger.Info($"{user.Username} requested new token."); return(new TokenResponse { AccessToken = token, ExpiresAtUtc = expiresAtUtc }); }
public async Task <IActionResult> ExternalLogin([FromBody] ExternalLoginBindingModel model) { var user = await _userRepository.FindAsync(x => x.Email == model.Email); if (user == null) { user = new ApplicationCore.Entities.Admin { Email = model.Email, Username = model.Email }; user = await _userRepository.AddAsync(user); var externalLogin = _mapper.Map <ExternalLogin>(model); await _externalLoginRepository.AddAsync(externalLogin); } else { var externalLogin = await _externalLoginRepository.FindAsync(x => x.UserId == user.Id && x.LoginProvider == model.LoginProvider && x.ProviderKey == model.ProviderKey); if (externalLogin == null) { externalLogin = _mapper.Map <ExternalLogin>(model); externalLogin.UserId = user.Id; await _externalLoginRepository.AddAsync(externalLogin); } } if (!user.Active || !user.Verified) { return(BadRequest(new BadRequestResponseModel(ErrorMessages.AuthenticatinError, "You are not active user."))); } return(Ok(GetTokenResponse(user, false))); }