liamcurry/passport-steam

req.user returns id, how to retrieve the users profile

Closed this issue · 1 comments

Hi, I've read the closed issue (#70) previously on this matter but I'm still stuck with their ID only, I can't seem to pull their profile stored in the database.

Passport.js

const passport = require('passport');
const SteamStrategy = require('passport-steam');
const User = require('../models/user');

require('dotenv').config();

passport.serializeUser((user, done) => {
  done(null, user.id);
});

passport.deserializeUser((id, done) => {
  User.findById(id).then(user => {
    done(null, user.id);
  });
});

passport.use(
  new SteamStrategy(
    {
      returnURL: 'https://hgvmp.localtunnel.me/auth/steam/redirect',
      realm: 'https://hgvmp.localtunnel.me/',
      apiKey: process.env.STEAM_API_KEY
    },
    (identifier, profile, done) => {
      // Check if user already exists in our database
      User.findOne({ steamID: profile._json.steamid }).then(existingUser => {
        if (existingUser) {
          // User already exists
          console.log(`User is: ${existingUser}`);
          done(null, existingUser);
        } else {
          // User does not exist, create them
          new User({
            username: profile.displayName,
            steamID: profile._json.steamid,
            avatar: profile._json.avatarfull,
            steamURL: profile._json.profileurl
          })
            .save()
            .then(newUser => {
              console.log(`new user created: ${newUser}`);
              done(null, newUser);
            });
        }
      });
    }
  )
);

Auth Route

const router = require('express').Router();
const passport = require('passport');

router.get(
  '/steam',
  passport.authenticate('steam', { failureRedirect: '/' }),
  (req, res) => {
    res.redirect('/');
  }
);

router.get(
  '/steam/redirect',
  (req, res, next) => {
    req.url = req.originalUrl;
    next();
  },
  passport.authenticate('steam', { failureRedirect: '/' }),
  (req, res) => {
    res.redirect('/');
  }
);

router.get('/logout', (req, res) => {
  req.logout();
  res.redirect('/');
});

module.exports = router;

server.js

const express = require('express');
const mongoose = require('mongoose');
const cookieSession = require('cookie-session');
const passport = require('passport');

require('dotenv').config();

const passportSetup = require('./config/passport');
const authRoutes = require('./routes/auth');

const app = express();

app.set('view engine', 'ejs');
app.use(express.static(`${__dirname}/public`));

app.use(cookieSession({
  maxAge: 24 * 60 * 60 * 1000,
  keys: [process.env.COOKIE_KEY]
}));

app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
  res.locals.user = req.user;
  next();
});

mongoose.connect(process.env.MLAB_URI, () => {
  console.log('Connected to the database')
});

app.use('/auth', authRoutes);

app.use('/', (req, res) => {
  res.render('index', {
    title: 'HGVMP: Multiplayer Modification & Community'
  });
});

// Start of server
app.listen(3000, () => {
  console.log('Server started')
})
// End of server

Sorted. It didn't like (de)serialising via the ID. Using the example given for the entire profile works just fine.