felicity-lims/webapp/stores/auth.ts

98 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-04-10 09:29:10 +08:00
import { defineStore } from 'pinia';
import { ref, watch } from 'vue';
import { IUser } from '../models/auth';
import { STORAGE_AUTH_KEY, USER_GROUP_OVERRIDE } from '../conf';
import { AUTHENTICATE_USER } from '../graphql/_mutations';
import { useNotifyToast, useApiUtil, userPreferenceComposable } from '../composables';
const { withClientMutation } = useApiUtil();
const { toastInfo } = useNotifyToast();
2023-04-10 09:29:10 +08:00
const { initPreferences } = userPreferenceComposable();
2022-04-01 04:29:09 +08:00
interface IAuth {
2023-04-10 09:29:10 +08:00
token?: string;
tokenType?: string;
user?: IUser;
isAuthenticated: boolean;
authenticating: boolean;
2022-04-01 04:29:09 +08:00
}
export const useAuthStore = defineStore('auth', () => {
const initialState: IAuth = {
2023-04-10 09:29:10 +08:00
user: undefined,
token: '',
tokenType: '',
isAuthenticated: false,
2023-04-10 09:29:10 +08:00
authenticating: false,
};
2022-04-01 04:29:09 +08:00
const auth = ref({ ...initialState });
2022-04-01 04:29:09 +08:00
2023-04-10 09:29:10 +08:00
const resetState = () => (auth.value = { ...initialState });
2022-04-01 04:29:09 +08:00
2022-04-04 02:54:31 +08:00
const reset = () => {
2023-04-10 09:29:10 +08:00
localStorage.removeItem(STORAGE_AUTH_KEY);
resetState();
};
2022-04-04 02:54:31 +08:00
const logout = () => {
2023-04-10 09:29:10 +08:00
toastInfo('Good bye ' + auth.value.user?.firstName);
reset();
};
2022-04-01 04:29:09 +08:00
const upsertPermission = () => {
2023-04-10 09:29:10 +08:00
if (USER_GROUP_OVERRIDE.length > 0) {
auth.value.user?.groups?.forEach(group => ({
...group,
name: USER_GROUP_OVERRIDE,
}));
2022-04-01 04:29:09 +08:00
}
2023-04-10 09:29:10 +08:00
};
2022-04-01 04:29:09 +08:00
2023-04-10 09:29:10 +08:00
if (localStorage.getItem(STORAGE_AUTH_KEY)) {
const data = JSON.parse(localStorage.getItem(STORAGE_AUTH_KEY)!);
auth.value = {
...auth.value,
...data,
isAuthenticated: true,
authenticating: false,
};
upsertPermission();
2022-04-01 04:29:09 +08:00
} else {
2022-04-04 02:54:31 +08:00
// logout()
2022-04-01 04:29:09 +08:00
}
2023-04-10 09:29:10 +08:00
watch(auth, authValue => {
if (authValue && authValue.user && authValue.token) {
localStorage.setItem(STORAGE_AUTH_KEY, JSON.stringify(authValue));
upsertPermission();
2022-04-01 04:29:09 +08:00
}
2023-04-10 09:29:10 +08:00
});
2022-04-01 04:29:09 +08:00
2023-04-10 09:29:10 +08:00
const persistAuth = async data => {
auth.value = data;
2022-04-15 16:32:24 +08:00
auth.value.isAuthenticated = true;
2023-04-10 09:29:10 +08:00
auth.value.authenticating = false;
};
2023-04-10 09:29:10 +08:00
const authenticate = async payload => {
auth.value.authenticating = true;
await withClientMutation(AUTHENTICATE_USER, payload, 'authenticateUser')
.then(res => {
toastInfo('Welcome back ' + res?.user?.firstName);
initPreferences(res.user?.preference);
persistAuth(res);
// .then((_) => router.push({ name: "DASHBOARD" }));
})
.catch(err => (auth.value.authenticating = false));
};
2022-04-01 04:29:09 +08:00
2023-04-10 09:29:10 +08:00
return {
auth,
authenticate,
2022-04-04 02:54:31 +08:00
reset,
2022-04-01 04:29:09 +08:00
persistAuth,
2023-04-10 09:29:10 +08:00
logout,
};
});