mirror of
https://github.com/tiddly-gittly/TidGi-Desktop.git
synced 2026-03-03 04:21:05 -08:00
51 lines
2 KiB
TypeScript
51 lines
2 KiB
TypeScript
import { useCallback, useEffect, useMemo } from 'react';
|
|
import AuthingSSO, { ITrackSessionResult } from '@authing/sso';
|
|
import { AuthenticationClient } from 'authing-js-sdk';
|
|
import { ServiceTokenTypes, ServiceUserNameTypes, ServiceEmailTypes } from '@services/auth/interface';
|
|
import { SupportedStorageServices, IAuthingUserInfo } from '@services/types';
|
|
import { APP_ID, APP_DOMAIN } from '@/constants/auth';
|
|
|
|
export function useAuth(storageService: SupportedStorageServices): [() => Promise<void>, () => Promise<void>] {
|
|
const authing = useMemo(
|
|
() =>
|
|
new AuthenticationClient({
|
|
appId: APP_ID,
|
|
appHost: APP_DOMAIN,
|
|
}),
|
|
[],
|
|
);
|
|
|
|
const onFailure = useCallback((error: Error) => {
|
|
console.error(error);
|
|
}, []);
|
|
const onClickLogout = useCallback(async () => {
|
|
await authing.logout();
|
|
await window.service.window.clearStorageData();
|
|
}, [authing]);
|
|
|
|
const onClickLogin = useCallback(async () => {
|
|
// clear token first, otherwise github login window won't give us a chance to see the form
|
|
// void this.auth.logout();
|
|
// window.remote.clearStorageData();
|
|
try {
|
|
await authing.social.authorize(storageService, {
|
|
onSuccess: async (user) => {
|
|
// DEBUG: console
|
|
console.log('user', user);
|
|
const thirdPartyIdentity = user.identities?.find((identity) => identity?.provider === storageService);
|
|
await Promise.all([
|
|
thirdPartyIdentity?.accessToken !== undefined &&
|
|
window.service.auth.set(`${storageService}-token` as ServiceTokenTypes, thirdPartyIdentity.accessToken),
|
|
window.service.auth.set(`${storageService}-userName` as ServiceUserNameTypes, user.username),
|
|
window.service.auth.set(`${storageService}-email` as ServiceEmailTypes, user.email),
|
|
]);
|
|
},
|
|
onError: (code, message) => onFailure(new Error(message + String(code))),
|
|
});
|
|
} catch (error) {
|
|
onFailure(error);
|
|
}
|
|
}, [authing.social, onFailure, storageService]);
|
|
|
|
return [onClickLogin, onClickLogout];
|
|
}
|