mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 04:46:40 +07:00
removed redux completely
This commit is contained in:
parent
26e6d8d41b
commit
a4bb07e9cf
323 changed files with 2464 additions and 3348 deletions
101
src/hooks/auth/useOAuth.ts
Normal file
101
src/hooks/auth/useOAuth.ts
Normal file
|
@ -0,0 +1,101 @@
|
|||
import { useCallback } from 'react';
|
||||
import { OAuthProvider } from '~/types/auth';
|
||||
import { API } from '~/constants/api';
|
||||
import { apiAttachSocial, apiDropSocial, apiGetSocials, apiLoginWithSocial } from '~/api/auth';
|
||||
import { useModal } from '~/hooks/modal/useModal';
|
||||
import { Dialog } from '~/constants/modal';
|
||||
import { showErrorToast } from '~/utils/errors/showToast';
|
||||
import { path } from 'ramda';
|
||||
import useSWR from 'swr';
|
||||
import { useAuth } from '~/hooks/auth/useAuth';
|
||||
|
||||
export const useOAuth = () => {
|
||||
const { isUser, setToken } = useAuth();
|
||||
const { showModal, hideModal } = useModal();
|
||||
|
||||
const { data, isValidating: isLoading, mutate } = useSWR(
|
||||
isUser ? API.USER.GET_SOCIALS : null,
|
||||
async () => {
|
||||
const result = await apiGetSocials();
|
||||
return result.accounts;
|
||||
}
|
||||
);
|
||||
|
||||
const openOauthWindow = useCallback((provider: OAuthProvider) => {
|
||||
window.open(API.USER.OAUTH_WINDOW(provider), '', 'width=600,height=400');
|
||||
}, []);
|
||||
|
||||
const createSocialAccount = useCallback(
|
||||
async (token?: string) => {
|
||||
try {
|
||||
if (!token) return;
|
||||
|
||||
const result = await apiLoginWithSocial({ token });
|
||||
|
||||
setToken(result.token);
|
||||
hideModal();
|
||||
} catch (error) {
|
||||
const needsRegister: string | undefined = path(
|
||||
['response', 'data', 'needs_register'],
|
||||
error
|
||||
);
|
||||
|
||||
if (needsRegister && token) {
|
||||
showModal(Dialog.LoginSocialRegister, { token });
|
||||
return;
|
||||
}
|
||||
|
||||
showErrorToast(error);
|
||||
}
|
||||
},
|
||||
[showModal, hideModal, setToken]
|
||||
);
|
||||
|
||||
const loginWithSocial = useCallback(
|
||||
(token: string | undefined) => {
|
||||
if (!token) {
|
||||
return;
|
||||
}
|
||||
|
||||
setToken(token);
|
||||
hideModal();
|
||||
},
|
||||
[setToken, hideModal]
|
||||
);
|
||||
|
||||
const attachAccount = useCallback(
|
||||
async (token?: string) => {
|
||||
try {
|
||||
if (!token) return;
|
||||
|
||||
await apiAttachSocial({ token });
|
||||
await mutate();
|
||||
} catch (error) {
|
||||
showErrorToast(error);
|
||||
}
|
||||
},
|
||||
[mutate]
|
||||
);
|
||||
|
||||
const dropAccount = useCallback(
|
||||
async (provider: OAuthProvider, id: string) => {
|
||||
try {
|
||||
await apiDropSocial({ id, provider });
|
||||
await mutate();
|
||||
} catch (error) {
|
||||
showErrorToast(error);
|
||||
}
|
||||
},
|
||||
[mutate]
|
||||
);
|
||||
|
||||
return {
|
||||
openOauthWindow,
|
||||
loginWithSocial,
|
||||
createSocialAccount,
|
||||
attachAccount,
|
||||
dropAccount,
|
||||
accounts: data || [],
|
||||
isLoading: !data && isLoading,
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue