-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
338 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import useLoading, { ErrorOrNull } from './useLoading'; | ||
import generateTokenFromPassword from '../utils/generateTokenFromPassword'; | ||
import getNewSessionFromCloudSync from '../api/cloudSync/getNewSession'; | ||
import getAccountFromCloudSync from '../api/cloudSync/getAccount'; | ||
import decryptAndSaveUserFromBase64 from '../services/decryptAndSaveUserFromBase64'; | ||
import saveFileCollectionFromBase64 from '../services/saveFileCollectionFromBase64'; | ||
import { useStoreActions } from '../store/typedHooks'; | ||
import loadUserFromDisk from '../services/loadUserFromDisk'; | ||
import syncAccountAndNotesToCloudSync from '../services/syncAccountAndNotesToCloudSync'; | ||
|
||
type LoginPayload = { | ||
username: string; | ||
password: string; | ||
}; | ||
|
||
const useCloudSyncLogin = (): { | ||
start: (payload: LoginPayload) => Promise<void>; | ||
isLoading: boolean; | ||
error: ErrorOrNull; | ||
isComplete: boolean; | ||
reset: () => void; | ||
} => { | ||
const [isLoading, error, isComplete, setIsLoading, setError, setIsComplete, reset] = useLoading(); | ||
|
||
const setUser = useStoreActions((a) => a.setUser); | ||
const setPasswordKey = useStoreActions((a) => a.setPasswordKey); | ||
const setCloudSyncPasswordKey = useStoreActions((a) => a.setCloudSyncPasswordKey); | ||
const setFileCollection = useStoreActions((a) => a.setFileCollection); | ||
|
||
const startLogin = async ({ username, password }: LoginPayload) => { | ||
setIsLoading(true); | ||
|
||
// Verify account and get sessionToken | ||
const token = await generateTokenFromPassword(password, username); | ||
|
||
const session = await getNewSessionFromCloudSync({ | ||
username, | ||
token, | ||
}); | ||
|
||
if ('error' in session) { | ||
switch (session.error) { | ||
case 'user does not exist': | ||
setError({ ...session, errorCode: 'no_user' }); | ||
break; | ||
case 'token does not match': | ||
setError({ ...session, errorCode: 'wrong_password' }); | ||
break; | ||
default: | ||
setError(session); | ||
} | ||
|
||
return; | ||
} | ||
|
||
const sessionToken = session.sessionToken; | ||
|
||
// Fetch account, then save user and fileCollection to disk | ||
const fetchAccount = await getAccountFromCloudSync({ | ||
username, | ||
sessionToken, | ||
}); | ||
|
||
if ('error' in fetchAccount) { | ||
setError(fetchAccount); | ||
return; | ||
} | ||
|
||
const saveUser = await decryptAndSaveUserFromBase64(username, password, fetchAccount.userItem); | ||
|
||
if ('error' in saveUser) { | ||
setError(saveUser); | ||
return; | ||
} | ||
|
||
await saveFileCollectionFromBase64(saveUser.userItem, fetchAccount.fileCollection); | ||
|
||
// Load user from disk | ||
const loadUser = await loadUserFromDisk({ username, password }); | ||
|
||
if ('error' in loadUser) { | ||
setError(loadUser); | ||
return; | ||
} | ||
|
||
// Start full sync | ||
const sync = await syncAccountAndNotesToCloudSync({ | ||
sessionToken, | ||
user: loadUser.user, | ||
fileCollection: loadUser.fileCollection, | ||
fileCollectionNonce: loadUser.user.fileCollectionNonce, | ||
passwordKey: loadUser.passwordKey, | ||
cloudSyncPasswordKey: loadUser.cloudSyncPasswordKey, | ||
}); | ||
|
||
if ('error' in sync) { | ||
setError(sync); | ||
return; | ||
} | ||
|
||
setUser(loadUser.user); | ||
setPasswordKey(loadUser.passwordKey); | ||
setCloudSyncPasswordKey(loadUser.cloudSyncPasswordKey); | ||
setFileCollection(loadUser.fileCollection); | ||
|
||
setIsComplete(true); | ||
}; | ||
|
||
return { start: startLogin, isLoading, error, isComplete, reset }; | ||
}; | ||
|
||
export default useCloudSyncLogin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.