@coji/remix-auth-google

中文文档
4.2.0发布于 2025/01/27MITTypeScriptES Module零依赖近 30 天下载 3.8万
原始使用文档
文档目录5 节
  1. Supported runtimes
  2. Usage
  3. Create an OAuth application
  4. Create the strategy instance
  5. Setup your routes

GoogleStrategy

The Google strategy is used to authenticate users against a Google account. It extends the OAuth2Strategy.

Supported runtimes

RuntimeHas Support
Node.js✅
Cloudflare✅

Usage

Create an OAuth application

Follow the steps on the Google documentation to create a new application and get a client ID and secret.

Create the strategy instance

// 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)

Setup your routes

// app/routes/login.tsx
export default function Login() {
  return (
    <Form action="/auth/google" method="GET">
      <button>Login with Google</button>
    </Form>
  )
}
// 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)
}
// 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 共发布 7 个版本,首次发布于 2024/10/25;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.2.0latest2025/01/271.8 年前
4.1.02024/12/101.9 年前
4.0.02024/11/272 年前
3.0.32024/10/252 年前
3.0.22024/10/252 年前
3.0.12024/10/252 年前

关于 @coji/remix-auth-google 的常见问题

如何安装 @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 和提交记录判断。