|
| 1 | +import * as Vue from 'vue'; |
| 2 | +import { Router } from 'vue-router'; |
| 3 | +import { isArrayIntersecting } from './helpers'; |
| 4 | +import { |
| 5 | + GuardConfig, |
| 6 | + GuardConfigOptions, |
| 7 | + GuardConfigRedirect, |
| 8 | + GuardConfigStore, |
| 9 | + GuardConfigToken, |
| 10 | +} from './types'; |
| 11 | +import Storage from './storage'; |
| 12 | + |
| 13 | +const defaultOptions = { |
| 14 | + permissionKey: 'permission', |
| 15 | +}; |
| 16 | + |
| 17 | +let guardStore: GuardConfigStore; |
| 18 | + |
| 19 | +export default class Guard { |
| 20 | + public $store: GuardConfigStore; |
| 21 | + static router: Router; |
| 22 | + static tokenConfig: GuardConfigToken; |
| 23 | + static redirect: GuardConfigRedirect; |
| 24 | + static options: GuardConfigOptions; |
| 25 | + static storage: Storage; |
| 26 | + static packageKey: string; |
| 27 | + |
| 28 | + constructor(config: GuardConfig, packageKey: string) { |
| 29 | + if (!config.token?.name) { |
| 30 | + throw Error('@thisdot/vue-route-guard: Token name is not set'); |
| 31 | + } |
| 32 | + |
| 33 | + Guard.router = config.router; |
| 34 | + Guard.tokenConfig = config.token; |
| 35 | + Guard.redirect = config.redirect || {}; |
| 36 | + Guard.options = Object.assign({}, defaultOptions, config.options); |
| 37 | + Guard.storage = new Storage(config.token.storage); |
| 38 | + Guard.packageKey = packageKey; |
| 39 | + |
| 40 | + guardStore = Vue.reactive({ |
| 41 | + state: { |
| 42 | + loading: false, |
| 43 | + authentication: null, |
| 44 | + isAuthenticated: false, |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + this.$store = guardStore; |
| 49 | + |
| 50 | + Guard.initializeAuthentication(); |
| 51 | + |
| 52 | + Guard.router.beforeEach((guard) => { |
| 53 | + // check if route requires authentication and authentication exists |
| 54 | + if (guard.meta.requiresAuth && !guardStore.state.isAuthenticated) { |
| 55 | + return Guard.redirect.noAuthentication || false; |
| 56 | + } |
| 57 | + |
| 58 | + // check if route requires authentication and authentication access exists |
| 59 | + if (guard.meta.requiresAuth && !this.hasAuthenticationAccess(guard.meta.access)) { |
| 60 | + return Guard.redirect.noPermission || Guard.redirect.noAuthentication || false; |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + public install(app: Vue.App) { |
| 68 | + app.config.globalProperties[`$${Guard.packageKey}`] = this; |
| 69 | + |
| 70 | + app.provide(Guard.packageKey, this); |
| 71 | + } |
| 72 | + |
| 73 | + public token() { |
| 74 | + return Guard.storage.get(Guard.tokenConfig.name); |
| 75 | + } |
| 76 | + |
| 77 | + public isAuthenticated() { |
| 78 | + return guardStore.state.isAuthenticated; |
| 79 | + } |
| 80 | + |
| 81 | + public hasAuthenticationAccess(permission: string[] = []) { |
| 82 | + const permissionKey = Guard.options.permissionKey; |
| 83 | + if ( |
| 84 | + guardStore.state.isAuthenticated === true && |
| 85 | + (permission.length === 0 || |
| 86 | + (permission.length > 0 && |
| 87 | + permissionKey && |
| 88 | + guardStore.state.authentication && |
| 89 | + guardStore.state.authentication[permissionKey] && |
| 90 | + isArrayIntersecting( |
| 91 | + permission, |
| 92 | + guardStore.state.authentication[permissionKey] as string[] |
| 93 | + ))) |
| 94 | + ) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + public async clearAuthentication() { |
| 102 | + Guard.clearAuthenticationState(); |
| 103 | + Guard.clearToken(); |
| 104 | + |
| 105 | + const redirectRoute = Guard.redirect.clearAuthentication || Guard.redirect.noAuthentication; |
| 106 | + if (redirectRoute) { |
| 107 | + Guard.router.push(redirectRoute); |
| 108 | + } |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + public async refreshAuthentication() { |
| 113 | + await Guard.initializeAuthentication(); |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + public async setToken({ token }: { token: string }) { |
| 118 | + Guard.storage.set(Guard.tokenConfig.name, token); |
| 119 | + await Guard.initializeAuthentication(); |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + static isTokenAvailable() { |
| 124 | + return !!Guard.storage.get(Guard.tokenConfig.name); |
| 125 | + } |
| 126 | + |
| 127 | + static clearToken() { |
| 128 | + Guard.storage.remove(Guard.tokenConfig.name); |
| 129 | + } |
| 130 | + |
| 131 | + static setAuthenticationState(authentication: { [key: string]: unknown }) { |
| 132 | + guardStore.state.loading = false; |
| 133 | + guardStore.state.authentication = authentication; |
| 134 | + guardStore.state.isAuthenticated = true; |
| 135 | + } |
| 136 | + |
| 137 | + static clearAuthenticationState() { |
| 138 | + guardStore.state.loading = false; |
| 139 | + guardStore.state.authentication = null; |
| 140 | + guardStore.state.isAuthenticated = false; |
| 141 | + } |
| 142 | + |
| 143 | + static async initializeAuthentication() { |
| 144 | + // check if token exists then set authentication |
| 145 | + if (Guard.isTokenAvailable() && typeof Guard.options.fetchAuthentication === 'function') { |
| 146 | + guardStore.state.loading = true; |
| 147 | + const auntenticationData = await Guard.options.fetchAuthentication(); |
| 148 | + Guard.setAuthenticationState(auntenticationData); |
| 149 | + } |
| 150 | + return true; |
| 151 | + } |
| 152 | +} |
0 commit comments