Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow set token with a function and fix a error #8

Merged
merged 3 commits into from
Mar 15, 2019
Merged

Allow set token with a function and fix a error #8

merged 3 commits into from
Mar 15, 2019

Conversation

mdbetancourt
Copy link
Contributor

@mdbetancourt mdbetancourt commented Nov 14, 2018

This "pull requests" fix an uncaught exception that occurs in a network error, because the response does not have the "status" property. It also allows you to configure the "access token" as a callback function and as a string of course

package.json Outdated Show resolved Hide resolved
@vesper8
Copy link

vesper8 commented Jan 13, 2019

@Akhail thanks for this! I need this badly!

I have a question though, how do I call this setAuthentication after having already initialized the plugin?

I initialize it as part of my store.js

VuexORM.use(VuexORMAxios, {
  database,
  http: {
    baseURL: (process.env.NODE_ENV === 'development' && !process.env.CORDOVA_PLATFORM)
      ? 'https://api.mydomain.dev' : 'https://api.mydomain.com',
    url: '/',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
  },
});

then later when my user actually logs in, I want to set the authorization token at that point. How can I access the plugin instance at that point?

And also.. how can I achieve this for now since this PR has not been accepted yet.

Thanks a lot!!

@mdbetancourt
Copy link
Contributor Author

mdbetancourt commented Jan 13, 2019

@vesper8
access token is a callback function only need return the token

VuexORM.use(VuexORMAxios, {
  database,
  http: {
    baseURL: (process.env.NODE_ENV === 'development' && !process.env.CORDOVA_PLATFORM)
      ? 'https://api.mydomain.dev' : 'https://api.mydomain.com',
    url: '/',
    access_token() {
       // your logic
      if(login) {
          return token;
      }
      return "";
   },
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
  },
});

without this.. i don't know, because this package uses a separate instance of axios, so it is impossible to access it.

@vesper8
Copy link

vesper8 commented Jan 14, 2019

thanks for the quick reply @Akhail

I just tried out your fork and unfortunately it doesn't work out for me.. I also don't understand how this could work for you. How can you access the user's token from within that callback? You can't even access 'this'.. let alone the $auth global that has access to the token

I use @websanova/vue-auth to handle my jwt login/access and I tried doing this:

    access_token: () => {
      if (this.$auth.check()) {
        return this.$auth.token();
      }
      return '';
    },

It tells me that $auth is not accessible. this is not accessible either.

In your mock example you refer to a variable "login" but how could you even have access to that in the scope of your store.js ?

@vesper8
Copy link

vesper8 commented Jan 14, 2019

Oh my.. I kept reading and trying things and I guess I got lucky.. this works!

    access_token() {
      if (Vue.auth.check()) {
        return Vue.auth.token();
      }
      return '';
    },

I also discovered this awesome package here https://github.com/ds300/patch-package which lets you create a patch super easily so you don't have to rely on forks when fixing/adding features such as in this case

Anyway your method works awesome and is much needed @Akhail ! Thanks a lot for helping put me on the right patch and for this PR!

@kiaking @KaniRobinson please please merge this PR!!

@fowkswe
Copy link

fowkswe commented Feb 16, 2019

For those wondering how to get the token from something like nuxt auth-module, I'm getting it from localStorage:

    access_token() {
      return window.localStorage['auth._token.local'].substr('Bearer '.length);
    }

Thanks to @go4cas in the Slack channel for this suggestion.

@twnaing
Copy link

twnaing commented Feb 19, 2019

@fowkswe, I couldn't access window.

@vesper8 , I am using this with nuxtjs and Vue.auth also doesn't work.

@Akhail , how can I check nuxtjs auth-module token?

From documentation, I should be able to access it using

localStorage.getItem("token") 
// or
$store.state.auth.getToken('local')

@twnaing
Copy link

twnaing commented Feb 20, 2019

For Nuxtjs, I have to create plugin and use that plugin (in nuxt.config.js) with mode: client and I can access auth using @fowkswe code.

Copy link
Member

@KaniRobinson KaniRobinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good Job Dude, Sorry for the lack of response

@KaniRobinson KaniRobinson merged commit 6e91564 into vuex-orm:master Mar 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants