-
Notifications
You must be signed in to change notification settings - Fork 8
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
8 changed files
with
174 additions
and
34 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import Vue from 'vue' | ||
import VAccess, { init, RouteWithAbility } from 'v-access' | ||
import { Plugin } from 'vuex' | ||
import { RootState } from '@/store' | ||
import VueRouter from 'vue-router' | ||
|
||
Vue.use(VAccess) | ||
|
||
type CreateAbilityPlugin = ( | ||
mutationType: string, | ||
instance: Vue | VueRouter, | ||
redirect: string, | ||
routes: RouteWithAbility[] | ||
) => Plugin<RootState> | ||
|
||
export const createAbilityPlugin: CreateAbilityPlugin = function( | ||
mutationType, // when should we call init | ||
vm, | ||
redirect, | ||
routes | ||
) { | ||
return store => { | ||
store.subscribe(mutation => { | ||
if (mutation.type === mutationType) { | ||
init({ | ||
vm, | ||
abilities: mutation.payload, | ||
redirect, | ||
routes | ||
}) | ||
return | ||
} | ||
}) | ||
} | ||
} |
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 @@ | ||
export const FORBIDDEN_ROUTE = '/forbidden' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Module } from 'vuex' | ||
import { RootState } from '..' | ||
import { Ability } from 'v-access' | ||
import { errorLog } from '@/shared/utils' | ||
|
||
interface UserState { | ||
token: string | ||
abilities: Ability[] | ||
} | ||
|
||
export const types = { | ||
setToken: 'setToken', | ||
setUserAbilities: 'setUserAbilities' | ||
} | ||
|
||
const user: Module<UserState, RootState> = { | ||
namespaced: true, | ||
|
||
state: { | ||
token: '', | ||
abilities: [] | ||
}, | ||
|
||
getters: { | ||
hasLogin({ token }) { | ||
return Boolean(token) | ||
} | ||
}, | ||
|
||
mutations: { | ||
setToken(state, token) { | ||
state.token = token | ||
}, | ||
setUserAbilities(state, abilities) { | ||
state.abilities = abilities | ||
} | ||
}, | ||
|
||
actions: { | ||
async fetchUserInfo( | ||
{ commit }, | ||
{ username, password }: Record<string, string> | ||
) { | ||
try { | ||
const { token } = await Promise.resolve({ | ||
token: | ||
Math.random() | ||
.toString(16) | ||
.slice(2) + | ||
username + | ||
password | ||
}) | ||
commit('setToken', token) | ||
} catch (error) { | ||
errorLog(error) | ||
} | ||
}, | ||
async fetchUserAbilities({ commit }) { | ||
try { | ||
const { abilities } = await Promise.resolve({ | ||
abilities: Array(20) | ||
.fill(null) | ||
.map((_, index) => ({ | ||
id: `ability.simulator.${index}`, | ||
name: `ability.simulator.${index}`, | ||
createAt: Date.now() + index | ||
})) | ||
}) | ||
commit( | ||
'setUserAbilities', | ||
abilities.map(ability => ability.id) | ||
) | ||
} catch (error) { | ||
errorLog(error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
export default user |
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