我也同样好奇,所以今天花了一点时间尝试一下。
简而言之,当使用 Firebase Auth 时,我相信providerId
需要是现有受支持的提供商之一。
如果您升级到使用 Google Cloud Identity Platform,我相信您将能够配置自定义提供商,然后使用此功能进行身份验证:
- https://cloud.google.com/identity-platform https://cloud.google.com/identity-platform
我们可以看到firebase.auth.OAuthProvider
and firebase.auth().signInWithPopup
(or firebase.auth().signInWithRedirect
)与这里的许多提供者一起使用,例如。
- https://cloud.google.com/identity-platform/docs/web/apple https://cloud.google.com/identity-platform/docs/web/apple
- https://cloud.google.com/identity-platform/docs/web/microsoft https://cloud.google.com/identity-platform/docs/web/microsoft
除了我们通过标准 Firebase Auth 获得的这些提供商选择之外,Google Cloud Identity Platform 还允许我们添加 SAML 和 OpenID Connect (OIDC) 集成:
- https://cloud.google.com/identity-platform/docs/web/saml https://cloud.google.com/identity-platform/docs/web/saml
- https://cloud.google.com/identity-platform/docs/web/oidc https://cloud.google.com/identity-platform/docs/web/oidc
当使用其中任何一个添加新的身份提供商时,我们可以指定要使用的“提供商 ID”(前缀为saml.
or oidc.
)。然后将此自定义提供商 ID 与firebase.auth.OAuthProvider
and firebase.auth().signInWithPopup
(or firebase.auth().signInWithRedirect
) 如上所述。
例如,如果我创建了一个 ID 为的新身份提供者oidc.foo
,我的集成代码最终将如下所示:
const provider = new firebase.auth.OAuthProvider('oidc.foo');
firebase.auth().signInWithPopup(provider)
.then((result) => {
// result.credential is a firebase.auth.OAuthCredential object.
// result.credential.providerId is equal to 'oidc.foo'.
// result.credential.idToken is the OIDC provider's ID token.
})
.catch((error) => {
// Handle error.
});
根据我对此的理解,我相信我们目前只能以这种方式添加自定义提供程序,前提是它们符合 OpenID Connect (OIDC) 标准(包括 OIDC Discovery 部分,该部分使用/.well-known/openid-configuration
URL):
注意:如果您的 OIDC 提供商不遵守OIDC 发现规范 https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig,它不适用于 Identity Platform。
据我所知,目前实现“普通”OAuth2 提供程序的最佳方法是您上面使用的自定义后端函数流(基于 Firebase Auth 示例)。
作为解决这个问题的一部分,我决定看看如果我使用的提供商 ID 与我的帐户中配置的任何内容都不匹配会发生什么(这是一个相当详细的步骤,主要答案已经包含在上面,但是这可能有助于提供更多背景信息/帮助某人,因此将其包含在这里)
var provider = new firebase.auth.OAuthProvider("foo.example.com");
firebase
.auth()
.signInWithRedirect(provider)
.then((result) => console.log("OAuthProvider:", result))
.catch((error) => console.log("OAuthProvider::error:", error));
firebase
.auth()
.getRedirectResult()
.then((result) => console.log("RedirectResult:", result))
.catch((error) => console.log("RedirectResult::error:", error));
首先我去这个auth/auth-domain-config-required
error:
OAuthProvider::error: {
"code": "auth/auth-domain-config-required",
"message": "Be sure to include authDomain when calling firebase.initializeApp(), by following the instructions in the Firebase console."
}
我想也许这应该设置为我想要登录的 OAuth 提供程序,所以我设置authDomain
在我的 firebase 配置中foo.myauthprovider.com
,但是当我打电话时signInWithRedirect
,它尝试加载以下 URL(其中apiKey
是我的 firebase 项目的 API 密钥),无法加载:
https://foo.myauthprovider.com/__/auth/handler?apiKey=REDACTED&appName=%5BDEFAULT%5D&authType=signInViaRedirect&providerId=foo.example.com&redirectUrl=http%3A%2F%2Flocalhost%3A3000%2F&v=7.14.5
This /__/auth/handler
URL 是 Firebase Auth 保留 URL 的一部分,您可以在以下位置阅读更多信息:
- https://firebase.google.com/docs/hosting/reserved-urls#auth_helpers https://firebase.google.com/docs/hosting/reserved-urls#auth_helpers
在 StackOverflow 答案中对此进行了更好的解释,但这基本上是 Firebase Auth 用来处理 OAuth 回调的方法,以避免需要在前端公开敏感凭据,因此用户不需要始终实现自己的处理程序):
- 为什么 Firebase 身份验证在返回我的应用程序之前使用“中间件”重定向? https://stackoverflow.com/questions/53990604/why-does-firebase-auth-uses-a-middleware-redirect-before-returning-to-my-app/54002778#54002778
改变authDomain
我的 firebase 项目的实际自定义域修复了该问题,然后导致以下结果auth/operation-not-allowed
当我尝试重定向时出错:
RedirectResult::error: u {code: "auth/operation-not-allowed", message: "The identity provider configuration is not found.", a: null}