Skip to content

Commit

Permalink
feat(#854): ios changes in backup and restore
Browse files Browse the repository at this point in the history
Signed-off-by: Abhishek Paul <abhishek.paul@thoughtworks.com>
  • Loading branch information
Abhishek Paul committed Apr 7, 2024
1 parent 65e3365 commit d37b1e2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
export interface Typegen0 {
'@@xstate/typegen': true;
internalEvents: {
'done.invoke.vc.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]': {
type: 'done.invoke.vc.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
'done.invoke.vcMeta.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]': {
type: 'done.invoke.vcMeta.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
data: unknown;
__tip: 'See the XState TS docs to learn how to strongly type this.';
};
'xstate.init': {type: 'xstate.init'};
};
invokeSrcNameMap: {
isUserSignedAlready: 'done.invoke.vc.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
isUserSignedAlready: 'done.invoke.vcMeta.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
};
missingImplementations: {
actions: never;
Expand All @@ -30,7 +30,7 @@ export interface Typegen0 {
| 'VERIFY_VC_FAILED'
| 'xstate.init';
loadReceivedVcs: 'REFRESH_RECEIVED_VCS' | 'STORE_RESPONSE';
logTamperedVCsremoved: 'done.invoke.vc.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
logTamperedVCsremoved: 'done.invoke.vcMeta.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
prependToMyVcs: 'VC_ADDED';
removeDownloadFailedVcsFromStorage: 'DELETE_VC';
removeDownloadingFailedVcsFromMyVcs: 'STORE_RESPONSE';
Expand All @@ -43,7 +43,7 @@ export interface Typegen0 {
resetInProgressVcsDownloaded: 'RESET_IN_PROGRESS_VCS_DOWNLOADED';
resetVerificationErrorMessage: 'RESET_VERIFY_ERROR';
resetWalletBindingSuccess: 'RESET_WALLET_BINDING_SUCCESS';
sendBackupEvent: 'done.invoke.vc.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
sendBackupEvent: 'done.invoke.vcMeta.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
setDownloadedVc: 'VC_DOWNLOADED';
setDownloadingFailedVcs: 'DOWNLOAD_LIMIT_EXPIRED';
setMyVcs: 'STORE_RESPONSE';
Expand All @@ -56,7 +56,7 @@ export interface Typegen0 {
};
eventsCausingDelays: {};
eventsCausingGuards: {
isSignedIn: 'done.invoke.vc.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
isSignedIn: 'done.invoke.vcMeta.tamperedVCs.triggerAutoBackupForTamperedVcDeletion:invocation[0]';
};
eventsCausingServices: {
isUserSignedAlready: 'REMOVE_TAMPERED_VCS';
Expand Down
46 changes: 18 additions & 28 deletions machines/backupAndRestore/backupAndRestoreSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const model = createModel(
errorMessage: '' as string,
serviceRefs: {} as AppServices,
shouldTriggerAutoBackup: false as boolean,
isCloudSignedIn:false as boolean
isCloudSignedIn: false as boolean,
},
{
events: {
Expand Down Expand Up @@ -160,17 +160,17 @@ export const backupAndRestoreSetupMachine = model.createMachine(
{
cond: 'isSignedIn',
actions: ['setProfileInfo', 'unsetIsLoading'],
target: 'backupAndRestore', //backupAndRestore abhishek
target: 'backupAndRestore', //backupAndRestore abhishek
},
{
cond: 'isNetworkError',
actions: 'sendBackupAndRestoreSetupErrorEvent',
target: '.noInternet',
},
{
cond:'isAuthorised',
actions:['unsetIsLoading'],
target:'.error'
cond: 'isAuthorised',
actions: ['unsetIsLoading'],
target: '.error',
},
// {
// cond: 'isIOSAndNotSignedIn',
Expand All @@ -190,8 +190,7 @@ export const backupAndRestoreSetupMachine = model.createMachine(
initial: 'idle',
states: {
idle: {},



error: {
entry: 'unsetIsLoading',
on: {
Expand Down Expand Up @@ -370,16 +369,14 @@ export const backupAndRestoreSetupMachine = model.createMachine(
isInternetConnected: (_, event) =>
!!(event.data as NetInfoState).isConnected,
isNetworkError: (_, event) => event.data.error === NETWORK_REQUEST_FAILED,
isSignedIn: (_context, event) =>
{

return (event.data as isSignedInResult).isSignedIn
isSignedIn: (_context, event) => {
return (event.data as isSignedInResult).isSignedIn;
},



isIOSAndSignInFailed: (_context, event) => {
console.warn("pp"+(event.data as SignInResult).status)
const isSignInFailed = !((event.data as SignInResult).status === Cloud.status.SUCCESS); // Adjust according to your data structure
const isSignInFailed = !(
(event.data as SignInResult).status === Cloud.status.SUCCESS
); // Adjust according to your data structure
return isIOS() && isSignInFailed;
},
isConfirmationAlreadyShown: (_context, event) => {
Expand All @@ -389,15 +386,12 @@ export const backupAndRestoreSetupMachine = model.createMachine(
] || false
);
},
isSignInSuccessful: (_context, event)=>{

return (event.data as SignInResult).status === Cloud.status.SUCCESS

isSignInSuccessful: (_context, event) => {
return (event.data as SignInResult).status === Cloud.status.SUCCESS;
},
isAuthorised:(_context,event)=>{
console.warn("authorised "+(event.data as isSignedInResult).isAuthorisedd);
isAuthorised: (_context, event) => {
return (event.data as isSignedInResult).isAuthorisedd || false;
}
},
},
},
);
Expand Down Expand Up @@ -441,14 +435,10 @@ export function selectIsSigningInSuccessful(state: State) {
}

export function selectIsSigningFailure(state: State) {

return state.matches('signIn.error') || state.matches('checkSignIn.error');
}

export function selectIsCloudSignedInFailed(state:State)
{
console.log("cloudsigninfailed")
console.log(state)
return state.matches('checkSignIn.error')
export function selectIsCloudSignedInFailed(state: State) {
return state.matches('checkSignIn.error');
}
type State = StateFrom<typeof backupAndRestoreSetupMachine>;
2 changes: 1 addition & 1 deletion screens/backupAndRestore/BackupAndRestoreScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const BackupAndRestoreScreen: React.FC<BackupAndRestoreProps> = props => {
const restoreController = useBackupRestoreScreen();

const {t} = useTranslation('BackupAndRestore');
console.log("i am backup");

useEffect(() => {
if (!props.isSigningIn) {
backupController.LAST_BACKUP_DETAILS();
Expand Down
103 changes: 44 additions & 59 deletions shared/CloudBackupAndRestoreUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {CloudStorage, CloudStorageScope} from 'react-native-cloud-storage';
import {GOOGLE_ANDROID_CLIENT_ID} from 'react-native-dotenv';
import {readFile, writeFile} from 'react-native-fs';
import {BackupDetails} from '../types/backup-and-restore/backup';
import {AppleButton, appleAuth} from '@invertase/react-native-apple-authentication';
import {
AppleButton,
appleAuth,
} from '@invertase/react-native-apple-authentication';
import jwt_decode from 'jwt-decode';
import {bytesToMB, sleep} from './commonUtil';
import {
Expand Down Expand Up @@ -128,38 +131,30 @@ class Cloud {
return accessToken;
}
}





static async signIn(): Promise<SignInResult | IsIOSResult> {
if (isIOS()) {
let profile;
console.warn('Beginning Apple Authentication');
let profile;

// start a login request
try {
const appleAuthRequestResponse = await appleAuth.performRequest({
requestedOperation: appleAuth.Operation.LOGIN,
requestedScopes: [appleAuth.Scope.FULL_NAME, appleAuth.Scope.EMAIL],
});
// const decodedToken=jwt_decode(appleAuthRequestResponse.identityToken + "")
// console.log('appleAuthRequestResponse', appleAuthRequestResponse);
// console.log(decodedToken);
const {
email,
nonce,
identityToken,
realUserStatus /* etc */,
} = appleAuthRequestResponse;
profile={email:email,picture: null};
await RNSecureStorage.set('userIdentifier', JSON.stringify(appleAuthRequestResponse),{accessible: ACCESSIBLE.WHEN_UNLOCKED});
console.log()
console.warn(`Apple Authentication Completed, ${email}`);
console.warn("profile"+profile)

return {status:this.status.SUCCESS,profileInfo:profile}
// const decodedToken=jwt_decode(appleAuthRequestResponse.identityToken + "")
// console.log('appleAuthRequestResponse', appleAuthRequestResponse);
// console.log(decodedToken);
const {email, nonce, identityToken, realUserStatus /* etc */} =
appleAuthRequestResponse;
profile = {email: email, picture: null};
await RNSecureStorage.set(
'userIdentifier',
JSON.stringify(appleAuthRequestResponse),
{accessible: ACCESSIBLE.WHEN_UNLOCKED},
);

return {status: this.status.SUCCESS, profileInfo: profile};
} catch (error) {
if (error.code === appleAuth.Error.CANCELED) {
console.warn('User canceled Apple Sign in.');
Expand All @@ -168,20 +163,15 @@ class Cloud {
error,
};
} else {

console.error(error);
return {
status: this.status.FAILURE,
error,
};
}
}




// Extract the user's email address from the response


}
this.configure();
try {
Expand Down Expand Up @@ -219,37 +209,32 @@ class Cloud {

static async isSignedInAlready(): Promise<isSignedInResult> {
try {
console.warn("I am in is already signed in")
if (isIOS()) {
console.warn("I am in is already signed in")
const isSignedIn = await CloudStorage.isCloudAvailable();
console.log(isSignedIn)

const userIdentifier = await RNSecureStorage.get('userIdentifier');
const userToken=JSON.parse(userIdentifier+"");
const user=userToken.user;
const email=userToken.email;
console.warn(email)
const userToken = JSON.parse(userIdentifier + '');
const user = userToken.user;
const email = userToken.email;

const credentialState = await appleAuth.getCredentialStateForUser(user);
let profile={email:email,picture:undefined};
if((credentialState === appleAuth.State.AUTHORIZED) && isSignedIn==true)
{

return{
isSignedIn:true,
isAuthorisedd:true,
profileInfo:profile
}
let profile = {email: email, picture: undefined};
if (
credentialState === appleAuth.State.AUTHORIZED &&
isSignedIn == true
) {
return {
isSignedIn: true,
isAuthorisedd: true,
profileInfo: profile,
};
} else {
return {
isSignedIn: false,
isAuthorisedd: true,
profileInfo: profile,
};
}

else{
console.log("cloud "+ isSignedIn);
return {

isSignedIn:false,
isAuthorisedd:true,
profileInfo:profile
};
}
}
this.configure();
const isSignedIn = await GoogleSignin.isSignedIn();
Expand All @@ -259,14 +244,14 @@ class Cloud {
return {
isSignedIn: true,
profileInfo,
isAuthorisedd:true,
isAuthorisedd: true,
};
} else {
const profileInfo = await this.profileInfo();
return {
isSignedIn: true,
profileInfo,
isAuthorisedd:true,
isAuthorisedd: true,
};
}
} catch (error) {
Expand All @@ -280,7 +265,7 @@ class Cloud {
return {
error: errorReason || error,
isSignedIn: false,
isAuthorisedd:false,
isAuthorisedd: false,
};
}
}
Expand Down Expand Up @@ -513,7 +498,7 @@ export type isSignedInResult = {
isSignedIn: boolean;
error?: string | null;
profileInfo?: ProfileInfo;
isAuthorisedd?:boolean;
isAuthorisedd?: boolean;
};

export type CloudUploadResult = {
Expand Down

0 comments on commit d37b1e2

Please sign in to comment.