import { Context } from '@nuxt/types'; import { Inject } from '@nuxt/types/app'; import { User } from 'hangar-api'; const createAuth = ({ app: { $cookies }, store, $api }: Context) => { class Auth { login(redirect: string): void { $cookies.set('returnRoute', redirect, { path: '/', maxAge: 120, // secure: true, // this doesn't work for me for some reason }); location.replace(`/login?returnUrl=http://localhost:3000${redirect}`); } processLogin(): Promise { store.commit('auth/SET_AUTHED', true); return this.updateUser(); } logout(): void { $api.invalidateSession(); location.reload(); // location.replace('/logout'); // TODO uncomment (maybe have a "full log out" system seperate so you dont have to log out from all paper sites?) } updateUser(): Promise { return $api.requestInternal('users/@me').then((user) => { store.commit('auth/SET_USER', user); }); } } return new Auth(); }; type authType = ReturnType; declare module 'vue/types/vue' { interface Vue { $auth: authType; } } declare module '@nuxt/types' { interface NuxtAppOptions { $auth: authType; } interface Context { $auth: authType; } } declare module 'vuex/types/index' { interface Store { $auth: authType; } } export default (ctx: Context, inject: Inject) => { inject('auth', createAuth(ctx)); };