sails-auth icon indicating copy to clipboard operation
sails-auth copied to clipboard

Documentation for local authentication updates and maintaining user in session

Open grantbi opened this issue 10 years ago • 16 comments

I'm fairly new at Sailjs and have a question about sails-auth. Could you provide more information about how to use the authenticated user, and what the external links/routes may be for local authentication? I'm assuming POST requests for all of these:

/auth/local/register
/auth/local/connect
/auth/local/disconnect

it would return to?

/auth/local/callback

When I do a post to /auth/local/register with username and password or email parameters it has a 500 Server Error response. Am I doing this correctly? Sending 500 ("Server Error") response: RangeError: Maximum call stack size exceeded

Authenticating through Google, Facebook works fine (I get a returned JSON user). It returns to /auth/callback. What I'd like to do is return to /user/profile and show more information or something. When I tried to create this in the controller, I'm not getting a req.user or req.session.user or req.locals.user defined. Is there a way to get the authenticated user from a session?

I see there are connect/disconnect options available, are there parameters that need to be defined to hook these up? Or do you just need to have a registered session?

I'm using: sails: ~0.11.0 sails-auth: 1.0.2

grantbi avatar Feb 28 '15 21:02 grantbi

When I do a post to /auth/local/register

Just post to /user as you normally would with any other crud action. That endpoint will create the passport and everything you need.

Sending 500 ("Server Error") response: RangeError: Maximum call stack size exceeded

Someone else saw this error a few days ago but I think it was due to a bad build. Can you update to the latest version and try again?

What I'd like to do is return to /user/profile and show more information or something. When I tried to create this in the controller, I'm not getting a req.user or req.session.user or req.locals.user defined.

If you're trying to access variables from a view, always use res. res.locals.user should definitely be defined in the controller.

The wiki has some more info: https://github.com/tjwebb/sails-auth/wiki. If you're looking for more featureful user access control and permissioning, check out https://www.npmjs.com/package/sails-permissions.

tjwebb avatar Mar 02 '15 18:03 tjwebb

I've got a distributed setup where my sailsjs server is just an api with the sails-auth and sails-permissions modules installed. my front-end is a sails-backbone-client running on another server. I'm able to use my backbone user model in the client and call save to create the user on the sails server. I cannot figure out how to successfully login with my email and password supplied during registration.

bgulotta avatar Mar 02 '15 20:03 bgulotta

Not sure if this helps, but I was able to get the setup working with a hybrid of this example: https://github.com/sails101/even-more-passport

grantbi avatar Mar 02 '15 20:03 grantbi

I cannot figure out how to successfully login with my email and password supplied during registration.

There are three ways you can login:

  1. Use a 3rd party provider by navigating (GET) to /auth/<provider>, e.g. /auth/google. This will create a session and the browser will remain authenticated
  2. Create a form that POSTs to /auth/local. This will also create a session.
  3. Use Basic Auth, e.g. https://github.com/hashware/hashpanel/blob/master/js/index.js#L23-L29

I've got a distributed setup where my sailsjs server is just an api with the sails-auth and sails-permissions modules installed. my front-end is a sails-backbone-client running on another server.

This is my setup as well.

tjwebb avatar Mar 02 '15 20:03 tjwebb

Not sure if this helps, but I was able to get the setup working with a hybrid of this example:

My intention is for sails-auth to be a complete authentication solution. If you can share the extra steps you took to set it up the way you like, I'd like to incorporate those into this project.

tjwebb avatar Mar 02 '15 20:03 tjwebb

thx. yeah i just extended the existing login method off the local protocol to do what i wanted

bgulotta avatar Mar 02 '15 21:03 bgulotta

My issue is when i try to post to auth/local i'm getting a passport initialized error:

[Error: passport.initialize() middleware not in use]

bgulotta avatar Mar 03 '15 14:03 bgulotta

Ah, the initialization occurs in the passport policy, and I'd omitted that from the AuthController policy by accident. I'll update the docs.

tjwebb avatar Mar 03 '15 18:03 tjwebb

Cool...Next issue:

warn: Error: Failed to serialize user into session at pass (/Users/bgulotta/git/reach-api/node_modules/sails-auth/node_modules/passport/lib/authenticator.js:277:19) at serialized (/Users/bgulotta/git/reach-api/node_modules/sails-auth/node_modules/passport/lib/authenticator.js:282:7) at module.exports (/Users/bgulotta/git/reach-api/node_modules/sails-auth/api/services/passport.js:354:3) at pass (/Users/bgulotta/git/reach-api/node_modules/sails-auth/node_modules/passport/lib/authenticator.js:290:9) at Authenticator.serializeUser (/Users/bgulotta/git/reach-api/node_modules/sails-auth/node_modules/passport/lib/authenticator.js:295:5) at IncomingMessage.req.login.req.logIn (/Users/bgulotta/git/reach-api/node_modules/sails-auth/node_modules/passport/lib/http/request.js:48:29) at /Users/bgulotta/git/reach-api/node_modules/sails-auth/api/controllers/AuthController.js:93:11

bgulotta avatar Mar 03 '15 20:03 bgulotta

Can you file new issues for each of these so they are more searchable?

Answer: That looks like an issue in your code, not mine :) It looks like you're re-implementing the stuff in https://github.com/tjwebb/sails-auth/blob/master/api/services/protocols/local.js and https://github.com/tjwebb/sails-auth/blob/master/api/services/passport.js but slightly differently. So I think passport might be trying to serialize and deserialize the user twice. The code you've written in your controller doesn't need to be there.

tjwebb avatar Mar 03 '15 20:03 tjwebb

"Sending 500 ("Server Error") response: RangeError: Maximum call stack size exceeded" from @grantbi

I also get this error when using only the local protocol. I've tried it on multiple fresh builds. I've been trying to track it down and I'm thinking it may be a lodash version or merge-defaults version issue possibly even a node.js version error. I've seen that in the bug reports in the past for this particular error.

I'm using: sails: ~0.11.0 sails-auth: 1.0.8 node: v0.10.29

@tjwebb if you have a link you can send me a link for your required testing or add a contributor.md for PRs I can contribute to this cool generator, cause I plan on using it allot.

scott-wyatt avatar Apr 09 '15 03:04 scott-wyatt

Thanks @scott-wyatt, any help is appreciated, and I'm glad you're finding it useful.

I'll find a good contributors.md. In the meantime, if you submit a PR that fixes a legit problem and includes a couple unit tests so I can see what's going on, I'll merge it in.

tjwebb avatar Apr 09 '15 03:04 tjwebb

Sounds good. I've tracked it down a little further by changing versions of node.js. It seems to be coming from some where in lodash.
After updating node to 0.12.2 this is the error i'm getting:

error: Sending 500 ("Server Error") response: 
 RangeError: Maximum call stack size exceeded
    at RegExp.test (native)
    at isNative (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/lodash/dist/lodash.js:1484:53)
    at Function.isPlainObject (/usr/local/lib/node_modules/sails/node_modules/waterline/node_modules/lodash/dist/lodash.js:2522:22)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:105:67
    at Array.forEach (native)
    at recursiveParse (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:96:22)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:115:16
    at Array.forEach (native)
    at recursiveParse (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:96:22)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:115:16
    at Array.forEach (native)
    at recursiveParse (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:96:22)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:115:16
    at Array.forEach (native)
    at recursiveParse (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:96:22)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:115:16
    at Array.forEach (native)
    at recursiveParse (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:96:22)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:115:16
    at Array.forEach (native)
    at recursiveParse (/usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:96:22)
    at /usr/local/lib/node_modules/sails/node_modules/waterline/lib/waterline/core/transformations.js:115:16 [RangeError: Maximum call stack size exceeded]

Still using: sails: 0.11.0 sails-auth: 1.0.8

UPDATE: I cleared the npm cache in my project folder and then did a npm install and all the errors I was having went away. My problem was user error combined with using a little old of version of node.

scott-wyatt avatar Apr 09 '15 04:04 scott-wyatt

The unit tests pass on node 0.12, so we'll want to contrive a failing test for this so we don't run into it again in the future.

tjwebb avatar Apr 09 '15 06:04 tjwebb

Hello World :)

I wanted to know if you did fix this issue ? I've got the same error one Node.Js 0.1, Sails 0.10.5

StoloADiane avatar May 25 '15 14:05 StoloADiane

@StoloADiane, I would definitely make the switch to sails 11, but that still doesn't fix the issue. I submitted a PR to the sails core for the patch and a few others have also, it may get pushed into the next version sometime.

scott-wyatt avatar May 27 '15 01:05 scott-wyatt