tarlepp / angular-sailsjs-boilerplate

'Boilerplate' for AngularJS + Sails.js
MIT License
307 stars 87 forks source link

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

Open Hanifb opened 8 years ago

Hanifb commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

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