Skip to content

Commit

Permalink
Use backend provided auth JWT instead of Apple
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryashutov committed Nov 27, 2023
1 parent 282decd commit 605cbd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
36 changes: 28 additions & 8 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import { isSuccess } from 'fhir-react/src/libs/remoteData';
import { Navigation } from 'react-native-navigation';
import { mainRoot } from 'screens/navigation';
import { loginRoot } from 'screens/Login/navigation';
import { service } from 'fhir-react/src/services/fetch';
import { DATASTREAM_BASE_URL } from 'config';

const AUTH_IDENTITY_KEYCHAIN_PATH = 'apple_identity';
const AUTH_IDENTITY_KEYCHAIN_PATH = 'datasequence_identity';

export enum AuthStatus {
Authenticated = 1,
Expand All @@ -33,6 +35,10 @@ export interface NotAuthenticated extends AuthState {
readonly status: AuthStatus.NotAuthenticated;
}

export interface AuthTokenResponse {
access_token: string;
}

export async function getUserIdentity() {
return KeychainStorage.retrieve<Authenticated>(AUTH_IDENTITY_KEYCHAIN_PATH);
}
Expand All @@ -48,14 +54,21 @@ export async function signin(authenticated: AuthenticatedAppleResponse) {
if (await KeychainStorage.retrieve<Authenticated>(AUTH_IDENTITY_KEYCHAIN_PATH)) {
await KeychainStorage.remove(AUTH_IDENTITY_KEYCHAIN_PATH);
}
await KeychainStorage.store(AUTH_IDENTITY_KEYCHAIN_PATH, identity);
const authTokenResponse = await getAuthToken(identity.jwt);
if (isSuccess(authTokenResponse)) {
const authToken = authTokenResponse.data.access_token;
await KeychainStorage.store(AUTH_IDENTITY_KEYCHAIN_PATH, { status: AuthStatus.Authenticated, jwt: authToken });

return await signinEMRPatient(authenticated.jwt, {
name: {
given: authenticated.username?.givenName ?? undefined,
family: authenticated.username?.familyName ?? undefined,
},
});
return await signinEMRPatient(authToken, {
name: {
given: authenticated.username?.givenName ?? undefined,
family: authenticated.username?.familyName ?? undefined,
},
});
} else {
console.error('authTokenResponse error', authTokenResponse.error);
return authTokenResponse;
}
}

export async function signinWithApple() {
Expand Down Expand Up @@ -95,3 +108,10 @@ async function openAppleAuthenticationDialog(): Promise<AuthenticatedAppleRespon
throw error;
}
}

async function getAuthToken(appleToken: string) {
return await service<AuthTokenResponse>(`${DATASTREAM_BASE_URL}/auth/token`, {
method: 'GET',
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${appleToken}` },
});
}
1 change: 1 addition & 0 deletions src/services/emr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function signinEMRPatient(token: string, user: { name: { given?: st
if (isSuccess(response)) {
return await fetchEMRPatient(token);
} else {
console.error('signinEMRPatient response errpr', response.error);
return response;
}
}
Expand Down

0 comments on commit 605cbd3

Please sign in to comment.