GoogleAuthProvider class

Nhà cung cấp để tạo OAuthCredential cho ProviderId .GOOGLE.

Chữ ký:

export declare class GoogleAuthProvider extends BaseOAuthProvider 

Mở rộng: BaseOAuthProvider

nhà xây dựng

Người xây dựng sửa đổi Sự miêu tả
(người xây dựng)() Xây dựng một phiên bản mới của lớp GoogleAuthProvider

Của cải

Tài sản sửa đổi Kiểu Sự miêu tả
GOOGLE_SIGN_IN_METHOD static 'google.com' Luôn đặt thành SignInMethod .GOOGLE.
PROVIDER_ID static 'google.com' Luôn đặt thành ProviderId .GOOGLE.

phương pháp

Phương pháp sửa đổi Sự miêu tả
thông tin xác thực (idToken, accessToken) static Tạo thông tin xác thực cho Google. Cần có ít nhất một mã thông báo ID và mã thông báo truy cập.
thông tin xác thựcFromError(lỗi) static Được sử dụng để trích xuất OAuthCredential cơ bản từ AuthError được đưa ra trong quá trình đăng nhập, liên kết hoặc xác thực lại.
thông tin xác thựcFromResult(userCredential) static Được sử dụng để trích xuất OAuthCredential cơ bản từ UserCredential .

GoogleAuthProvider.(hàm tạo)

Xây dựng một phiên bản mới của lớp GoogleAuthProvider

Chữ ký:

constructor();

GoogleAuthProvider.GOOGLE_SIGN_IN_METHOD

Luôn đặt thành SignInMethod .GOOGLE.

Chữ ký:

static readonly GOOGLE_SIGN_IN_METHOD: 'google.com';

GoogleAuthProvider.PROVIDER_ID

Luôn đặt thành ProviderId .GOOGLE.

Chữ ký:

static readonly PROVIDER_ID: 'google.com';

GoogleAuthProvider.credential()

Tạo thông tin xác thực cho Google. Cần có ít nhất một mã thông báo ID và mã thông báo truy cập.

Chữ ký:

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

Thông số

Tham số Kiểu Sự miêu tả
idToken chuỗi | vô giá trị Mã thông báo ID Google.
truy cập thẻ chuỗi | vô giá trị Mã thông báo truy cập của Google.

Trả về:

Thông tin xác thực OAuth

Ví dụ

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

GoogleAuthProvider.credentialFromError()

Được sử dụng để trích xuất OAuthCredential cơ bản từ AuthError được đưa ra trong quá trình đăng nhập, liên kết hoặc xác thực lại.

Chữ ký:

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

Thông số

Tham số Kiểu Sự miêu tả
lỗi FirebaseLỗi

Trả về:

Thông tin xác thực OAuth | vô giá trị

GoogleAuthProvider.credentialFromResult()

Được sử dụng để trích xuất OAuthCredential cơ bản từ UserCredential .

Chữ ký:

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

Thông số

Tham số Kiểu Sự miêu tả
thông tin người dùng Thông tin người dùng Thông tin xác thực của người dùng.

Trả về:

Thông tin xác thực OAuth | vô giá trị

ví dụ 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;
}

Ví dụ 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;