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

v6.0.0: How can I return the user object along with the token ? #131

Closed
djhi opened this Issue Mar 26, 2016 · 14 comments

Comments

Projects
None yet
4 participants
@djhi
Copy link

djhi commented Mar 26, 2016

Hi! I'm trying to have a simple local authentication which is working fine. I do receive the jwt token when calling the app.authenticate method using rest from feathers-rest/client.

However, I'd like to retrieve the user data (id, email, etc.) and I can't figure out how. I understand that the API has changed in v0.5 and that app.get('user') is not populated anymore. Is it possible to configure authentication in order to get that feature back ? If not, what is the preferred method to achieve this ?

Here is the code I'm using:

/* globals ADMIN_API_URL */
import feathers from 'feathers/client';
import hooks from 'feathers-hooks';
import rest from 'feathers-rest/client';
import authentication from 'feathers-authentication/client';
import fetch from 'isomorphic-fetch';

const app = feathers()
    .configure(rest(ADMIN_API_URL).fetch(fetch))
    .configure(hooks())
    .configure(authentication({ storage: window.localStorage }));

function redirect() {
    window.location = '/admin';
}

// check for user already logged in
if (window.localStorage.getItem('token')) {
    redirect();
}

document.getElementById('loginForm').addEventListener('submit', (event) => {
    event.preventDefault(); // stop form from submitting

    app.authenticate({
        type: 'local',
        email: document.getElementById('inputEmail').value,
        password: document.getElementById('inputPassword').value,
    }).then(({ token }) => {
        window.localStorage.setItem('token', token);
        const userService = app.service('users');
        return userService.get();
    }).then(user => {
        console.log({ user });
        window.localStorage.setItem('id', user.id);
        window.localStorage.setItem('email', user.email);
        redirect();
    }).catch(err => {
        console.error({ err });
        document.getElementById('loginError').style.display = 'block';
        document.getElementById('submitButton').disabled = false;
    });
});

Thanks !

@djhi

This comment has been minimized.

Copy link
Author

djhi commented Mar 26, 2016

If possible, Id'l like to avoid the call to userService

@ekryski

This comment has been minimized.

Copy link
Member

ekryski commented Mar 26, 2016

We released v0.6 a couple days ago and haven't documented the breaking changes. I have a couple more changes coming in a v0.7 release later today and will update the docs then. You will get the current user in the response when you authenticate, and it will be available with app.get('user'). 😄

This actually already happens in v0.6.0. You can see the release notes in the README if you want to upgrade or just wait a day or so and we'll have the docs and examples updated.

@ekryski ekryski modified the milestone: 0.7 Mar 26, 2016

@djhi

This comment has been minimized.

Copy link
Author

djhi commented Mar 26, 2016

Thanks for answering so quickly :) That was my first attempt but unfortunately, app.get('user') returns undefined

@djhi

This comment has been minimized.

Copy link
Author

djhi commented Mar 26, 2016

For information, I used the yeoman generator to initialize my app

@djhi

This comment has been minimized.

Copy link
Author

djhi commented Mar 26, 2016

Found it! The auth configuration must have an idField property otherwise the user cannot be retrieved. This might be important to mention in the docs :)

@djhi djhi closed this Mar 26, 2016

@ekryski

This comment has been minimized.

Copy link
Member

ekryski commented Mar 26, 2016

It depends on which DB you are using. By default it sets _id as the idField.

@djhi

This comment has been minimized.

Copy link
Author

djhi commented Mar 26, 2016

I'm using postgres. Thanks and great lib btw !

@ekryski

This comment has been minimized.

Copy link
Member

ekryski commented Mar 26, 2016

Yes so for PG you'd need to set that manually. I'm going to create an issue for the generator to do that for you though.

@djhi

This comment has been minimized.

Copy link
Author

djhi commented Mar 26, 2016

Great! However I really think this should be documented. In my case, I won't be using the generator anymore as it does not play well with my folder structure: marmelab boilerplate with feathers replacing the Koa API.

@longntp

This comment has been minimized.

Copy link

longntp commented Mar 3, 2017

Do you have any options to get current user without using feathers client ?

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Mar 3, 2017

Decode the JWT. Take the user id from the resulting payload and run a GET /users/<userid>.

@longntp

This comment has been minimized.

Copy link

longntp commented Mar 3, 2017

@daffl : Can I use successRidirect on "auth" property in config file. Redirect to another path (middleware) and attach user object along with the response back to client ? I think making another request to server is not a good idea :(.

@daffl

This comment has been minimized.

Copy link
Member

daffl commented Mar 3, 2017

Yes, you can also add an app.service('authentication').after({ create(hook) {} }) hook that adds the user information to hook.result.

@longntp

This comment has been minimized.

Copy link

longntp commented Mar 3, 2017

I will try it and let me know if any further issue. Many many thanks for your instant support :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.