public LoginResponse AuthenticateAndLogIn([FromBody] LogInRequest request) { var response = new LoginResponse(); try { GoogleTokenInformation tokenInformation = _googleTokenAuthentication.VerifyToken(request.Token); if (tokenInformation.Valid == false) { response.AddError($"Server failed to verify Google credentials. Please try again.", request); return(response); } UserSession sessionInformation = _userService.LogInUser(tokenInformation); response.NewUser = sessionInformation.NewUser; response.SessionId = sessionInformation.SessionId; response.User = _userService.GetPersonFromSession(sessionInformation.SessionId.ToString()); } catch (ErrorCodeException exception) { response.AddError($"An unexpected exception occured: {exception}", request, exception.Code); } catch (Exception exception) { response.AddError($"An unexpected exception occured: {exception}", request); } return(response); }
public ActiveUser AddPersonUsingGoogleInformation(GoogleTokenInformation tokenInformation) { _connection.Open(); try { var command = new NpgsqlCommand($"INSERT INTO public.\"Person\" (\"FirstName\", \"LastName\", \"Image\", \"Active\", \"GoogleClientId\") " + $"VALUES ('{tokenInformation.given_name}', '{tokenInformation.family_name}', '{tokenInformation.picture}', true, '{tokenInformation.sub}') " + "RETURNING \"Id\"", _connection); Int64 userId = -1; var reader = command.ExecuteReader(); while (reader.Read()) { userId = Convert.ToInt64(reader[0]); } reader.Close(); return(new ActiveUser { PersonId = Convert.ToInt32(userId) }); } catch (System.Exception exception) { throw new System.Exception($"An Error occured while adding the user after first sign in", exception); } finally { _connection.Close(); } }
public UserSession LogInUser(GoogleTokenInformation tokenInformation) { bool newUser = false; ActiveUser user = _peopleRepository.GetPersonFromGoogleClientId(tokenInformation.sub); if (user == null) { user = _peopleRepository.AddPersonUsingGoogleInformation(tokenInformation); newUser = true; } Guid sessionId = _userCache.GenerateUserSession(user); return(new UserSession { SessionId = sessionId, NewUser = newUser }); }