question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get user and token from login response

See original GitHub issue

I’m very confused which data vue-auth expects from which API calls. I currently login like this:

this.$auth.login({
  body: this.data.body,
  redirect: {name: redirect ? redirect.from.name : 'AdminHome'},
  fetchUser: true,
  success () {
    this.success = true
  }, error (res) {
    console.log('error ' + res.data)
    this.error = res.data
  }
})

This API responds with this:

{
  "success": true,
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjUyZGJhNjczLWNjYzMtNDJkYi04NzQ1LWQ5ZTE5ZjBkYTRmMyIsIm5hbWUiOiJQZXJyeSIsInN1cm5hbWUiOiJOYXNlY2siLCJlbWFpbCI6IjE4bmFzZWNrcEBzbXRleGFzLm9yZyIsImRpc2FibGVkIjpmYWxzZSwicm9sZSI6MiwiZGV2aWNlSWQiOiI3YzU2NTUzYS1lZDI3LTQ1OWEtOGIxOC04MzA2N2MwMWU0MjUiLCJpYXQiOjE1MDA2NjA2NTcsImV4cCI6MTUwMTI2NTQ1N30.LjsW4fDJAUvZLWjAXDwAkikluwxiwEoxaLpG14SRqnE",
  "data": {
    "id": "52dba673-ccc3-42db-8745-d9e19f0da4f3",
    "name": "ExFirstName",
    "surname": "ExLastName",
    "email": "exmaple@example.com",
    "disabled": false,
    "role": 2,
    "deviceId": "7c56553a-ed27-459a-8b18-83067c01e425"
  }
}

But then the library makes another call to api/user, but it doesn’t include the token in that request, so my API doesn’t know what to do (well, really it just gives a 400 as it’s supposed to).

Also, is it supposed to decode the JWT to get the user data, or am I supposed to hand it to it in an object like this?

Thank you for making this awesome library and saving me a lot of time trying to implement a full auth system myself.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:39 (18 by maintainers)

github_iconTop GitHub Comments

0reactions
mochafizIHcommented, Apr 26, 2019

Hi @websanova or anyone, I still confused on how to set response data from login response.

Actually, I have implemented the custom authentication token, but I stuck on how to pass the login response and use it as “rolesVar”

my login response is:

{
"success": true,
    "data": {
        "user-id": 60,
        "email-user": "mochaxxx@xxx.com",
        "mobile-phone": "+62816xxx",
        "roles": [
            {
                "id": 24,
                "name": "member"
            }
        ],
        "access-token": "f9f18fdc-8181-492d-89e9-7580b3fc64d4",
        "created-at": "2019-03-21T03:19:34"
    },
    "message": null
}

on main.js:

Vue.use(VueAuth, {
  auth: require('./plugin/modules/customAuth.js'),
  http: require('@websanova/vue-auth/drivers/http/axios.1.x.js'),
  router: require('@websanova/vue-auth/drivers/router/vue-router.2.x.js'),
  authRedirect: {path: '/pages/login'},
  refreshData: true,
  rememberMe: true,
  tokenStore: ['cookie'],
  parseUserData: function (data) {
    console.log("ParseUserData");
    console.log(data);
    return data;
  },
  rolesVar: 'roles.name'
})

And also, when I try to print the data on parseUserData, its return empty object {}

on customAuth.js:

module.exports = {
  request: function (req, token) {
    this.options.http._setHeaders.call(this, req, {'access-token': token});
    // req.headers.set('access-token', token);
  },
  response: function (res) {
    let token = (res.data.data || {})['access-token'];
    return token;
  }
};

on login script:

this.$auth.login({
          data: t.userLogin, // Axios
          rememberMe: false,
          fetchUser: false,
          url: process.env.UMS_URL + '/v1/users/login',
          success(res) {
            t.isLoading = false;
          },
          error(err) {
            t.notifyVue('top', 'center', err.message);
            t.loading = false
          }
        })

please help me with this. I have been stuck with this for a week.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get Access Tokens - Auth0
Learn how to request Access Tokens using the Authorize endpoint when authenticating users and include the target audience and scope of access requested...
Read more >
Retrieve an Access Token and Refresh Token
After the user has completed entering their code, the Device Token Response you receive from Login with Amazon will contain the following parameters: ......
Read more >
Get access on behalf of a user - Microsoft Graph
The first step to getting an access token for many OpenID Connect (OIDC) and OAuth 2.0 flows is to redirect the user to...
Read more >
How get API response using username and password?
You likely need an auth interceptor, and need to utilize a refresh token. It's a lot to discover on a whim, but effectively...
Read more >
How to get an access token with Authorization Code Grant
Before you can make any API calls using Authorization Code Grant, you must get your user's consent for your app to act for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found