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

Creating a new (local) user and passport. #100

Open
Hanifb opened this issue Oct 25, 2015 · 2 comments
Open

Creating a new (local) user and passport. #100

Hanifb opened this issue Oct 25, 2015 · 2 comments

Comments

@Hanifb
Copy link

Hanifb commented Oct 25, 2015

So, i started to do a little digging while trying to get a grip about Facebook integration and found out that one main issue is that the boilerplate does not have any (sane) logic for creating a local user.

This is the intended logic for creating a local user, any user creation has to be made by an authenticated user with two steps.

  1. One creating the user with a POST to /User
  2. Do an /auth/local/connect request with a password

Number 1 is pretty straightforward, number 2 gets tricky, because the Passport.protocol.local.connect requires a User model to be attached to the request object. This is not done anywhere.

One way of creating this logic is adding a custom register method in AuthController, attach the newly created user to the request object and call the Passport service. But the Passport Service looks for params like :provider and :action, overriding these params with hardcode seems hacky.

So this is what i managed too cook up. Opinions? Worth making a PL on?

//Add this in the beginning of AuthController.js 
var actionUtil = require('../../node_modules/sails/lib/hooks/blueprints/actionUtil');

register: function(req, res){

    var data = actionUtil.parseValues(req);

    User.create(data).exec(function(err, user){
        if(err) return res.negotiate(err);
        if(!user) return res.notFound("User not found"); //This should never happen

        req.user = user;
        req.params.action = "connect";
        sails.services['passport'].callback(req,res,function(err, user){
            if(err) return res.negotiate(err);
            res.ok(user);
        });
    });
},

Dont forget to add this in routes.js

'POST /auth/local/register': 'AuthController.register',
@bdupreez
Copy link

Hi,
Thanks for this, I got a the register user functionality working with this.

Just a little thing I did do different, instead on User.create(data)... I specified it as:
sails.models.user.create(data).

@jjgonver
Copy link

I don't get to do at least user registration with the local strategy. Can anyone guide me? Someone who has completed can share their code? Thanks

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

No branches or pull requests

3 participants