websanova / vue-auth

A simple light-weight authentication library for Vue.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to extend Auth using v3 createAuth

darkons opened this issue · comments

I trying to implement a "super roles" validation. It is that when the authenticated user has one of these super roles, all auth checks must returns true.

I found what I needed in #648 issue but I'm using Vue Auth v3 createAuth method so I don't know where I should extend auth "check" or "_isAccess" method to validate "super roles".

Thank you very much in advance.
Best

Ah yea, I think the answer is right in the thread you linked, just need to extend or use route auth or check method.

Thank you for your response @websanova but I'm using vue-auth/src/v3 so I can't extend auth features like thread #648

My implementation:

// plugins/auth.js

import { createAuth } from '@websanova/vue-auth/src/v3'
import axios from '@/plugins/axios'
import router from '@/router'
import driverHttpAxios from '@websanova/vue-auth/src/drivers/http/axios.1.x'
import driverRouterVueRouter from '@websanova/vue-auth/src/drivers/router/vue-router.2.x'

export default (app) => {
  app.use(
    createAuth({
      .........
    })
  )
}
// composables/useAuthComp.js

import { useAuth } from '@websanova/vue-auth/src/v3'

export default function useAuthComp() {
  const auth = useAuth()

  const fetch = (data) => {
    return auth.fetch(data)
  }

  const refresh = (data) => {
    return auth.refresh(data)
  }

  const login = (data) => {
    data = data || {}

    return new Promise((resolve, reject) => {
      auth
        .login({
          data,
        })
        .then((res) => {
          localStorage.setItem('timezone', res.data.data.timezone)
          resolve(res)
        }, reject)
    })
  }

  const logout = () => {
    return auth.logout().then(() => {
      auth.token(auth.options.refreshTokenKey, null)
    })
  }

  const user = () => {
    return auth.user()
  }

  const ready = () => {
    return auth.ready()
  }

  const check = (data) => {
    return auth.check(data)
  }

  return { fetch, refresh, login, logout, user, ready, check }
}

Where do you suppose I should extend the check method?

I really appreciate your help

You can just take a look at:

https://github.com/websanova/vue-auth/blob/master/src/v3.js

createAuth is a simple function that just returns new instance of the Auth object. So you can likely just override the createAuth function after import.

Just off the top of my head, one possible way could be something like below.

import { createAuth } from '@websanova/vue-auth/src/v3'

var myAuth = function () {
   var auth = createAuth();

   auth.somefunc = function() {}

   return auth
}

Though, I think what you're asking is more a generic Vue/JS question that would apply to any (in this case) Vue plugin so maybe ask around that way as well.

Thank you for your help @websanova

I did the following:

import { createAuth } from '@websanova/vue-auth/src/v3'

var auth = createAuth()

var temp = auth.check

auth.check = (role, key) => {
  if (auth.$vm.state.data.roles.includes('superadmin')) {
    return true
  }

  return temp(role, key)
}

export default (app) => {
  app.use(auth)
}

It's working fine when using auth.check() in my Vue templates but is not working for routes meta. What am I missing?
Thank you so much for your patience