Problem Authentication and Sequelize promise

#1

I have a problem on authentication WAMP-CRA with promise in my method


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.

0 Likes

#2

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

Cheers,
/Tobias

···

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) {

0 Likes

#3

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

···

El lunes, 16 de noviembre de 2015, 16:16:19 (UTC-5), Tobias Oberstein escribió:

Am 16.11.2015 um 22:08 schrieb Javier Girón:

I have a problem on authentication WAMP-CRA with promise in my method

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

0 Likes

#4

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

···

Am 16.11.2015 um 23:03 schrieb Javier Girón:

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

0 Likes

#5

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

···

El lunes, 16 de noviembre de 2015, 17:28:07 (UTC-5), Tobias Oberstein escribió:

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

0 Likes

#6

Javier,

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

···

Am 17.11.2015 um 00:09 schrieb Javier Girón:

>

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){
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";
}
});

returnd.promise;
}

>

Would be great include this tip on the documentation XD

El lunes, 16 de noviembre de 2015, 17:28:07 (UTC-5), Tobias Oberstein > escribió:

    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

--
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.

0 Likes