public async Task <IronToken> Login(UserGoogle user) { var googlePublicKey = _configuration["Google:PublicKey"]; var userValidPayload = await GoogleJsonWebSignature.ValidateAsync(user.TokenGoogle, new GoogleJsonWebSignature.ValidationSettings() { Audience = new string[] { googlePublicKey } }); var userBd = await _unitOfWork.Users.GetByEmail(userValidPayload.Email); if (userBd != null) { if (!userBd.GoogleAuth) { throw new ValidationException($"User { userValidPayload.Email } was created with default authentication. Use email and Password"); } } else { var profilePicture = await _fileService.DownloadAndSaveFromUrl(userValidPayload.Picture); userBd = new User(userValidPayload.Name, userValidPayload.Email, AuthConstants.UserGoogle_FakePassword, eRole.Employee, true); if (profilePicture.Success) { userBd.SetProfilePicture(profilePicture.FileName); } await Register(userBd); } return(_tokenService.Generate(userBd)); }
public async Task <ActionResult <IronToken> > Google(LoginGoogleDto user) { try { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var userGoogle = new UserGoogle(user.TokenGoogle); IronToken token = await _service.Login(userGoogle); return(Created("", token)); } catch (InvalidJwtException) { ModelState.AddModelError("", "Invalid credentials"); } catch (Exception ex) { //TODO: Change for return 500 code not only 400 HandleException(ex); } return(BadRequest(ModelState)); }