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

Binding doesn't work on some models possibly with HTTP POST #27

Open
spunkedy opened this issue Jan 9, 2015 · 0 comments
Open

Binding doesn't work on some models possibly with HTTP POST #27

spunkedy opened this issue Jan 9, 2015 · 0 comments

Comments

@spunkedy
Copy link

spunkedy commented Jan 9, 2015

This might be an issue with Sails, or my configuration.

However in my controller I am binding:

$sailsBind.bind("user", $scope)
$sailsBind.bind("task", $scope)

When I am using the register function on the controller updates are not being sent. I can see socket messages being pushed to the task object.

the register function is being called via HTTP POST instead of a socket call.

Task works, user does not.

User model:

var User = {
  // Enforce model schema in the case of schemaless databases
  schema: true,

  attributes: {
    groups: {
        collection: 'groups',
        via: 'members',
    required: true
    },
    username  : { type: 'string', unique: true, required: true },
  email     : { type: 'email',  unique: true, required: true },

  passports : { collection: 'Passport', via: 'user' }
    // Groups

  }
};

module.exports = User;

User controller:

/**
 * UserController
 *
 * @description :: Server-side logic for managing Users
 * @help        :: See http://links.sailsjs.org/docs/controllers
 */


module.exports = {

  session: function(req, res) {

    res.json({
      success: true
    });
  },


  //expose a method for angular to be able to see if we are logged in
  isLoggedIn: function(req,res){

    //TODO: make sure that only the clients with the right session get the broadcast
    //sails.sockets.broadcast("authNotification" , "authorized",{socketId: req.socket.id, user: req.user} );    

    res.json({sessionID: req.session.sessionID, roles: req.session.roles, user: req.user} );
    //res.json(req.isAuthenticated() ? {sessionID: req.sessionID, user: req.user} : 0);
  },
  //expose a method for angular to be able to see if we are logged in
  sayHi: function(req,res){
    sails.sockets.broadcast("authNotification" , "message", {message: "onLogin"} );  
    sails.sockets.broadcast("authNotification" , "message", {message: "hello3"} );    
    res.json({result:"sent"});
  },
  sendHello: function(req,res) {
    //req.sessionID
    sails.io.sockets.in('asdf').emit("eventName", {data:true});
    //sails.sockets.broadcast.to("asdf").emit('loggedOut', {loggedOff: 'true'});

    res.json({
        sessionid: req.sessionID,
        message: "sent"
    })

  },
  register: function(req,res){

    //associate group
    Groups.findOrCreate({
      groupName: 'user'
    }, {
      groupName: 'user',
      description: 'Generic user'
    }).exec(function createFindCB(err, userGroup) {
      //Check password for equality
      var password = req.param('password');
      var confirmation = req.param('confirmation');
      //TODO: enfore better passsword enforcement
      if(!password){
        res.json({err: { message: "You must enter a password" }});
      } else if (password == confirmation){
        var userObj = {
          username: req.param('username'),
          email: req.param('email'),
          groups: [userGroup.id]
        };

        //Create user
        User.create(userObj, function userCreated(err, user) {
          if (err) {
            res.json({
              err: err
            });
          } else {
            //Associate passport ( password )
            //TODO: include other authentication mechanisms
            Passport.create({
              protocol: 'local',
              password: password,
              user: user.id
            }, function (err, passport) {
              if (err){
                User.destroy(user.id, function deleteCB(deleteErr){
                  sails.log(deleteErr);
                });
                res.json({err:err});
                console.log(err);              
              } else {
                res.json({success: true});
              }
            });

          }
        });
      } else {
        res.json({err: { message: "Passwords do not match" }});
      }

    });

  }

};
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

1 participant