I have two applications:
- server ( REST API Server)
- node js
- Express
- jsonwebtokens
- express-jwt
- mongoose
- client (Portable Front-end)
- bootstrap
- Angular JS
- local-storage
- angular-facebook
- angular-jwt
Lateron, the client app will be ported for android, iphone and other platforms using phonegap. For OAuth, I am using Facebook as the provider. Now, I just realized JSON Web Tokens are the way to go for this kind of set up. My question is an architectural one rather than syntactical one - how to manage a secret key when signing the facebook access token and user id with JWT in nodejs?
So this is how the flow works in my app:
- Angular client has a Login button
- User Clicks the button > Facebook Auth starts
- Client receives user_id and FB Access Token
- Client sends[POST json body] both user_id and Access Token to Node+Express Server at 'http://server.com/auth/login'
Node Server has applied express-jwt to all routes except /auth/login with a
var expressJwt = require('express-jwt');
var jwt = require('jsonwebtoken');
app.use(expressjwt({ secret: ''}).unless({path: ['/auth/login']}));
Node server receives data from req.body, fetches all profile details from facebook using the JavascriptSDK, and signs it using
var token=expressjwt.sign({profile}, );
- Node Server stores(updates, if user_id exists) the new token in db and sends it as response to client
- client stores the new token it received as json data in local-storage
- client uses angular-jwt to fetch profile data from the new token and automatically attach the new token in Authorization header for all requests it sends to the server
Now, my questions are:
- Do I really need to store the JWT tokens in database? I am certainly not comparing the tokens in request headers with database
- Do I need to generate random secret keys for security, each time a person logs in? If yes then how would that fit in both client and server?
- When and where do I need to check for token expiration? and How do I refresh it?
I am kind of lost about the design flow and mechanism.