build: switch from yarn to npm to manage js dependencies and move js contents to root
yarn v1 is being deprecated and starts to have some issues Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
22
src/router/guards/auth-guard.ts
Normal file
22
src/router/guards/auth-guard.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { NavigationGuard } from "vue-router";
|
||||
import { UserRouteName } from "@/router/user";
|
||||
import { AUTH_ACCESS_TOKEN } from "@/constants";
|
||||
import { LoginErrorCode } from "@/types/enums";
|
||||
|
||||
export const authGuardIfNeeded: NavigationGuard = async (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_ACCESS_TOKEN)) {
|
||||
return next({
|
||||
name: UserRouteName.LOGIN,
|
||||
query: {
|
||||
code: LoginErrorCode.NEED_TO_LOGIN,
|
||||
redirect: to.fullPath,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return next();
|
||||
};
|
||||
33
src/router/guards/register-guard.ts
Normal file
33
src/router/guards/register-guard.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { IConfig } from "@/types/config.model";
|
||||
import { ErrorCode } from "@/types/enums";
|
||||
import { provideApolloClient, useQuery } from "@vue/apollo-composable";
|
||||
import { NavigationGuard } from "vue-router";
|
||||
import { CONFIG } from "../../graphql/config";
|
||||
import { apolloClient } from "../../vue-apollo";
|
||||
import { ErrorRouteName } from "../error";
|
||||
|
||||
export const beforeRegisterGuard: NavigationGuard = async (to, from, next) => {
|
||||
const { onResult, onError } = provideApolloClient(apolloClient)(() =>
|
||||
useQuery<{ config: IConfig }>(CONFIG)
|
||||
);
|
||||
|
||||
onResult(({ data }) => {
|
||||
if (!data) return next();
|
||||
const { config } = data;
|
||||
|
||||
if (!config.registrationsOpen && !config.registrationsAllowlist) {
|
||||
return next({
|
||||
name: ErrorRouteName.ERROR,
|
||||
query: { code: ErrorCode.REGISTRATION_CLOSED },
|
||||
});
|
||||
}
|
||||
|
||||
return next();
|
||||
});
|
||||
|
||||
onError((err) => {
|
||||
console.error(err);
|
||||
return next();
|
||||
});
|
||||
return next();
|
||||
};
|
||||
Reference in New Issue
Block a user