Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const functions = require('firebase-functions');
- const admin = require('firebase-admin');
- admin.initializeApp(functions.config().firebase);
- const express = require('express');
- const app = express();
- const passport = require('passport')
- const util = require('util')
- const session = require('express-session')
- const SteamStrategy = require('passport-steam').Strategy
- // 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://example.com/auth/steam/return',
- realm: 'http://example.com/',
- apiKey: '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);
- });
- }
- ));
- // 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('/hello', (req, res) => {
- res.send(`Hello`);
- });
- app.get('/account', ensureAuthenticated, function(req, res){
- res.status(200).json({ user: req.user });
- });
- // 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
- app.get('/auth/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.
- app.get('/auth/steam/return',
- passport.authenticate('steam', { failureRedirect: '/' }),
- function(req, res) {
- res.redirect('http://example.com/');
- });
- // 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('/');
- }
- exports.app = functions.https.onRequest(app);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement