Hangar/frontend/middleware/auth.ts

30 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-01-22 11:11:24 +08:00
import { Context } from '@nuxt/types';
2021-02-03 07:47:15 +08:00
export default async ({ store, app: { $cookies }, $auth, $api, redirect, route }: Context) => {
if ($cookies.get('returnRoute') && route.query.token) {
// is returning from login
2021-02-03 07:47:15 +08:00
console.log('returning from login');
2021-01-23 03:20:03 +08:00
const returnRoute = $cookies.get<string>('returnRoute');
2021-02-03 07:47:15 +08:00
store.commit('auth/SET_TOKEN', route.query.token);
return $auth.processLogin(<string>route.query.token).then(() => {
$cookies.remove('returnRoute', {
path: '/',
});
2021-02-03 07:47:15 +08:00
console.log('before redirect');
redirect(returnRoute);
});
} else {
2021-02-03 07:47:15 +08:00
const token = await $api.refreshToken();
if (token != null) {
return $auth.processLogin(token);
}
2021-02-03 07:47:15 +08:00
// const session = $cookies.get<ApiSession>('api_session');
// if (!session || session.type !== ApiSessionType.USER) return;
// if (!store.state.auth.authenticated) {
// return $auth.processLogin(store.state.auth.token);
// } else {
// return $auth.updateUser(store.state.auth.token);
// }
2021-01-22 11:11:24 +08:00
}
};