Frontend/src/auth/data/authActions.ts
2022-07-18 11:38:44 +03:00

40 lines
1.1 KiB
TypeScript

import { AuthCredentials } from "auth/domain/authEntity";
import * as actionTypes from "./authActionTypes";
import { auth, dropTokens, getTokens, signout } from "./authService";
const authInitAction = () => (dispatch: any) => {
dispatch({ type: actionTypes.AUTH_LOGIN });
const [access, refresh] = getTokens();
if (access && refresh) {
dispatch({ type: actionTypes.AUTH_LOGIN_COMPLETE });
return;
}
dispatch({ type: actionTypes.AUTH_LOGIN_FAILED });
};
const authenticateAction =
(code: string) =>
(dispatch: any): Promise<AuthCredentials | void> => {
dispatch({ type: actionTypes.AUTH_LOGIN });
return auth(code)
.then((credentials) => {
dispatch({ type: actionTypes.AUTH_LOGIN_COMPLETE });
return credentials;
})
.catch((reason) => {
dispatch({ type: actionTypes.AUTH_LOGIN_FAILED });
});
};
const dropAuth =
() =>
async (dispatch: any): Promise<void> => {
try {
await signout();
} catch (error) {}
dropTokens();
dispatch({ type: actionTypes.DROP_AUTH });
};
export { authenticateAction, authInitAction, dropAuth };