From e29df34bee8ee86d38e38b5d91ae04598bb17a97 Mon Sep 17 00:00:00 2001 From: Johannes Klein Date: Wed, 22 Apr 2026 12:23:42 +0200 Subject: [PATCH] Refactor return type of authenticate user to be an object --- .../LoginSignUp/AuthenticationService.ts | 19 ++++++++---- src/components/LoginSignUp/LoginForm.tsx | 7 +++-- .../LoginSignUp/SignUpConfirmationForm.tsx | 4 +-- .../LoginSignUp/loginFormHelpers.ts | 30 ++++++++++--------- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/src/components/LoginSignUp/AuthenticationService.ts b/src/components/LoginSignUp/AuthenticationService.ts index bd02f679f..aad7c31e2 100644 --- a/src/components/LoginSignUp/AuthenticationService.ts +++ b/src/components/LoginSignUp/AuthenticationService.ts @@ -542,14 +542,19 @@ async function verifyCredentials( return afterVerifyCredentials( tokenResponse, apiClient ); } -async function afterAuthenticateUser( userDetails: UserDetails | null, realm: Realm ) { +export interface AuthenticateUserResult { success: boolean } + +async function afterAuthenticateUser( + userDetails: UserDetails | null, + realm: Realm, +): Promise { if ( !userDetails ) { - return false; + return { success: false }; } const { userId, username: remoteUsername, accessToken } = userDetails; if ( !userId ) { - return false; + return { success: false }; } // Save authentication details to secure storage @@ -586,7 +591,9 @@ async function afterAuthenticateUser( userDetails: UserDetails | null, realm: Re realm.create( "User", localUser, UpdateMode.Modified ); }, "saving current user in AuthenticationService" ); clearAuthCache( ); - return true; + return { + success: true, + }; } /** @@ -601,7 +608,7 @@ const authenticateUser = async ( username: string, password: string, realm: Realm, -): Promise => { +): Promise => { const userDetails = await verifyCredentials( username, password ); return afterAuthenticateUser( userDetails, realm ); @@ -611,7 +618,7 @@ async function authenticateUserByAssertion( assertionType: "apple" | "google", assertion: string, realm: Realm, -) { +): Promise { const apiClient = createAPI( { Accept: "application/json" } ); const formData = { client_id: Config.OAUTH_CLIENT_ID, diff --git a/src/components/LoginSignUp/LoginForm.tsx b/src/components/LoginSignUp/LoginForm.tsx index 19f7a1da7..7bca79d23 100644 --- a/src/components/LoginSignUp/LoginForm.tsx +++ b/src/components/LoginSignUp/LoginForm.tsx @@ -1,5 +1,6 @@ import { useNavigation, useRoute } from "@react-navigation/native"; import classnames from "classnames"; +import type { AuthenticateUserResult } from "components/LoginSignUp/AuthenticationService"; import { authenticateUser, signOut } from "components/LoginSignUp/AuthenticationService"; import { Body1, Body2, Button, CloseButton, Heading4, INatIcon, INatIconButton, List2, @@ -99,11 +100,11 @@ const LoginForm = ( { return unsubscrubeTransition; }, [navigation] ); - const logIn = React.useCallback( async ( logInCallback: () => Promise ) => { + const logIn = useCallback( async ( logInCallback: () => Promise ) => { setLoading( true ); - const success = await logInCallback( ); + const result = await logInCallback( ); - if ( !success ) { + if ( !result.success ) { setError( t( "Failed-to-log-in" ) ); setLoading( false ); return; diff --git a/src/components/LoginSignUp/SignUpConfirmationForm.tsx b/src/components/LoginSignUp/SignUpConfirmationForm.tsx index d1b44c9ff..e12361a83 100644 --- a/src/components/LoginSignUp/SignUpConfirmationForm.tsx +++ b/src/components/LoginSignUp/SignUpConfirmationForm.tsx @@ -98,9 +98,9 @@ const SignUpConfirmationForm = ( ) => { setLoading( false ); return; } - const success = await authenticateUser( processedUser.login, processedUser.password, realm ); + const result = await authenticateUser( processedUser.login, processedUser.password, realm ); setLoading( false ); - if ( !success ) { + if ( !result.success ) { navigation.navigate( "Login" ); return; } diff --git a/src/components/LoginSignUp/loginFormHelpers.ts b/src/components/LoginSignUp/loginFormHelpers.ts index c6e485e59..7f0569e05 100644 --- a/src/components/LoginSignUp/loginFormHelpers.ts +++ b/src/components/LoginSignUp/loginFormHelpers.ts @@ -12,6 +12,7 @@ import Config from "react-native-config"; import type Realm from "realm"; import { log } from "sharedHelpers/logger"; +import type { AuthenticateUserResult } from "./AuthenticationService"; import { authenticateUserByAssertion, } from "./AuthenticationService"; @@ -29,7 +30,7 @@ function showSignInWithAppleFailed() { ); } -async function signInWithApple( realm: Realm ) { +async function signInWithApple( realm: Realm ): Promise { // Request sign in w/ apple. This should pop up some system UI for signing // in let appleAuthRequestResponse; @@ -42,11 +43,11 @@ async function signInWithApple( realm: Realm ) { } catch ( appleAuthRequestError ) { if ( ( appleAuthRequestError as AppleAuthError ).code === appleAuth.Error.CANCELED ) { // The user canceled sign in, no need to log - return false; + return { success: false }; } logger.error( "Apple auth request failed", appleAuthRequestError ); showSignInWithAppleFailed(); - return false; + return { success: false }; } // Check if auth was successful @@ -74,19 +75,20 @@ async function signInWithApple( realm: Realm ) { } ), } ); try { + // return await authenticateUserByAssertion( "apple", assertion, realm ); await authenticateUserByAssertion( "apple", assertion, realm ); } catch ( authenticateUserByAssertionError ) { logger.error( "Assertion with Apple token failed", authenticateUserByAssertionError ); showSignInWithAppleFailed(); - return false; + return { success: false }; } - return true; + return { success: true }; } // We only get here if the user does not grant access... I think, so no need // to log an error logger.info( "Apple auth failed, credentialState: ", credentialState ); showSignInWithAppleFailed(); - return false; + return { success: false }; } GoogleSignin.configure( { @@ -124,33 +126,33 @@ function showSignInWithGoogleFailed() { ); } -async function signInWithGoogle( realm: Realm ) { +async function signInWithGoogle( realm: Realm ): Promise { const hasPlayServices = await confirmGooglePlayServices( ); - if ( !hasPlayServices ) return false; + if ( !hasPlayServices ) return { success: false }; let signInResp; try { signInResp = await GoogleSignin.signIn(); } catch ( signInError ) { logger.error( "Failed to sign in with Google", signInError ); - return false; + return { success: false }; } - if ( signInResp.type === "cancelled" ) return false; + if ( signInResp.type === "cancelled" ) return { success: false }; let tokens; try { tokens = await GoogleSignin.getTokens(); } catch ( getTokensError ) { logger.error( "Failed to get tokens from Google", getTokensError ); - return false; + return { success: false }; } - if ( !tokens?.accessToken ) return false; + if ( !tokens?.accessToken ) return { success: false }; try { await authenticateUserByAssertion( "google", tokens.accessToken, realm ); } catch ( authenticateUserByAssertionError ) { logger.error( "Assertion with Google token failed", authenticateUserByAssertionError ); showSignInWithGoogleFailed(); - return false; + return { success: false }; } - return true; + return { success: true }; } export {