Skip to content

Commit

Permalink
Add plugin, router AuthGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
lcharette committed Jul 26, 2024
1 parent dbfb7d0 commit defbcdf
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 3 deletions.
55 changes: 55 additions & 0 deletions app/assets/guards/authGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { watchEffect } from 'vue'
import { useAuthStore } from '../stores/auth'
import type { Router } from 'vue-router'

export function useAuthGuard(router: Router) {
const auth = useAuthStore()

/**
* Return the auth RouteGuard
*/
const getRouteAuth = () => {
return router.currentRoute.value.meta.auth ?? null
}

/**
* Return the guest RouteGuard
*/
const getRouteGuest = () => {
return router.currentRoute.value.meta.guest ?? null
}

/**
* Apply auth route guard
*/
const applyAuthGuard = () => {
const authGuard = getRouteAuth()
if (authGuard !== null && !auth.isAuthenticated) {
const redirectTo = authGuard.redirect ?? '/login'
redirect(redirectTo)
}
}

/**
* Apply guest route guard
*/
const applyGuestGuard = () => {
const guestGuard = getRouteGuest()
if (guestGuard !== null && auth.isAuthenticated) {
const redirectTo = guestGuard.redirect ?? '/'
redirect(redirectTo)
}
}

/**
* Redirect to the specified route
*/
const redirect = (redirectTo: string | { name: string }) => {
router.push(redirectTo)
}

watchEffect(() => {
applyAuthGuard()
applyGuestGuard()
})
}
3 changes: 2 additions & 1 deletion app/assets/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { LoginForm } from './loginForm'
import type { UserInterface } from './userInterface'
import { type AlertInterface, AlertStyle } from './alerts'
import type { RouteGuard } from './routes'

export { type LoginForm, type UserInterface, type AlertInterface, AlertStyle }
export { type LoginForm, type UserInterface, type AlertInterface, type RouteGuard, AlertStyle }
12 changes: 12 additions & 0 deletions app/assets/interfaces/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'vue-router'

export interface RouteGuard {
redirect: string | { name: string }
}

declare module 'vue-router' {
interface RouteMeta {
auth?: RouteGuard
guest?: RouteGuard
}
}
17 changes: 17 additions & 0 deletions app/assets/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { App } from 'vue'
import { useAuthStore } from './stores/auth'
import { useAuthGuard } from './guards/authGuard'
import type { Router } from 'vue-router'

/* Install plugins */
export default {
install: (app: App, options: { router: Router }) => {
// Run auth check on load
const auth = useAuthStore()
auth.check()

// Setup router guards
const { router } = options
useAuthGuard(router)
}
}
1 change: 1 addition & 0 deletions dist/guards.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const i=require("vue"),l=require("./stores.cjs");function d(e){const r=l.useAuthStore(),c=()=>e.currentRoute.value.meta.auth??null,s=()=>e.currentRoute.value.meta.guest??null,o=()=>{const t=c();if(t!==null&&!r.isAuthenticated){const u=t.redirect??"/login";n(u)}},a=()=>{const t=s();if(t!==null&&r.isAuthenticated){const u=t.redirect??"/";n(u)}},n=t=>{e.push(t)};i.watchEffect(()=>{o(),a()})}exports.useAuthGuard=d;
25 changes: 25 additions & 0 deletions dist/guards.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { watchEffect as i } from "vue";
import { useAuthStore as l } from "./stores.js";
function p(e) {
const r = l(), c = () => e.currentRoute.value.meta.auth ?? null, o = () => e.currentRoute.value.meta.guest ?? null, s = () => {
const t = c();
if (t !== null && !r.isAuthenticated) {
const u = t.redirect ?? "/login";
n(u);
}
}, a = () => {
const t = o();
if (t !== null && r.isAuthenticated) {
const u = t.redirect ?? "/";
n(u);
}
}, n = (t) => {
e.push(t);
};
i(() => {
s(), a();
});
}
export {
p as useAuthGuard
};
3 changes: 3 additions & 0 deletions dist/guards/authGuard.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Router } from 'vue-router';

export declare function useAuthGuard(router: Router): void;
3 changes: 2 additions & 1 deletion dist/interfaces/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LoginForm } from './loginForm';
import { UserInterface } from './userInterface';
import { AlertInterface, AlertStyle } from './alerts';
import { RouteGuard } from './routes';

export { type LoginForm, type UserInterface, type AlertInterface, AlertStyle };
export { type LoginForm, type UserInterface, type AlertInterface, type RouteGuard, AlertStyle };
12 changes: 12 additions & 0 deletions dist/interfaces/routes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

export interface RouteGuard {
redirect: string | {
name: string;
};
}
declare module 'vue-router' {
interface RouteMeta {
auth?: RouteGuard;
guest?: RouteGuard;
}
}
1 change: 1 addition & 0 deletions dist/plugin.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const u=require("./stores.cjs"),r=require("./guards.cjs"),s={install:(o,e)=>{u.useAuthStore().check();const{router:t}=e;r.useAuthGuard(t)}};exports.default=s;
9 changes: 9 additions & 0 deletions dist/plugin.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { App } from 'vue';
import { Router } from 'vue-router';

declare const _default: {
install: (app: App, options: {
router: Router;
}) => void;
};
export default _default;
12 changes: 12 additions & 0 deletions dist/plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useAuthStore as u } from "./stores.js";
import { useAuthGuard as r } from "./guards.js";
const c = {
install: (a, t) => {
u().check();
const { router: o } = t;
r(o);
}
};
export {
c as default
};
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
"url": "https://github.com/userfrosting/UserFrosting/issues"
},
"exports": {
".": {
"import": "./dist/plugin.js",
"require": "./dist/plugin.cjs",
"types": "./dist/plugin.d.ts"
},
"./types": {
"import": "./dist/types.js",
"require": "./dist/types.cjs",
Expand All @@ -34,6 +39,11 @@
"require": "./dist/stores.cjs",
"types": "./dist/stores/auth.d.ts"
},
"./guards": {
"import": "./dist/guards.js",
"require": "./dist/guards.cjs",
"types": "./dist/guards/authGuard.d.ts"
},
"./*": "./app/assets/*"
},
"files": [
Expand Down
4 changes: 3 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default defineConfig({
outDir: './dist',
lib: {
entry: {
types: 'app/assets/interfaces/index.ts',
plugin: 'app/assets/plugin.ts',
types: 'app/assets/interfaces/index.ts',
guards: 'app/assets/guards/authGuard.ts',
stores: 'app/assets/stores/auth.ts'
}
},
Expand Down

0 comments on commit defbcdf

Please sign in to comment.