@dsbn/vue-auth
Usage
npm i -S @dsbn/vue-auth
import Auth from '@dsbn/vue-auth'
Vue.use(Auth, {
router,
url: 'https://api.dsbn.org/auth/v1',
loginUrl: `/login/app-slug`,
refreshUrl: `/refresh/app-slug`,
unauthorizedRedirect: '/unauthorized',
failedRedirect: '/authorizefailure',
cookie: true,
interceptor: false
})
Vue
vue-auth
exposes a global $auth
object for your vue components. This gives you access to the reactive user
prop along with some methods.
Props
$auth.user
This contains the decoded payload passed by the server along with an authenticating variable to determine if the user is being fetched.
user = {
authenticating: false,
token: 'eyJ...'
name: '',
username: '',
email: '',
..etc
}
Methods
$auth.authenticated()
Returns a promise to resolve the users current authentication status.
$auth.isValid( token )
Checks if the current token is valid. Returns true|false
$auth.isRole( role1, role2, ... )
Checks if user has any of the passed roles. Returns true|false
$auth.isUser( username1, username2, ... )
Checks if user is any of the passed users. Returns true|false
Vue-router
To safeguard your routes and check user authorization before navigating apply { auth: true
in the meta prop of the desired route. You can also check roles and specific users:
{
path: '/restricted-to-users',
meta: { auth: true }
}
Only developers or monkeys
{
path: '/restricted-to-dev-monkeys',
meta: { auth: { role: ['Developer','Monkey'] } }
}
Only linus
{
path: '/restricted-to-dev-monkeys',
meta: { auth: { user: ['linus'] } }
}