Problem Authentication and Sequelize promise

70 views
Skip to first unread message

Javier Girón

unread,
Nov 16, 2015, 4:08:29 PM11/16/15
to Crossbar
I have a problem on authentication WAMP-CRA with promise in my method
```javascript
function authenticate (args)
{
var realm = args[0];
var authid = args[1];
var details = args[2];
var auth = {};

models.Users.findOne({
where: {username: authid}
}).then(function (user) {
if (user !== null) {

user.getAuthItems().then(function (items)
{
var role = null;

for (i in items) {
role = items[i].name;
}

if (role !== null) {
auth = {
'secret': user.password,
'role': role
}

} else {
throw "not role assigned";
}

});
} else {
throw "no such user";
}
});

console.error(auth);
return auth;
}
```

Then while is trying to authenticate the code run without end the promise.
if a dum auth var on my code I get " {}" because authentication follow up while promise is not getting back.

Tobias Oberstein

unread,
Nov 16, 2015, 4:16:19 PM11/16/15
to cross...@googlegroups.com
Am 16.11.2015 um 22:08 schrieb Javier Girón:
> I have a problem on authentication WAMP-CRA with promise in my method
> ```javascript
> function authenticate (args)
> {
> var realm = args[0];
> var authid = args[1];
> var details = args[2];
> var auth = {};
>
> models.Users.findOne({
> where: {username: authid}
> }).then(function (user) {

Presuming that .then here returns a promise (models.Users.findOne runs
async), you need to return that promise.

Cheers,
/Tobias

Javier Girón

unread,
Nov 16, 2015, 5:03:37 PM11/16/15
to Crossbar


Problem is that, authenticator following the reference ask to return a dictionary.



not a promise.

Im trying to extract password, and role from a database, followin this example

https://github.com/crossbario/crossbarexamples/tree/master/authenticate/wampcradynamic/nodejs

Tobias Oberstein

unread,
Nov 16, 2015, 5:28:07 PM11/16/15
to cross...@googlegroups.com
Am 16.11.2015 um 23:03 schrieb Javier Girón:
>
> Problem is that, authenticator following the reference ask to return a
> dictionary.

You can either return a "plain" (non-async) dict, or you can return a
promise that resolves to a dict.

If you want to do anything async inside authenticate(), then you MUST
return a promise. Otherwise your code will just return immediately, and
not work (the issue you have).

What does

models.Users.findOne()
models.Users.findOne().then()

return?

What is "models" anyway? What async lib?

But anyway. Even without proper async lib, using mere callbacks, you can
make it work like so:


function authenticate (args)
{
var d = autobahn.when.defer();

// now do your async DB query thing, and in the callbacks:

// for success
d.resolve({'secret': .., 'role': ...});

// for error
d.reject("no such user");

return d.promise;
}


/Tobias

Javier Girón

unread,
Nov 16, 2015, 6:09:42 PM11/16/15
to Crossbar
wow !
Thank you SO much Tobias !
That works for me, 

function authenticate (args)
{
 
var realm = args[0];
 var authid = args[1];
 var details = args[2];

 var d = autobahn.when.defer();

 models.Users.findOne({
 
where: {username: authid}
 
}).then(function (user) {

 
if (user !== null) {

   user
.getAuthItems().then(function (items)
   
{
   
var role = null;

    for (i in items) {
     
role = items[i].name;
    }

   
if (role !== null) {
     
auth = {
     
'secret': user.password,
      'role': role
     }

     
d.resolve(auth);
    } else {
     
d.reject("no such role");
     throw "not role assigned";
    }

   
});
  } else {

   
d.reject("no such user");
   throw "no such user";
  }
 
});

 return d.promise;
}

Would be great include this tip on the documentation XD

Tobias Oberstein

unread,
Nov 16, 2015, 6:46:50 PM11/16/15
to cross...@googlegroups.com
Javier,

Am 17.11.2015 um 00:09 schrieb Javier Girón:
> wow !
> Thank you SO much Tobias !

np, come again;)

> That works for me,

glad you got it working!

cheers,
/Tobias

PS: you can (should) remove the "throw" lines in below (the d.reject()
is enough

>
> |
>
> functionauthenticate (args)
> {
> varrealm =args[0];
> varauthid =args[1];
> vardetails =args[2];
>
> vard =autobahn.when.defer();
>
> models.Users.findOne({
> where:{username:authid}
> }).then(function(user){
> if(user !==null){
>
> user.getAuthItems().then(function(items)
> {
> varrole =null;
>
> for(i initems){
> role =items[i].name;
> }
>
> if(role !==null){
> --
> You received this message because you are subscribed to the Google
> Groups "Crossbar" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to crossbario+...@googlegroups.com
> <mailto:crossbario+...@googlegroups.com>.
> To post to this group, send email to cross...@googlegroups.com
> <mailto:cross...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/crossbario/c322c722-cdd3-4fa3-b958-feb764856dba%40googlegroups.com
> <https://groups.google.com/d/msgid/crossbario/c322c722-cdd3-4fa3-b958-feb764856dba%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages