146 lines
4.1 KiB
TypeScript
146 lines
4.1 KiB
TypeScript
import { ThunkAction } from 'redux-thunk';
|
|
import { StoreState } from '@Reducers/_InitializeStore/types';
|
|
import { MiddleWare } from '@Reducers/_initializeMiddleware/types';
|
|
import { errorCatch } from '@Reducers/_Capture/errorCapture';
|
|
import { postGlobalReduxReset } from '@Reducers/global/actions';
|
|
import { delay } from '@Tools/utility';
|
|
import {
|
|
checkUserTokenIsValid,
|
|
saveLocalUserToken,
|
|
readLocalUserToken,
|
|
removeLocalUserToken,
|
|
} from '@Reducers/_Capture/tokenCapture';
|
|
import USER_ACTION from '@Reducers/_Constants/User';
|
|
import {
|
|
SetUserLoginStateAction,
|
|
SetUserAuthStateAction,
|
|
SetUserTokenAction,
|
|
SetUserAccountInfoAction,
|
|
SetUserOAuthAction,
|
|
SetUserSignOutAction,
|
|
UserAccountInfo,
|
|
UserOAuthUrl,
|
|
UserSignOutUrl,
|
|
} from './types';
|
|
|
|
/* User */
|
|
export const SET_USER_LOGIN_STATE = ({ isLogin }: { isLogin: boolean }): SetUserLoginStateAction => ({
|
|
type: USER_ACTION.SET_USER_LOGIN_STATE,
|
|
isLogin,
|
|
});
|
|
export const SET_USER_AUTH_STATE = ({ isAuth }: { isAuth: boolean }): SetUserAuthStateAction => ({
|
|
type: USER_ACTION.SET_USER_AUTH_STATE,
|
|
isAuth,
|
|
});
|
|
export const SET_USER_TOKEN = ({ token }: { token: string }): SetUserTokenAction => ({
|
|
type: USER_ACTION.SET_USER_TOKEN,
|
|
token,
|
|
});
|
|
export const SET_USER_ACCOUNT_INFO = ({ accountInfo }: { accountInfo: UserAccountInfo }): SetUserAccountInfoAction => ({
|
|
type: USER_ACTION.SET_USER_ACCOUNT_INFO,
|
|
accountInfo,
|
|
});
|
|
export const SET_USER_OAUTH_URL = ({ url }: { url: UserOAuthUrl }): SetUserOAuthAction => ({
|
|
type: USER_ACTION.SET_USER_OAUTH_URL,
|
|
url,
|
|
});
|
|
export const SET_USER_SIGN_OUT_URL = ({ url }: { url: UserSignOutUrl }): SetUserSignOutAction => ({
|
|
type: USER_ACTION.SET_USER_SIGN_OUT_URL,
|
|
url,
|
|
});
|
|
|
|
/* User Action */
|
|
export const getUserAccountInfo = (): ThunkAction<Promise<void>, StoreState, MiddleWare, { type: string }> => async (
|
|
dispatch,
|
|
getState,
|
|
{ api, env },
|
|
): Promise<void> => {
|
|
const refreshSuccess = await checkUserTokenIsValid(env, api, dispatch);
|
|
if (!refreshSuccess) return;
|
|
const result = await api.user.getUserAccountInfo();
|
|
dispatch(
|
|
SET_USER_ACCOUNT_INFO({
|
|
accountInfo: result,
|
|
}),
|
|
);
|
|
if (result.error) {
|
|
dispatch(errorCatch(result.error));
|
|
}
|
|
};
|
|
export const getUserSSO = (
|
|
backUrl: string,
|
|
): ThunkAction<Promise<void>, StoreState, MiddleWare, { type: string }> => async (
|
|
dispatch,
|
|
getState,
|
|
{ api },
|
|
): Promise<void> => {
|
|
const result = await api.user.getUserSSO(backUrl);
|
|
await delay(500);
|
|
if (!result.error) {
|
|
dispatch(
|
|
SET_USER_OAUTH_URL({
|
|
url: result,
|
|
}),
|
|
);
|
|
}
|
|
if (result.error) {
|
|
dispatch(errorCatch(result.error));
|
|
}
|
|
};
|
|
export const getUserIsLogin = (): ThunkAction<Promise<void>, StoreState, MiddleWare, { type: string }> => async (
|
|
dispatch,
|
|
getState,
|
|
{ api, env },
|
|
): Promise<void> => {
|
|
const isValidToken = await readLocalUserToken(env, api);
|
|
if (isValidToken) {
|
|
dispatch(SET_USER_LOGIN_STATE({ isLogin: true }));
|
|
dispatch(getUserAccountInfo());
|
|
} else {
|
|
api.removeAccessToken();
|
|
removeLocalUserToken(env);
|
|
}
|
|
await delay(500);
|
|
dispatch(SET_USER_AUTH_STATE({ isAuth: true }));
|
|
};
|
|
export const postUserTokenInfoSignIn = (
|
|
token: string,
|
|
): ThunkAction<Promise<void>, StoreState, MiddleWare, { type: string }> => async (
|
|
dispatch,
|
|
getState,
|
|
{ api, env },
|
|
): Promise<void> => {
|
|
api.updateAccessToken(token);
|
|
saveLocalUserToken(env, token);
|
|
dispatch(SET_USER_TOKEN({ token }));
|
|
dispatch(SET_USER_LOGIN_STATE({ isLogin: true }));
|
|
dispatch(getUserAccountInfo());
|
|
await delay(500);
|
|
};
|
|
export const postUserSignOut = (
|
|
callback?: () => void,
|
|
): ThunkAction<Promise<void>, StoreState, MiddleWare, { type: string }> => async (
|
|
dispatch,
|
|
getState,
|
|
{ api, env },
|
|
): Promise<void> => {
|
|
const result = await api.user.postUserSignOut();
|
|
if (!result.error) {
|
|
if (result.url) {
|
|
dispatch(
|
|
SET_USER_SIGN_OUT_URL({
|
|
url: result,
|
|
}),
|
|
);
|
|
} else {
|
|
dispatch(postGlobalReduxReset());
|
|
if (callback) callback();
|
|
}
|
|
api.removeAccessToken();
|
|
removeLocalUserToken(env);
|
|
}
|
|
if (result.error) {
|
|
dispatch(errorCatch(result.error));
|
|
}
|
|
};
|