Google 提供商 SSO 配置
¥Google provider SSO configuration
本页面说明如何为 单点登录 (SSO) 功能 设置 Google 提供程序。
¥The present page explains how to setup the Google provider for the Single Sign-On (SSO) feature.
安装
¥Installation
¥Install passport-google-oauth2:
- yarn
- npm
yarn add passport-google-oauth2
npm install --save passport-google-oauth2
配置示例
¥Configuration example
Google SSO 提供程序在 config/admin 文件 的 auth.providers 数组中配置:
¥The Google SSO provider is configured in the auth.providers array of the config/admin file:
- JavaScript
- TypeScript
/config/admin.js
const GoogleStrategy = require("passport-google-oauth2");
module.exports = ({ env }) => ({
  auth: {
    // ...
    providers: [
      {
        uid: "google",
        displayName: "Google",
        icon: "https://cdn2.iconfinder.com/data/icons/social-icons-33/128/Google-512.png",
        createStrategy: (strapi) =>
          new GoogleStrategy(
            {
              clientID: env("GOOGLE_CLIENT_ID"),
              clientSecret: env("GOOGLE_CLIENT_SECRET"),
              scope: [
                "https://www.googleapis.com/auth/userinfo.email",
                "https://www.googleapis.com/auth/userinfo.profile",
              ],
              callbackURL:
                strapi.admin.services.passport.getStrategyCallbackURL("google"),
            },
            (request, accessToken, refreshToken, profile, done) => {
              done(null, {
                email: profile.email,
                firstname: profile.given_name,
                lastname: profile.family_name,
              });
            }
          ),
      },
    ],
  },
});
/config/admin.ts
import {Strategy as GoogleStrategy } from "passport-google-oauth2";
export default ({ env }) => ({
  auth: {
    // ...
    providers: [
      {
        uid: "google",
        displayName: "Google",
        icon: "https://cdn2.iconfinder.com/data/icons/social-icons-33/128/Google-512.png",
        createStrategy: (strapi) =>
          new GoogleStrategy(
            {
              clientID: env("GOOGLE_CLIENT_ID"),
              clientSecret: env("GOOGLE_CLIENT_SECRET"),
              scope: [
                "https://www.googleapis.com/auth/userinfo.email",
                "https://www.googleapis.com/auth/userinfo.profile",
              ],
              callbackURL:
                strapi.admin.services.passport.getStrategyCallbackURL("google"),
            },
            (request, accessToken, refreshToken, profile, done) => {
              done(null, {
                email: profile.email,
                firstname: profile.given_name,
                lastname: profile.family_name,
              });
            }
          ),
      },
    ],
  },
});