Files
directus/src/auth.ts
rijkvanzanten e506b2dd95 Squashed commit of the following:
commit aabfcdb9fc75789e8b68a026297651dc3b9a432d
Author: rijkvanzanten <rijkvanzanten@me.com>
Date:   Fri Jul 3 11:40:44 2020 -0400

    Fix more projects stuff

commit 5df3364e0887627fca8361297b92a9ff5b36c98f
Author: rijkvanzanten <rijkvanzanten@me.com>
Date:   Fri Jul 3 11:36:28 2020 -0400

    More project deletions

commit 75d49100adffee5ad94736e62470cffa89a44aa3
Author: rijkvanzanten <rijkvanzanten@me.com>
Date:   Fri Jul 3 11:32:04 2020 -0400

    Batch get rid of projects

commit 02a64a5d4c6ae02ece42878dc10560b662b1dfca
Author: rijkvanzanten <rijkvanzanten@me.com>
Date:   Fri Jul 3 11:06:20 2020 -0400

    Some more things

commit 617e22e0abd5d6bf57e1eac95b9a68287cd0044b
Author: rijkvanzanten <rijkvanzanten@me.com>
Date:   Fri Jul 3 10:35:43 2020 -0400

    Fix module bar logo projects check

commit a8fbf0be75055c31075f2352b150309fea424e8a
Author: rijkvanzanten <rijkvanzanten@me.com>
Date:   Fri Jul 3 10:35:24 2020 -0400

    Set auth token in auth handler

commit c0c1c1bb164bd49d252c6dd1d944d7bc4b6aede5
Author: rijkvanzanten <rijkvanzanten@me.com>
Date:   Fri Jul 3 10:18:30 2020 -0400

    public login
2020-07-03 11:40:54 -04:00

73 lines
1.7 KiB
TypeScript

import { RawLocation } from 'vue-router';
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() {
/** @todo base this on existence of access token / response token */
return true;
// try {
// const response = await api.get(`/auth/check`);
// return response.data.data.authenticated;
// } catch {
// return false;
// }
}
export type LoginCredentials = {
email: string;
password: string;
};
export async function login(credentials: LoginCredentials) {
const response = await api.post(`/auth/authenticate`, {
...credentials,
mode: 'cookie',
});
api.defaults.headers['Authorization'] = `Bearer ${response.data.data.token}`;
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 };
// 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(`/auth/logout`);
}
await dehydrate();
if (options.navigate === true) {
const location: RawLocation = {
path: `/login`,
query: { reason: options.reason },
};
router.push(location);
}
}