forked from liamcurry/passport-steam
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a separate example using Express's router module to demonstrate
@Burnett01's excellent solution to liamcurry#37
- Loading branch information
Showing
5 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* Basic example demonstrating passport-steam usage within Express framework | ||
* This example uses Express's router to separate the steam authentication routes | ||
*/ | ||
var express = require('express') | ||
, passport = require('passport') | ||
, util = require('util') | ||
, session = require('express-session') | ||
, SteamStrategy = require('../../').Strategy | ||
, authRoutes = require('./routes/auth'); | ||
|
||
// Passport session setup. | ||
// To support persistent login sessions, Passport needs to be able to | ||
// serialize users into and deserialize users out of the session. Typically, | ||
// this will be as simple as storing the user ID when serializing, and finding | ||
// the user by ID when deserializing. However, since this example does not | ||
// have a database of user records, the complete Steam profile is serialized | ||
// and deserialized. | ||
passport.serializeUser(function(user, done) { | ||
done(null, user); | ||
}); | ||
|
||
passport.deserializeUser(function(obj, done) { | ||
done(null, obj); | ||
}); | ||
|
||
// Use the SteamStrategy within Passport. | ||
// Strategies in passport require a `validate` function, which accept | ||
// credentials (in this case, an OpenID identifier and profile), and invoke a | ||
// callback with a user object. | ||
passport.use(new SteamStrategy({ | ||
returnURL: 'http://localhost:3000/auth/steam/return', | ||
realm: 'http://localhost:3000/', | ||
apiKey: 'Your API key here' | ||
}, | ||
function(identifier, profile, done) { | ||
// asynchronous verification, for effect... | ||
process.nextTick(function () { | ||
|
||
// To keep the example simple, the user's Steam profile is returned to | ||
// represent the logged-in user. In a typical application, you would want | ||
// to associate the Steam account with a user record in your database, | ||
// and return that user instead. | ||
profile.identifier = identifier; | ||
return done(null, profile); | ||
}); | ||
} | ||
)); | ||
|
||
var app = express(); | ||
|
||
// configure Express | ||
app.set('views', __dirname + '/views'); | ||
app.set('view engine', 'ejs'); | ||
|
||
app.use(session({ | ||
secret: 'your secret', | ||
name: 'name of session id', | ||
resave: true, | ||
saveUninitialized: true})); | ||
|
||
// Initialize Passport! Also use passport.session() middleware, to support | ||
// persistent login sessions (recommended). | ||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
app.use(express.static(__dirname + '/../../public')); | ||
|
||
app.get('/', function(req, res){ | ||
res.render('index', { user: req.user }); | ||
}); | ||
|
||
app.get('/account', ensureAuthenticated, function(req, res){ | ||
res.render('account', { user: req.user }); | ||
}); | ||
|
||
app.get('/logout', function(req, res){ | ||
req.logout(); | ||
res.redirect('/'); | ||
}); | ||
|
||
// See views/auth.js for authentication routes | ||
app.use('/auth', authRoutes); | ||
|
||
app.listen(3000); | ||
|
||
// Simple route middleware to ensure user is authenticated. | ||
// Use this route middleware on any resource that needs to be protected. If | ||
// the request is authenticated (typically via a persistent login session), | ||
// the request will proceed. Otherwise, the user will be redirected to the | ||
// login page. | ||
function ensureAuthenticated(req, res, next) { | ||
if (req.isAuthenticated()) { return next(); } | ||
res.redirect('/'); | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var express = require('express') | ||
, router = express.Router() | ||
, passport = require('passport'); | ||
|
||
// GET /auth/steam | ||
// Use passport.authenticate() as route middleware to authenticate the | ||
// request. The first step in Steam authentication will involve redirecting | ||
// the user to steamcommunity.com. After authenticating, Steam will redirect the | ||
// user back to this application at /auth/steam/return | ||
router.get('/steam', | ||
passport.authenticate('steam', { failureRedirect: '/' }), | ||
function(req, res) { | ||
res.redirect('/'); | ||
}); | ||
|
||
// GET /auth/steam/return | ||
// Use passport.authenticate() as route middleware to authenticate the | ||
// request. If authentication fails, the user will be redirected back to the | ||
// login page. Otherwise, the primary route function function will be called, | ||
// which, in this example, will redirect the user to the home page. | ||
router.get('/steam/return', | ||
// Issue #37 - Workaround for Express router module stripping the full url, causing assertion to fail | ||
function(req, res, next) { | ||
req.url = req.originalUrl; | ||
next(); | ||
}, | ||
passport.authenticate('steam', { failureRedirect: '/' }), | ||
function(req, res) { | ||
res.redirect('/'); | ||
}); | ||
|
||
module.exports = router; |
This file contains 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