Refactor return type of authenticate user to be an object

This commit is contained in:
Johannes Klein
2026-04-22 12:23:42 +02:00
parent dc581625c1
commit e29df34bee
4 changed files with 35 additions and 25 deletions

View File

@@ -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<AuthenticateUserResult> {
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<boolean> => {
): Promise<AuthenticateUserResult> => {
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<AuthenticateUserResult> {
const apiClient = createAPI( { Accept: "application/json" } );
const formData = {
client_id: Config.OAUTH_CLIENT_ID,

View File

@@ -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<boolean> ) => {
const logIn = useCallback( async ( logInCallback: () => Promise<AuthenticateUserResult> ) => {
setLoading( true );
const success = await logInCallback( );
const result = await logInCallback( );
if ( !success ) {
if ( !result.success ) {
setError( t( "Failed-to-log-in" ) );
setLoading( false );
return;

View File

@@ -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;
}

View File

@@ -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<AuthenticateUserResult> {
// 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<AuthenticateUserResult> {
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 {