Files
directus/src/auth.ts
2020-05-22 17:59:28 -04:00

83 lines
2.0 KiB
TypeScript

import { RawLocation } from 'vue-router';
import { useProjectsStore } from '@/stores/projects/';
import api from '@/api';
import { hydrate, dehydrate } from '@/hydrate';
import router from '@/router';
/**
* Check if the current user is authenticated to the current project
*/
export async function checkAuth() {
const { currentProjectKey } = useProjectsStore().state;
if (!currentProjectKey) return false;
try {
const response = await api.get(`/${currentProjectKey}/auth/check`);
return response.data.data.authenticated;
} catch {
return false;
}
}
export type LoginCredentials = {
email: string;
password: string;
};
export async function login(credentials: LoginCredentials) {
const projectsStore = useProjectsStore();
const { currentProjectKey } = projectsStore.state;
await api.post(`/${currentProjectKey}/auth/authenticate`, {
...credentials,
mode: 'cookie',
});
await hydrate();
}
export enum LogoutReason {
SIGN_OUT = 'SIGN_OUT',
ERROR_SESSION_EXPIRED = 'ERROR_SESSION_EXPIRED',
}
export type LogoutOptions = {
navigate?: boolean;
reason?: LogoutReason;
};
/**
* Everything that should happen when someone logs out, or is logged out through an external factor
*/
export async function logout(optionsRaw: LogoutOptions = {}) {
const defaultOptions: Required<LogoutOptions> = {
navigate: true,
reason: LogoutReason.SIGN_OUT,
};
const options = { ...defaultOptions, ...optionsRaw };
const projectsStore = useProjectsStore();
const { currentProjectKey } = projectsStore.state;
// You can't logout of a project if you're not in a project
if (currentProjectKey === null) return;
// Only if the user manually signed out should we kill the session by hitting the logout endpoint
if (options.reason === LogoutReason.SIGN_OUT) {
await api.post(`/${currentProjectKey}/auth/logout`);
}
await dehydrate();
if (options.navigate === true) {
const location: RawLocation = {
path: `/${currentProjectKey}/login`,
query: { reason: options.reason },
};
router.push(location);
}
}