-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters