diff --git a/src/services/auth.ts b/src/services/auth.ts index effbc77..bf8ea89 100644 --- a/src/services/auth.ts +++ b/src/services/auth.ts @@ -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, @@ -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(AUTH_IDENTITY_KEYCHAIN_PATH); } @@ -48,14 +54,21 @@ export async function signin(authenticated: AuthenticatedAppleResponse) { if (await KeychainStorage.retrieve(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() { @@ -95,3 +108,10 @@ async function openAppleAuthenticationDialog(): Promise(`${DATASTREAM_BASE_URL}/auth/token`, { + method: 'GET', + headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${appleToken}` }, + }); +} diff --git a/src/services/emr.ts b/src/services/emr.ts index ed08007..88eb319 100644 --- a/src/services/emr.ts +++ b/src/services/emr.ts @@ -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; } }