Skip to content

Commit

Permalink
Created a separate example using Express's router module to demonstrate
Browse files Browse the repository at this point in the history
@Burnett01's excellent solution to liamcurry#37
  • Loading branch information
welps committed Jul 16, 2016
1 parent 5000c7b commit f34c633
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 2 deletions.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ app.get('/auth/steam/return',

## Examples

### Basic example using Express

For a complete, working example, refer to the [signon example](https://github.com/liamcurry/passport-steam/tree/master/examples/signon) or follow the steps below. Do not forget to add your API key.

To run the example, you can perform the following from the command line:
Expand Down Expand Up @@ -95,6 +97,42 @@ To run the example, you can perform the following from the command line:
8. Go to the address you put in `realm` in your browser.
### Basic example using Express with Router
For a complete, working example, refer to the [signon example](https://github.com/liamcurry/passport-steam/tree/master/examples/signon) or follow the steps below. Do not forget to add your API key.
To run the example, you can perform the following from the command line:
1. Clone the repository
```git clone https://github.com/liamcurry/passport-steam.git```
2. Go into the repository
```cd passport-steam```
3. Download the required dependencies
```npm install```
4. Edit `examples/signon/app-router.js` with your favorite text editor
5. Update the `localhost` parameter and add your API key in this block within `examples/signon/app-router.js`
```
returnURL: 'http://localhost:3000/auth/steam/return',
realm: 'http://localhost:3000/',
apiKey: 'Your API key here'
```
6. Save your changes to `examples/signon/app-router.js`
7. Start the example
```
npm run example-router
```
8. Go to the address you put in `realm` in your browser.
## Tests
If you would like to contribute, please provide accompanying tests with [AVA](https://github.com/sindresorhus/ava)
Expand All @@ -113,7 +151,7 @@ $ ava
- [welps](https://github.com/welps)
- [mnzt](https://github.com/mnzt)
- [scholtzm](https://github.com/scholtzm)

- [Burnett01](https://github.com/Burnett01)

## License

Expand Down
94 changes: 94 additions & 0 deletions examples/signon/app-router.js
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('/');
}
3 changes: 3 additions & 0 deletions examples/signon/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/**
* Basic example demonstrating passport-steam usage within Express framework
*/
var express = require('express')
, passport = require('passport')
, util = require('util')
Expand Down
32 changes: 32 additions & 0 deletions examples/signon/routes/auth.js
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;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
},
"scripts": {
"test": "ava test/*.test.js",
"example": "node examples/signon/app.js"
"example": "node examples/signon/app.js",
"example-router": "node examples/signon/app-router.js"
},
"engines": {
"node": ">= 0.4.0"
Expand Down

0 comments on commit f34c633

Please sign in to comment.