Advertisement
chriswingler

index.js

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