2021-01-22 11:11:24 +08:00
|
|
|
import { Context } from '@nuxt/types';
|
|
|
|
|
2021-02-04 17:34:24 +08:00
|
|
|
export default async ({ app: { $cookies }, $auth, $api, store, redirect }: Context) => {
|
|
|
|
if ($cookies.get('returnRoute')) {
|
2021-01-22 13:18:09 +08:00
|
|
|
// is returning from login
|
2021-01-23 03:20:03 +08:00
|
|
|
const returnRoute = $cookies.get<string>('returnRoute');
|
2021-02-04 17:34:24 +08:00
|
|
|
$cookies.remove('returnRoute', {
|
|
|
|
path: '/',
|
2021-01-22 13:18:09 +08:00
|
|
|
});
|
2021-02-04 17:34:24 +08:00
|
|
|
$cookies.remove('url', {
|
|
|
|
path: '/',
|
|
|
|
});
|
|
|
|
redirect(returnRoute);
|
|
|
|
} 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) {
|
2021-02-04 17:34:24 +08:00
|
|
|
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 13:18:09 +08:00
|
|
|
}
|
2021-01-22 11:11:24 +08:00
|
|
|
}
|
|
|
|
};
|