Feathers: Combining signup and auth

Created on 27 Dec 2016  ·  3Comments  ·  Source: feathersjs/feathers

I feel like there isn't very much explanation on the 'magic' behind authentication. I would like to combine my signup action to return a JWT token. Could anyone help me?

I think it would be something like:

  • middleware/signup.js
    ```js
    const newUser = {
    user: req.body.user,
    pass: req.body.pass
    }
    app.service('users').create(newUser)
    .then(user => app.authenticate(user)) // authenticate not defined though
    .then(authenticatedUser => res.json(authenticatedUser));
    ````

Can I get any help or explanation of how to use auth outside of the magical realm? Thanks!

Edit: I made this an issue because I feel like there should be more documentation on manual authentication. This is more of a demonstration, I already know there is an issue on this, such as #467

Most helpful comment

The generated app will still use old authentication though in which case it'll have to be

const { email, password } = req.body;

app.service('users').create({ email, password })
  .then(user => app.service('auth/local').create({ email, password }))
  .then(authenticatedUser => res.json(authenticatedUser));

All 3 comments

@rstegg it looks like you're pretty close. If app.authenticate() is undefined, you probably are using the signup middleware before the authentication plugin. Make sure the auth plugin gets used, first.

const newUser = { 
  user: req.body.user,
  pass: req.body.pass
}
app.service('users').create(newUser)
  .then(user => app.authenticate(user)) // authenticate should be defined if you register middleware in the correct order.
  .then(jwt => res.json(jwt));

The generated app will still use old authentication though in which case it'll have to be

const { email, password } = req.body;

app.service('users').create({ email, password })
  .then(user => app.service('auth/local').create({ email, password }))
  .then(authenticatedUser => res.json(authenticatedUser));

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings