GoogleAuthProvider class

用于为ProviderId生成OAuthCredential 的提供程序。谷歌。

签名:

export declare class GoogleAuthProvider extends BaseOAuthProvider 

扩展: BaseOAuthProvider

构造函数

构造函数修饰符描述
(构造函数)()构造GoogleAuthProvider类的新实例

特性

财产修饰符类型描述
GOOGLE_SIGN_IN_METHOD static 'google.com'始终设置为SignInMethod 。谷歌。
提供商_ID static 'google.com'始终设置为ProviderId 。谷歌。

方法

方法修饰符描述
凭证(idToken,accessToken) static为 Google 创建凭据。至少需要 ID 令牌和访问令牌之一。
凭证来自错误(错误) static用于从登录、链接或重新身份验证操作期间引发的AuthError中提取基础OAuthCredential
凭证来自结果(用户凭证) static用于从UserCredential中提取底层OAuthCredential

GoogleAuthProvider.(构造函数)

构造GoogleAuthProvider类的新实例

签名:

constructor();

GoogleAuthProvider.GOOGLE_SIGN_IN_METHOD

始终设置为SignInMethod 。谷歌。

签名:

static readonly GOOGLE_SIGN_IN_METHOD: 'google.com';

GoogleAuthProvider.PROVIDER_ID

始终设置为ProviderId 。谷歌。

签名:

static readonly PROVIDER_ID: 'google.com';

GoogleAuthProvider.credential()

为 Google 创建凭据。至少需要 ID 令牌和访问令牌之一。

签名:

static credential(idToken?: string | null, accessToken?: string | null): OAuthCredential;

参数

范围类型描述
id令牌字符串|无效的Google ID 令牌。
访问令牌字符串|无效的谷歌访问令牌。

返回:

OAuth凭证

例子

// \`googleUser\` from the onsuccess Google Sign In callback.
const credential = GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token);
const result = await signInWithCredential(credential);

GoogleAuthProvider.credentialFromError()

用于从登录、链接或重新身份验证操作期间引发的AuthError中提取基础OAuthCredential

签名:

static credentialFromError(error: FirebaseError): OAuthCredential | null;

参数

范围类型描述
错误Firebase错误

返回:

OAuth 凭证|无效的

GoogleAuthProvider.credentialFromResult()

用于从UserCredential中提取底层OAuthCredential

签名:

static credentialFromResult(userCredential: UserCredential): OAuthCredential | null;

参数

范围类型描述
用户凭证用户凭证用户凭证。

返回:

OAuth 凭证|无效的

实施例1

// Sign in using a redirect.
const provider = new GoogleAuthProvider();
// Start a sign in process for an unauthenticated user.
provider.addScope('profile');
provider.addScope('email');
await signInWithRedirect(auth, provider);
// This will trigger a full page redirect away from your app

// After returning from the redirect when your app initializes you can obtain the result
const result = await getRedirectResult(auth);
if (result) {
  // This is the signed-in user
  const user = result.user;
  // This gives you a Google Access Token.
  const credential = GoogleAuthProvider.credentialFromResult(result);
  const token = credential.accessToken;
}

实施例2

// Sign in using a popup.
const provider = new GoogleAuthProvider();
provider.addScope('profile');
provider.addScope('email');
const result = await signInWithPopup(auth, provider);

// The signed-in user info.
const user = result.user;
// This gives you a Google Access Token.
const credential = GoogleAuthProvider.credentialFromResult(result);
const token = credential.accessToken;