Last active
March 9, 2017 00:08
-
-
Save HenriBeck/fa1fa401b8193da765e207fa4e4ca750 to your computer and use it in GitHub Desktop.
Feathers authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import auth from 'feathers-authentication'; | |
import SteamStrategy from 'passport-steam'; | |
export default function authentication() { | |
const that = this; | |
const options = { | |
secret: 'supersecret', | |
cookie: { | |
enabled: true, | |
name: 'feathers-jwt', | |
}, | |
jwt: { | |
expiresIn: '30d', | |
issuer: 'feathers', | |
audience: 'localhost', | |
}, | |
}; | |
that.configure(auth(options)); | |
that.passport.use( | |
new SteamStrategy({ | |
returnURL: 'http://localhost:3000/auth/steam/return', | |
realm: 'http://localhost:3000/', | |
profile: false, | |
}, async (identifier, profile, done) => { | |
const [, id] = identifier.match(/https?:\/\/steamcommunity\.com\/openid\/id\/(\d+)/); | |
const users = that.service('users'); | |
const query = { id }; | |
const registeredUser = await users.find(query); | |
if (registeredUser.length === 1) { | |
return done(null, registeredUser[0]); | |
} | |
try { | |
const newUser = await users.create(query); | |
return done(null, newUser); | |
} catch (error) { | |
that.service('logs').create({ | |
message: 'Error while creating new user', | |
environment: 'server', | |
info: error, | |
steamId: id, | |
}); | |
return done(error, null); | |
} | |
}), | |
); | |
that.get('/auth/steam', auth.express.authenticate('steam')); | |
that.get( | |
'/auth/steam/return', | |
auth.express.authenticate('steam'), | |
(req, res, next) => { | |
req.app.passport | |
.createJWT({ id: req.user.id }, options) | |
.then((token) => { | |
res.cookie('feathers-jwt', token, { maxAge: 1000 * 60 * 60 * 24 * 30 }); | |
next(); | |
}); | |
}, | |
(req, res) => res.redirect('/'), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment