Add error page and login error redirection
This commit is contained in:
21
js/src/router/guards/auth-guard.ts
Normal file
21
js/src/router/guards/auth-guard.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { NavigationGuard } from 'vue-router';
|
||||
import { UserRouteName } from '@/router/user';
|
||||
import { LoginErrorCode } from '@/types/login-error-code.model';
|
||||
import { AUTH_TOKEN } from '@/constants';
|
||||
|
||||
export const authGuardIfNeeded: NavigationGuard = async function (to, from, next) {
|
||||
if (to.meta.requiredAuth !== true) return next();
|
||||
|
||||
// We can't use "currentUser" from apollo here because we may not have loaded the user from the local storage yet
|
||||
if (!localStorage.getItem(AUTH_TOKEN)) {
|
||||
return next({
|
||||
name: UserRouteName.LOGIN,
|
||||
query: {
|
||||
code: LoginErrorCode.NEED_TO_LOGIN,
|
||||
redirect: to.fullPath,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
23
js/src/router/guards/register-guard.ts
Normal file
23
js/src/router/guards/register-guard.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { apolloProvider } from '@/vue-apollo';
|
||||
import { CONFIG } from '@/graphql/config';
|
||||
import { IConfig } from '@/types/config.model';
|
||||
import { NavigationGuard } from 'vue-router';
|
||||
import { ErrorRouteName } from '@/router/error';
|
||||
import { ErrorCode } from '@/types/error-code.model';
|
||||
|
||||
export const beforeRegisterGuard: NavigationGuard = async function (to, from, next) {
|
||||
const { data } = await apolloProvider.defaultClient.query({
|
||||
query: CONFIG,
|
||||
});
|
||||
|
||||
const config: IConfig = data.config;
|
||||
|
||||
if (config.registrationsOpen === false) {
|
||||
return next({
|
||||
name: ErrorRouteName.ERROR,
|
||||
query: { code: ErrorCode.REGISTRATION_CLOSED },
|
||||
});
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
Reference in New Issue
Block a user