# @coji/remix-auth-google 中文文档

规范地址：https://npmcn.com/package/%40coji/remix-auth-google/

版本：4\.2\.0
npm 元数据更新时间：2025-01-27T05:05:42.122Z
中文译文更新时间：2026-11-10T10:35:57.880Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40coji/remix-auth-google
原文对照：https://npmcn.com/package/%40coji/remix-auth-google/?lang=original
项目主页：https://github.com/coji/remix-auth-google#readme
源码仓库：https://github.com/coji/remix-auth-google

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：9.8 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2024-10-25T06:35:28.970Z
- 最新版本发布：2025-01-27T05:05:41.959Z
- 近 30 天下载量：37807（2026-10-11 至 2026-11-09，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 0 个）

## 中文简介

暂无中文简介

## 安装

```sh
npm install @coji/remix-auth-google
```

也可以使用：`pnpm add @coji/remix-auth-google`、`yarn add @coji/remix-auth-google`、`bun add @coji/remix-auth-google`

## README

# 谷歌战略

Google策略被用来认证用户对一个Google账户的认证. 它扩展了OAuth2战略.

## 支持的运行时间

| 运行时间 | 有支持 |
| --- | --- |
| 节点.js | ✅ |
| 云雾花 | ✅ |

## 使用量

### 创建 OAuth 应用程序

继续前进 [谷歌文档](https://developers.google.com/identity/protocols/oauth2/web-server#creatingcred) 创建新的应用程序,并获得客户身份和秘密.

### 创建策略实例

```ts
// app/services/auth.server.ts
import { Authenticator } from 'remix-auth'
import { GoogleStrategy } from 'remix-auth-google'
import { createCookieSessionStorage, redirect } from 'react-router'

export type SessionUser = {
  id: string
  email: string
  displayName: string
  pictureUrl: string
}

let SESSION_KEY = 'user'
export const sessionStorage = createCookieSessionStorage<{
  [SESSION_KEY]: SessionUser
}>({
  cookie: {
    name: '__session',
    httpOnly: true,
    path: '/',
    sameSite: 'lax',
    secrets: [process.env.SESSION_SECRET],
    secure: process.env.NODE_ENV === 'production',
  },
})

export const getSession = async (request: Request) => {
  return await sessionStorage.getSession(request.headers.get('Cookie'))
}

export const getSessionUser = async (request: Request) => {
  const session = await getSession(request)
  return session?.get(SESSION_KEY)
}

export const saveSession = async (request: Request, user: SessionUser) => {
  const session = await getSession(request)
  session.set(SESSION_KEY, user)
  return new Headers({
    'Set-Cookie': await sessionStorage.commitSession(session),
  })
}

const googleStrategy = new GoogleStrategy(
  {
    clientId: 'YOUR_CLIENT_ID',
    clientSecret: 'YOUR_CLIENT_SECRET',
    redirectURI: 'https://example.com/auth/google/callback',
  },
  async ({ accessToken, tokens }) => {
    // Get the user data from your DB or API using the tokens and profile
    const profile = await GoogleStrategy.userProfile(tokens)
    return User.findOrCreate({ email: profile.emails[0].value })
  },
)

export const authenticator = new Authenticator<SessionUser>()
authenticator.use(googleStrategy)
```

### 设置路线

```tsx
// app/routes/login.tsx
export default function Login() {
  return (
    <Form action="/auth/google" method="GET">
      <button>Login with Google</button>
    </Form>
  )
}
```

```tsx
// app/routes/auth.google.tsx
import { authenticator } from '~/services/auth.server'
import type { Route } from './+types/auth.google'

export const loader = async ({ request }: Route.LoaderArgs) => {
  return await authenticator.authenticate('google', request)
}
```

```tsx
// app/routes/auth.google.callback.tsx
import { redirect } from 'react-router'
import { authenticator, saveSession } from '~/services/auth.server'
import type { Route } from './+types/auth.google.callback'

export let loader = ({ request }: Route.LoaderArgs) => {
  const user = authenticator.authenticate('google', request)
  const headers = await saveSession(user)
  return redirect('/dashboard', { headers })
}
```

## 常见问题

### 如何安装 @coji/remix-auth-google？

在项目目录运行 npm install @coji/remix-auth-google，或使用 pnpm add / yarn add / bun add。

### @coji/remix-auth-google 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @coji/remix-auth-google 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @coji/remix-auth-google 可以商用吗？

@coji/remix-auth-google 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @coji/remix-auth-google 的下载量有多少？

根据 npm 官方下载统计，@coji/remix-auth-google 在 2026/10/11 – 2026/11/09 这 30 天内被下载 37,807 次，日均约 1,260 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @coji/remix-auth-google 还在维护吗？

最新版本 4\.2\.0 发布于 2025/01/27（1\.8 年前）；至今共发布 7 个版本，其中过去 12 个月发布了 0 个；首次发布于 2024/10/25。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

