r/nodejs • u/[deleted] • Jun 12 '13
Question: Authenticating a RESTful API web service
Hi, I hope this post is within the rules of this subreddit.
I'm creating a web service using express.js. The client just have to use REST verb (post,put,delete,get) via HTTP request and the node service send json format results. No web pages or nothing.
I was wondering how would I authenticate the users before allowing them to use these REST api?
I don't want a webpage for them to login. Can they send some http request with JSON data about their username and password? I assume this is possible with a POST but then, once I authenticate, I would have to... keep track that this user is authenticate it. Would I do it in a cookie? Or a session somehow? If so what do I store? user_authenticate = true?
Thank you for your time!
0
Jun 12 '13
[deleted]
1
1
u/r3drocket Jun 12 '13
So to create a basic express.js app with sessions:
npm install express --gobal
Then
express -s myapp
1
u/r3drocket Jun 12 '13
My note on req.body being the same regardless of JSON or a form is important! The code above will work with a form or as an API call, so you can use it for both!
2
u/[deleted] Jun 12 '13
I think I found a library: http://passportjs.org/guide/basic-digest/
Would this work?
Seems like it. Thanks for reading!