Hangar/frontend/middleware/auth.ts

29 lines
1.0 KiB
TypeScript
Raw Normal View History

2021-01-22 11:11:24 +08:00
import { Context } from '@nuxt/types';
export default async ({ app: { $cookies }, $auth, $api, store, redirect }: Context) => {
if ($cookies.get('returnRoute')) {
// is returning from login
2021-01-23 03:20:03 +08:00
const returnRoute = $cookies.get<string>('returnRoute');
$cookies.remove('returnRoute', {
path: '/',
});
$cookies.remove('url', {
path: '/',
});
redirect(returnRoute);
2021-03-10 12:07:53 +08:00
// TODO if not running hangarauth locally, this needs to just be a regular if not an else-if (idk what a good fix for that is)
} else if ($cookies.get('HangarAuth_REFRESH', { parseJSON: false })) {
const token = await $api.getToken(true);
2021-02-03 07:47:15 +08:00
if (token != null) {
if (store.state.auth.authenticated) {
return $auth.updateUser(token);
} else {
return $auth.processLogin(token);
}
} else {
2021-02-10 16:31:43 +08:00
console.log('LOGGING OUT VIA MIDDLEWARE');
2021-02-05 04:30:47 +08:00
return $auth.logout(process.client);
}
2021-01-22 11:11:24 +08:00
}
};