Skip to content

Instantly share code, notes, and snippets.

@erandirjunior
Created March 12, 2019 11:52
Show Gist options
  • Save erandirjunior/54f8783785fa558ffc0ee651a4b0164b to your computer and use it in GitHub Desktop.
Save erandirjunior/54f8783785fa558ffc0ee651a4b0164b to your computer and use it in GitHub Desktop.
Autorização
import Permission from '../classes/Permission'
export default ({router, store, Vue}) => {
router.beforeEach((to, from, next) => {
const authorization = to.meta.authorization
const token = localStorage.getItem('token')
const permission = new Permission(token)
if (authorization && !permission.hasPermission(authorization)) {
next('/')
}
next(next)
})
}
import axios from 'axios'
export default class Permission {
constructor (idUser) {
this.$axios = axios
this.permissions = []
this.getPermission(idUser)
}
async getPermission () {
const token = localStorage.getItem('token')
await this.$axios.post(`${process.env.API}/permissions`, { token })
.then(resp => resp.data)
.then(resp => resp.data)
.then(resp => {
this.permissions = resp
})
}
hasPermission (number) {
return this.permissions[number]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment