简体   繁体   中英

Easy user authentication for Node.Js

I've been trying to use Passport / Express / Node.Js combination to handle user authentication, but it proved to be too difficult.

All the tutorials out there use MongoDB (which I don't want to use as I have Neo4J database that I want to use instead) and the examples on Passport's passport-local are not really suitable to me, because I already used a local authentication strategy from Node.Js in Action book - see on my github and I don't understand how to swap it over to the new one.

I can't seem to find any good, easy-to-use, step-by-step introduction for the beginners like me. It doesn't have to be Passport, but it has to be something, which would enable me to login users via a local strategy, as well as Facebook and Google.

It would also be good to know how it could be implemented without disrupting what I already have, so any help with this would be very much appreciated....

You could take a look to a very simple user authentication snippet created by Charles Daniel: https://gist.github.com/charlesdaniel/1686663

Basically the username and password are harcoded in the sever code (hack/thegibson) but you can use it as a start to build something more meaningful

Passport is going to be your best/easiest choice because a lot of other people use and it has a lot of pre-made strategies as you mention.

The tutorial use of Mongo is just an example. You can make use of any asynchronous connection that you want (mysql, neo4j, read the file system, etc) so long as you construct an object which represents your user and call the done function that is given to you.

One of the greatest benefits of using Passport is that you CAN switch the strategy involved without disrupting anything else. If you are simply swapping out the data storage behind the LocalStrategy then all you should have to change are the authorize and deserializeUser functions such that they access your new DB. If the structure of the User object changes then you'll also have to tweak your serializeUser function to return whatever the identifying token/value is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM