126 lines
2.9 KiB
TypeScript
Raw Normal View History

import { currentBackend } from '../backend';
2022-09-28 20:04:00 -06:00
import { addSnackbar } from '../store/slices/snackbars';
import type { AnyAction } from 'redux';
2022-10-20 11:57:30 -04:00
import type { ThunkDispatch } from 'redux-thunk';
import type { Credentials, User } from '../interface';
import type { RootState } from '../store';
export const AUTH_REQUEST = 'AUTH_REQUEST';
export const AUTH_SUCCESS = 'AUTH_SUCCESS';
export const AUTH_FAILURE = 'AUTH_FAILURE';
export const AUTH_REQUEST_DONE = 'AUTH_REQUEST_DONE';
export const LOGOUT = 'LOGOUT';
export function authenticating() {
return {
type: AUTH_REQUEST,
} as const;
}
export function authenticate(userData: User) {
return {
type: AUTH_SUCCESS,
payload: userData,
} as const;
}
export function authError(error: Error) {
return {
type: AUTH_FAILURE,
error: 'Failed to authenticate',
payload: error,
} as const;
}
export function doneAuthenticating() {
return {
type: AUTH_REQUEST_DONE,
} as const;
}
export function logout() {
return {
type: LOGOUT,
} as const;
}
// Check if user data token is cached and is valid
export function authenticateUser() {
2022-10-20 11:57:30 -04:00
return (dispatch: ThunkDispatch<RootState, {}, AnyAction>, getState: () => RootState) => {
const state = getState();
2022-10-20 11:57:30 -04:00
if (!state.config.config) {
return;
}
const backend = currentBackend(state.config.config);
dispatch(authenticating());
return Promise.resolve(backend.currentUser())
.then(user => {
if (user) {
dispatch(authenticate(user));
} else {
dispatch(doneAuthenticating());
}
})
.catch((error: Error) => {
dispatch(authError(error));
dispatch(logoutUser());
});
};
}
export function loginUser(credentials: Credentials) {
2022-10-20 11:57:30 -04:00
return (dispatch: ThunkDispatch<RootState, {}, AnyAction>, getState: () => RootState) => {
const state = getState();
2022-10-20 11:57:30 -04:00
if (!state.config.config) {
return;
}
const backend = currentBackend(state.config.config);
dispatch(authenticating());
return backend
.authenticate(credentials)
.then(user => {
dispatch(authenticate(user));
})
.catch((error: Error) => {
console.error(error);
dispatch(
2022-09-28 20:04:00 -06:00
addSnackbar({
type: 'warning',
message: {
key: 'ui.toast.onFailToAuth',
2022-09-28 20:04:00 -06:00
message: error.message,
},
}),
);
dispatch(authError(error));
});
};
}
export function logoutUser() {
2022-10-20 11:57:30 -04:00
return (dispatch: ThunkDispatch<RootState, {}, AnyAction>, getState: () => RootState) => {
const state = getState();
2022-10-20 11:57:30 -04:00
if (!state.config.config) {
return;
}
const backend = currentBackend(state.config.config);
Promise.resolve(backend.logout()).then(() => {
dispatch(logout());
});
};
}
export type AuthAction = ReturnType<
| typeof authenticating
| typeof authenticate
| typeof authError
| typeof doneAuthenticating
| typeof logout
>;