Hangar/frontend/middleware/auth.ts

23 lines
688 B
TypeScript
Raw Normal View History

2021-01-22 11:11:24 +08:00
import { Context } from '@nuxt/types';
2021-03-13 18:10:56 +08:00
export default ({ app: { $cookies }, $auth, redirect }: Context) => {
let shouldRefresh = $cookies.get('HangarAuth_REFRESH', { parseJSON: false });
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);
// only refresh when fake user is enabled
shouldRefresh = process.env.fakeUser || false;
}
if (shouldRefresh) {
2021-03-13 18:10:56 +08:00
return $auth.refreshUser();
2021-01-22 11:11:24 +08:00
}
};