Passport-js如何创建自定义策略 [英] Passport-js How to create a custom strategy

查看:316
本文介绍了Passport-js如何创建自定义策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求制定自己的策略.

I am looking to create my own strategy.

我有client_idclient_secret和相关的元数据.我也知道执行流程.因此,我想创建自己的策略并添加用于身份验证的自定义逻辑.

I have client_id, client_secret and related meta data. I also know the flow of execution. So I want to create my own strategy and add my custom logic for authentication.

我查看了 passport-strategy ,但我不了解如何实施我的自己的策略.有人可以解释吗?

I looked at passport-strategy, but I am not understanding how to implement my own strategy. Can anyone explain it?

推荐答案

您在此处有两个选择:

如果您具有用于身份验证的自定义逻辑,则实际上不需要创建自己的策略...您可以使用密码自定义策略,可让您构建此逻辑.根据文档:

If you have a custom logic for authentication you don't really need to create your own strategy... you can use the passport-custom strategy which allows you to build this logic. According to the documentation:

自定义身份验证策略通过您选择的自定义逻辑对用户进行身份验证

The custom authentication strategy authenticates users by custom logic of your choosing

除非您实际上想构建要分发的策略(例如,OpenID的实现或类似的东西),否则我不认为实现自己的策略很重要.

Unless you want to actually build a strategy that you want to distribute (eg: an implementation of OpenID or something like that), I don't see the point on implementing your own strategy.

但是,实施自己的策略包括实施护照策略抽象类.我建议您查看 Github 页面而不是npm页面,因为它具有有关的更多信息如何起床并运行.基本上,要制定自己的策略,要遵循的步骤是:

However, implementing your own strategy consists in implementing the passport-strategy abstract class. I'd suggest to look into the Github page instead of the npm page as it has more information about how to get up and running. Basically, the steps to follow to have your own strategy is:

  1. 子类别策略
  2. 通过在原型上定义authenticate()方法来实施身份验证(这里将有您的自定义逻辑).
  3. 调用增强方法之一(.success,.fail,.pass,.redirect或.error)

最后,您需要将其打包为npm模块,一旦一切就绪,就可以在Node.js项目中要求自己的策略.

Finally you would need to pack it as an npm module and once you have everything in place you can go and require your own strategy in your Node.js project.

正如我所说,我认为您需要有充分的理由采取自己的策略.我会尝试密码自定义.

As I said, I think that you need to have a good reason to go for your own strategy. I'd give a try to the passport-custom.

这篇关于Passport-js如何创建自定义策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆