mirror of
https://github.com/DeviceFarmer/stf.git
synced 2026-04-18 08:03:30 +02:00
Add user to rethinkdb after login.
This commit is contained in:
35
lib/middleware/auth.js
Normal file
35
lib/middleware/auth.js
Normal file
@@ -0,0 +1,35 @@
|
||||
var jwtutil = require('../util/jwtutil')
|
||||
var urlutil = require('../util/urlutil')
|
||||
|
||||
var dbapi = require('../db/api')
|
||||
|
||||
module.exports = function(options) {
|
||||
return function(req, res, next) {
|
||||
if (req.query.jwt) {
|
||||
// Coming from auth client
|
||||
var data = jwtutil.decode(req.query.jwt, options.secret)
|
||||
, redir = urlutil.removeParam(req.url, 'jwt')
|
||||
if (data) {
|
||||
// Redirect once to get rid of the token
|
||||
dbapi.saveUserAfterLogin(data)
|
||||
.then(function() {
|
||||
req.session.jwt = data
|
||||
res.redirect(redir)
|
||||
})
|
||||
.catch(next)
|
||||
}
|
||||
else {
|
||||
// Invalid token, forward to auth client
|
||||
res.redirect(options.authUrl)
|
||||
}
|
||||
}
|
||||
else if (req.session && req.session.jwt) {
|
||||
// Continue existing session
|
||||
next()
|
||||
}
|
||||
else {
|
||||
// No session, forward to auth client
|
||||
res.redirect(options.authUrl)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user