跳到內容
從 NextAuth.js v4 遷移?請閱讀 我們的遷移指南.

providers/google

內建的 Google 整合。

GoogleProfile

繼承

屬性

aud

aud: string;

azp

azp: string;

email

email: string;

email_verified

email_verified: boolean;

exp

exp: number;

family_name?

optional family_name: string;

given_name

given_name: string;

hd?

optional hd: string;

iat

iat: number;

iss

iss: string;

jti?

optional jti: string;

locale?

optional locale: string;

name

name: string;

nbf?

optional nbf: number;

picture

picture: string;

sub

sub: string;

default()

default<P>(options): OAuthConfig<P>

將 Google 登入新增至您的頁面。

設定

回調網址

https://example.com/api/auth/callback/google

組態

import { Auth } from "@auth/core"
import Google from "@auth/core/providers/google"
 
const request = new Request(origin)
const response = await Auth(request, {
  providers: [
    Google({ clientId: GOOGLE_CLIENT_ID, clientSecret: GOOGLE_CLIENT_SECRET }),
  ],
})

資源

注意事項

預設情況下,Auth.js 假設 Google 供應商基於 Open ID Connect 規格。

建立憑證時使用的「授權重新導向 URI」必須包含您的完整網域,並以回調路徑結尾。例如:

  • 適用於生產環境:https://{YOUR_DOMAIN}/api/auth/callback/google
  • 適用於開發環境:https://127.0.0.1:3000/api/auth/callback/google
⚠️

Google 僅在使用者首次登入時,向應用程式提供重新整理權杖。

若要強制 Google 重新發出重新整理權杖,使用者需要從他們的帳戶中移除該應用程式,然後再次登入:https://myaccount.google.com/permissions

或者,您也可以在 authorizationparams 物件中傳遞選項,這將強制在每次登入時都提供重新整理權杖,然而,這將要求所有使用者在每次登入時確認他們是否希望授權您的應用程式存取。

如果您需要存取 Google 帳戶的 RefreshToken 或 AccessToken,並且您未使用資料庫來持久儲存使用者帳戶,這可能是您需要做的事情。

const options = {
  providers: [
    Google({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
      authorization: {
        params: {
          prompt: "consent",
          access_type: "offline",
          response_type: "code"
        }
      }
    })
  ],
}
💡

Google 也會在 OAuth 設定檔中傳回 email_verified 布林值屬性。

您可以使用此屬性來限制僅允許在特定網域中具有已驗證帳戶的人員存取。

const options = {
  ...
  callbacks: {
    async signIn({ account, profile }) {
      if (account.provider === "google") {
        return profile.email_verified && profile.email.endsWith("@example.com")
      }
      return true // Do different verification for other providers that don't have `email_verified`
    },
  }
  ...
}
💡

Google 供應商附帶 預設組態。若要覆寫您的使用案例的預設值,請查看自訂內建 OAuth 供應商

免責聲明如果您認為在預設組態中發現錯誤,您可以開啟問題

Auth.js 嚴格遵守規格,並且不對供應商偏離規格的任何行為負責。您可以開啟問題,但如果問題是不符合規格,我們可能不會尋求解決方案。您可以在討論區中尋求更多協助。

類型參數

類型參數
P extends GoogleProfile

參數

參數類型
選項OAuthUserConfig<P>

回傳值

OAuthConfig<P>

Auth.js © Balázs Orbán 和團隊 -2024