【问题标题】:steam passport implementation, convert from express to nestjsSteam 护照实施,从 express 转换为 nestjs
【发布时间】:2023-02-24 03:44:04
【问题描述】:

我已经开始将 express 项目转换为 nestjs。它应该如何在 Nestjs 中工作。这是 Express 的工作代码。 (下面的代码只是重定向到 Steam 登录页面)

/* eslint-disable space-before-function-paren */
// Require all the installs
var express = require('express');
var passport = require('passport');
var session = require('express-session');
var passportSteam = require('passport-steam');
var SteamStrategy = passportSteam.Strategy;
var app = express();
// Let's set a port
var port = 4000;
// Spin up the server
app.listen(port, () => {
  console.log('Listening, port ' + port);
});

// Set up the SteamStrategy
// Required to get data from user for sessions
passport.serializeUser((user, done) => {
  done(null, user);
});

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

// Initiate Strategy
passport.use(
  new SteamStrategy(
    {
      returnURL: 'http://localhost:' + port + '/api/auth/steam/return',
      realm: 'http://localhost:' + port + '/',
      apiKey: 'My API key',
    },
    function (identifier, profile, done) {
      process.nextTick(function () {
        profile.identifier = identifier;
        return done(null, profile);
      });
    }
  )
);

app.use(
  session({
    secret: 'Whatever_You_Want',
    saveUninitialized: true,
    resave: false,
    cookie: {
      maxAge: 3600000,
    },
  })
);
app.use(passport.initialize());
app.use(passport.session());

// Routes
app.get('/', (req, res) => {
  res.send(req.user);
});
app.get(
  '/api/auth/steam',
  passport.authenticate('steam', { failureRedirect: '/' }),
  function (req, res) {
    res.redirect('/');
  }
);

app.get(
  '/api/auth/steam/return',
  passport.authenticate('steam', { failureRedirect: '/' }),
  function (req, res) {
    res.redirect('/');
  }
);

问题是如何在 nestjs 中实现相同的??? 或者,如果我想为护照库(serializeUser、deserializeUser)实现中间件,它应该如何发生,在 nest 官方文档中我找到了自定义中间件的示例

export function logger(req: Request, res: Response, next: NextFunction) {
  console.log(`Request...`);
  next();
};

但是我应该如何使用护照中间件

【问题讨论】:

  • 你可能会在here找到你的答案

标签: javascript express nestjs passport.js


【解决方案1】:

如果您仍然需要答案,请根据 Nestjs 文档了解如何完成此操作:

我假设您知道如何设置 Nest 应用程序并解决 Steam 身份验证问题

  1. 创建一个处理/steam/auth路径的控制器:
    import { Controller, Get, Req, Res, UseGuards } from '@nestjs/common';
    import { AuthGuard } from '@nestjs/passport';
    import { Request, Response } from 'express';
    import { AuthService } from './auth.service';
    
    @Controller('auth')
    export class AuthController {
     constructor(private readonly authService: AuthService) {}
    
     @Get('steam')
     @UseGuards(AuthGuard('steam'))
     steamLogin() {}
    }
    
    1. 在单独的steam.strategy文件中配置steam策略:
    import { Strategy } from 'passport-steam';
    import { PassportStrategy } from '@nestjs/passport';
    import { Injectable } from '@nestjs/common';
    
    
    @Injectable()
    export class SteamStrategy extends PassportStrategy(Strategy) { 
        super({
          returnURL: 'http://localhost:3001/auth/return',
          realm: 'http://localhost:3001/',
          apiKey: 'Your API key',
        });
      }
      validate(profile) {
         // add your code that is to be executed after the login succeeds here
         // the 'profile' variable is the https://steamcommunity.com/openid/id/<steamid> link
      }
    
    1. 不要忘记将策略导入您的auth.module:
    import { Module } from '@nestjs/common';
    import { SteamStrategy } from './steam.strategy';
    @Module({
      providers: [SteamStrategy],
    })
    export class AuthModule {}
    

    就这样。之后,您可以像使用 express 一样继续操作!

【讨论】:

    猜你喜欢
    • 2014-04-21
    • 1970-01-01
    • 2022-12-24
    • 2018-09-13
    • 2015-11-02
    • 2015-01-03
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多