steam auth in javascript

To implement Steam authentication in node.js using passport, you can follow the below steps:

  1. Install the required packages:
index.tsx
npm install passport-steam steam
33 chars
2 lines
  1. In your app.js or entry file, add the following code:
index.tsx
const passport = require('passport');
const SteamStrategy = require('passport-steam').Strategy;

passport.use(new SteamStrategy({
    returnURL: 'http://localhost:8080/auth/steam/return',
    realm: 'http://localhost:8080/',
    apiKey: 'YOUR_STEAM_API_KEY' // Replace with your Steam API key
  },
  function(identifier, profile, done) {
    // Here, you can get the user data from the 'profile' object and save it to database or session
    return done(null, profile);
  }
));
  
app.get('/auth/steam',
  passport.authenticate('steam'),
  function(req, res) {
});

app.get('/auth/steam/return',
  passport.authenticate('steam', {
    failureRedirect: '/'
  }),
  function(req, res) {
    // Successful authentication
    res.redirect('/');
});
745 chars
28 lines
  1. In your frontend code or client-side, create a button or link to redirect to the authentication route:
<a href="/auth/steam">Login with Steam</a>
43 chars
2 lines
  1. Upon clicking the button, the user will be redirected to the Steam login page. After successful authentication, the user will be redirected back to your app and the passport.authenticate() middleware function will authenticate the user and redirect to the success route.

Note: Replace the YOUR_STEAM_API_KEY with your own Steam API key. You can get the API key from the Steam developers website: https://steamcommunity.com/dev/apikey

gistlibby LogSnag