gpt4 book ai didi

facebook - 如何在nestjs facebook策略回调url中传递动态参数

转载 作者:行者123 更新时间:2023-12-02 20:15:04 25 4
gpt4 key购买 nike

如何在 Facebook 登录回调 URL 中传递一些动态参数?

我有不同类型的用户(通过“类型”参数区分)使用 Facebook 登录进行注册。我使用 Passport-facebook 创建了一个 facebook 身份验证策略,效果很好。

但是在身份验证之后,当调用回调 url 时,我需要知道哪种类型的用户请求注册。

我猜我可以在定义回调 URL 时传递参数

类似这样的事情

http://localhost:3000/auth/facebook/callback/type1 http://localhost:3000/auth/facebook/callback/type2

如何将动态值传递到 FacebookStrategy 中?

或者实现这一目标的可能解决方法是什么?

//PassportStrategy.ts

@Injectable()
export class FacebookStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
clientID: 'MYID',
clientSecret: 'MYSCRET',
callbackURL: "http://localhost:3000/auth/facebook/callback",
profileFields: ['id', 'displayName', 'emails', 'photos']
});
}

async validate(accessToken: any, refreshToken: any, profile: any) {
return {
name: profile.displayName,
email: profile.emails[0].value,
provider: "facebook",
providerId: profile.id,
photo: profile.photos[0].value
}
}
}

//授权 Controller

@Controller('auth')
export class AuthController {
constructor(
@Inject(forwardRef(() => AuthService)) private readonly authService: AuthService,
) { }

@Get('/facebook')
@UseGuards(AuthGuard('facebook'))
async facebookAuth(@Request() req) {
return
}

@UseGuards(AuthGuard('facebook'))
@Get('/facebook/callback')
async facebookCallback(@Request() req) {
return this.authService.login(req.user);
}

}

基本上我希望能够调用“/auth/facebook/:type”并传递策略中定义的回调网址中的类型值

回调端点类似于“/auth/facebook/callback/:type”

因此,当我调用 authservice.login 函数时,我可以传递该“类型”并决定在第一次注册时要创建哪种类型的用户

如果我的方法错误,请指导我。谢谢

最佳答案

我最近一直在处理类似的问题,这是我的方法。可能不是最好的,但目前有效。

import { Inject, Injectable, Logger } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import passport = require('passport');
import { Strategy } from 'passport-facebook';

@Injectable()
export class FacebookStrategy extends PassportStrategy(Strategy, 'facebook') {
private readonly logger = new Logger(FacebookStrategy.name);

constructor(
@Inject('FACEBOOK_STRATEGY_CONFIG')
private readonly facebookStrategyConfig,
) {
super(
facebookStrategyConfig,
async (
request: any,
accessToken: string,
refreshToken: string,
profile: any,
done,
) => {
this.logger.log(profile);

// take the state from the request query params
const { state } = request.query;
this.logger.log(state);

// register user

// return callback
return done(null, profile);
},
);
passport.use(this);
}
}
import { Controller, Get, HttpStatus, Inject, Param, Query, Req } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Redirect } from '@nestjsplus/redirect';

@Controller('auth')
export class AuthController {
@Inject('ConfigService')
private readonly configService: ConfigService;

@Get(':provider/callback')
@Redirect()
async socialCallback(@Req() req, @Param('provider') provider: string, @Query('state') state: string) {
// here you can use the provider and the state
return {
statusCode: HttpStatus.FOUND,
url: `${this.configService.get('FRONTEND_HOST')}/dashboard`,
};
}
}
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AuthController } from './auth.controller';
import { FacebookStrategy } from './facebook.strategy';
import passport = require('passport');

const facebookStrategyConfigFactory = {
provide: 'FACEBOOK_STRATEGY_CONFIG',
useFactory: (configService: ConfigService) => {
return {
clientID: `${configService.get('FACEBOOK_CLIENT_ID')}`,
clientSecret: `${configService.get('FACEBOOK_CLIENT_SECRET')}`,
callbackURL: `${configService.get('FACEBOOK_OAUTH_REDIRECT_URI')}/callback`,
profileFields: ['id', 'displayName', 'link', 'photos', 'emails', 'name'],
passReqToCallback: true,
};
},
inject: [ConfigService],
};

@Module({
controllers: [AuthController],
providers: [facebookStrategyConfigFactory, FacebookStrategy],
})
export class AuthModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {

const facebookLoginOptions = {
session: false,
scope: ['email'],
state: null,
};
consumer
.apply((req: any, res: any, next: () => void) => {
const {
query: { state },
} = req;
facebookLoginOptions.state = state;
next();
}, passport.authenticate('facebook', facebookLoginOptions))
.forRoutes('auth/facebook/*');
}
}

现在让我解释一下:D。诀窍在于中间件配置。

const facebookLoginOptions = {
session: false,
scope: ['email'],
state: null,
};
consumer
.apply((req: any, res: any, next: () => void) => {
const {
query: { state },
} = req;
facebookLoginOptions.state = state;
next();
}, passport.authenticate('facebook', facebookLoginOptions))
.forRoutes('auth/facebook/*');

因此,oAuth 具有此功能,您可以通过登录流程传递状态参数。通过提取变量中的 Passport 选项,我们可以通过在 Passport 中间件之前应用另一个中间件来动态更改状态参数。这样,你现在就可以调用 http://localhost:3000/auth/facebook/login?state=anything-you-want并且此 state 查询参数将通过策略以及回调调用中传递。

我还使用示例创建了一个 git 存储库:https://github.com/lupu60/passport-dynamic-state

关于facebook - 如何在nestjs facebook策略回调url中传递动态参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57026793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com