ran*_*its 6 node.js passport-facebook passport.js nestjs passport-facebook-token
我已经研究了两者passport-facebook以及passport-facebook-token与 NestJS 的集成。问题在于 NestJS 使用自己的实用程序(例如 AuthGuard)抽象了通行证实现。
因此,记录的ExpressJS样式实现不适用于 NestJS。例如,这与@nestjs/passport包不兼容:
var FacebookTokenStrategy = require('passport-facebook-token');
passport.use(new FacebookTokenStrategy({
clientID: FACEBOOK_APP_ID,
clientSecret: FACEBOOK_APP_SECRET
}, function(accessToken, refreshToken, profile, done) {
User.findOrCreate({facebookId: profile.id}, function (error, user) {
return done(error, user);
});
}
));
Run Code Online (Sandbox Code Playgroud)
这篇博文展示了一种passport-facebook-token使用不符合AuthGuard.
@Injectable()
export class FacebookStrategy {
constructor(
private readonly userService: UserService,
) {
this.init();
}
init() {
use(
new FacebookTokenStrategy(
{
clientID: <YOUR_APP_CLIENT_ID>,
clientSecret: <YOUR_APP_CLIENT_SECRET>,
fbGraphVersion: 'v3.0',
},
async (
accessToken: string,
refreshToken: string,
profile: any,
done: any,
) => {
const user = await this.userService.findOrCreate(
profile,
);
return done(null, user);
},
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
这里的问题是,这似乎与 NestJS 期望您处理通行证策略的方式完全不同。它是一起被黑客入侵的。它也可能在未来的 NestJS 更新中中断。这里也没有异常处理;我无法捕获异常,例如由于正在使用的回调性质InternalOAuthError而引发的异常passport-facebook-token。
是否有一种干净的方法来实现其中之一passport-facebook或passport-facebook-token使其使用@nestjs/passport'svalidate()方法?来自文档:对于每个策略,Passport 将调用 verify 函数(使用 @nestjs/passport 中的 validate() 方法实现)。应该有一种方法可以在构造函数中传递 a clientId,clientSecret然后将其余的逻辑放入validate()方法中。
我想最终结果看起来类似于以下内容(这不起作用):
import { Injectable } from "@nestjs/common";
import { PassportStrategy } from "@nestjs/passport";
import FacebookTokenStrategy from "passport-facebook-token";
@Injectable()
export class FacebookStrategy extends PassportStrategy(FacebookTokenStrategy, 'facebook')
{
constructor()
{
super({
clientID : 'anid', // <- Replace this with your client id
clientSecret: 'secret', // <- Replace this with your client secret
})
}
async validate(request: any, accessToken: string, refreshToken: string, profile: any, done: Function)
{
try
{
console.log(`hey we got a profile: `, profile);
const jwt: string = 'placeholderJWT'
const user =
{
jwt
}
done(null, user);
}
catch(err)
{
console.log(`got an error: `, err)
done(err, false);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在我的特殊情况下,我对callbackURL. 我只是在验证客户端转发到服务器的访问令牌。我只是把上面的内容明确化。
此外,如果您很好奇,上面的代码会产生一个InternalOAuthError但我无法在策略中捕获异常以查看真正的问题是什么,因为它没有正确实现。我知道在这种特殊情况下,access_token我传递的是无效的,如果我传递的是有效的,则代码有效。通过适当的实现,我将能够捕获异常,检查错误,并能够向用户冒泡适当的异常,在这种情况下是 HTTP 401。
InternalOAuthError: Failed to fetch user profile
Run Code Online (Sandbox Code Playgroud)
很明显异常是在validate()方法之外抛出的,这就是为什么我们的 try/catch 块没有捕获InternalOAuthError. 处理此异常对于正常的用户体验至关重要,我不确定 NestJS 在此实现中处理它的方式或应如何进行错误处理。
Strategy您所使用的类设置是在正确的轨道上extends PassportStrategy()。为了捕获 Passport 中的错误,您可以扩展AuthGuard('facebook')并添加一些自定义逻辑到handleRequest(). 您可以在此处阅读有关它的更多信息,或者查看文档中的此片段:
import {
ExecutionContext,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {
canActivate(context: ExecutionContext) {
// Add your custom authentication logic here
// for example, call super.logIn(request) to establish a session.
return super.canActivate(context);
}
handleRequest(err, user, info) {
// You can throw an exception based on either "info" or "err" arguments
if (err || !user) {
throw err || new UnauthorizedException();
}
return user;
}
}
Run Code Online (Sandbox Code Playgroud)
是的,这是使用 JWT 而不是 Facebook,但底层逻辑和处理程序是相同的,因此它仍然适合您。
| 归档时间: |
|
| 查看次数: |
1357 次 |
| 最近记录: |