Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. /**
  2. * Basic example demonstrating passport-steam usage within Express framework
  3. */
  4. var express = require('express')
  5. , passport = require('passport')
  6. , util = require('util')
  7. , session = require('express-session')
  8. , SteamStrategy = require('./').Strategy
  9. , path = require('path');
  10.  
  11. // Passport session setup.
  12. // To support persistent login sessions, Passport needs to be able to
  13. // serialize users into and deserialize users out of the session. Typically,
  14. // this will be as simple as storing the user ID when serializing, and finding
  15. // the user by ID when deserializing. However, since this example does not
  16. // have a database of user records, the complete Steam profile is serialized
  17. // and deserialized.
  18. passport.serializeUser(function(user, done) {
  19. done(null, user);
  20. });
  21.  
  22. passport.deserializeUser(function(obj, done) {
  23. done(null, obj);
  24. });
  25.  
  26. // Use the SteamStrategy within Passport.
  27. // Strategies in passport require a `validate` function, which accept
  28. // credentials (in this case, an OpenID identifier and profile), and invoke a
  29. // callback with a user object.
  30. passport.use(new SteamStrategy({
  31. returnURL: 'http://localhost:3000/auth/steam/return',
  32. realm: 'http://localhost:3000/',
  33. apiKey: 'DC5B7F1DDF731DA0463444ABB88BB790'
  34. },
  35. function(identifier, profile, done) {
  36. // asynchronous verification, for effect...
  37. process.nextTick(function () {
  38.  
  39. // To keep the example simple, the user's Steam profile is returned to
  40. // represent the logged-in user. In a typical application, you would want
  41. // to associate the Steam account with a user record in your database,
  42. // and return that user instead.
  43. profile.identifier = identifier;
  44. return done(null, profile);
  45. });
  46. }
  47. ));
  48.  
  49. var app = express();
  50.  
  51. // configure Express
  52. app.use('/views', express.static(path.join(__dirname, 'views')));
  53. app.use('/assets', express.static(path.join(__dirname, 'assets')));
  54. app.set('view engine', 'ejs');
  55.  
  56. app.use(session({
  57. secret: 'your secret',
  58. name: 'name of session id',
  59. resave: true,
  60. saveUninitialized: true}));
  61.  
  62. // Initialize Passport! Also use passport.session() middleware, to support
  63. // persistent login sessions (recommended).
  64. app.use(passport.initialize());
  65. app.use(passport.session());
  66. app.use(express.static(__dirname + '/public'));
  67.  
  68. app.get('/', function(req, res){
  69. res.render('index', { user: req.user });
  70. });
  71.  
  72. app.get('/account', ensureAuthenticated, function(req, res){
  73. res.render('account', { user: req.user });
  74. });
  75.  
  76. app.get('/logout', function(req, res){
  77. req.logout();
  78. res.redirect('/');
  79. });
  80.  
  81. // GET /auth/steam
  82. // Use passport.authenticate() as route middleware to authenticate the
  83. // request. The first step in Steam authentication will involve redirecting
  84. // the user to steamcommunity.com. After authenticating, Steam will redirect the
  85. // user back to this application at /auth/steam/return
  86. app.get('/auth/steam',
  87. passport.authenticate('steam', { failureRedirect: '/' }),
  88. function(req, res) {
  89. res.redirect('/');
  90. });
  91.  
  92. // GET /auth/steam/return
  93. // Use passport.authenticate() as route middleware to authenticate the
  94. // request. If authentication fails, the user will be redirected back to the
  95. // login page. Otherwise, the primary route function function will be called,
  96. // which, in this example, will redirect the user to the home page.
  97. app.get('/auth/steam/return',
  98. passport.authenticate('steam', { failureRedirect: '/' }),
  99. function(req, res) {
  100. res.redirect('/');
  101. });
  102.  
  103. app.listen(3000);
  104.  
  105. // Simple route middleware to ensure user is authenticated.
  106. // Use this route middleware on any resource that needs to be protected. If
  107. // the request is authenticated (typically via a persistent login session),
  108. // the request will proceed. Otherwise, the user will be redirected to the
  109. // login page.
  110. function ensureAuthenticated(req, res, next) {
  111. if (req.isAuthenticated()) { return next(); }
  112. res.redirect('/');
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement