# 0uth 中文文档

规范地址：https://npmcn.com/package/0uth/

版本：1\.2\.1
npm 元数据更新时间：2023-08-25T09:08:02.508Z
中文译文更新时间：2026-10-31T14:05:22.184Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/0uth
原文对照：https://npmcn.com/package/0uth/?lang=original
项目主页：https://github.com/Mantra27/0uth#readme
源码仓库：https://github.com/Mantra27/0uth

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

- 包体积（解压后）：55.6 kB，51 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- 运行依赖：21 个
- 首次发布：2023-08-09T17:59:16.091Z
- 最新版本发布：2023-08-25T09:08:02.317Z
- 已发布版本数：12（近 12 个月 0 个）

## 中文简介

![GitHub](https://img.shields.io/github/license/Mantra27/0uth) ![npm](https://img.shields.io/npm/v/0uth) ![Node.js](https://img.shields.io/node/v/0uth) ![GitHub last commit](https://img.shields.io/github/last-commit/Mantra27/0uth)

## 安装

```sh
npm install 0uth
```

也可以使用：`pnpm add 0uth`、`yarn add 0uth`、`bun add 0uth`

## README

* * *

# Zerouth

Zerouth是一个在Node.js应用程序中处理OAuth身份验证的模块。它提供了一个中间件，可以简化各种OAuth提供程序的集成。使用Zerouth，您可以快速为b谷歌，Discord, Reddit， Facebook等流行平台设置OAuth认证，以及更多即将推出的平台。

## 安装

你可以使用npm或yarn安装Zerouth：

使用 [npm](https://www.npmjs.com/package/0uth)：

```
npm install 0uth
```

使用纱:

```
yarn add 0uth
```

## 使用

Zerouth旨在使OAuth身份验证在Node.js应用程序中变得更容易。下面是如何使用Zerouth处理不同平台上的OAuth身份验证的示例。

```javascript
const passport = require('passport');
const zerouth = require('0uth'); // Replace with the correct import path
const express = require("express");

const app = express();

//use middlwares in this sequence only
app.use(require("express-session")({ secret: 'SECRET' , resave: true, saveUninitialized: true}))
app.use(passport.initialize())
app.use(passport.session())

// Define your OAuth configuration
const GoogleConfig = {
  client: 'google',
  client_id: 'GOOGLE_CLIENT_ID',
  redirect_url: '/google/callback', //add http://localhost:3000/google/callback in google cloud console
  client_secret: 'GOOGLE_CLIENT_SECRET',
  success_redirect: '/success',
  failure_redirect: '/login',
  scope: ["email", "profile"]
};

const LocalStrategy = {
  client: 'local',
  client_id: null, //<could_use_cookie>
  client_secret: null, //<could_use_cookie>
  redirect_url: '/local/callback',
  success_redirect: '/local/lmao',
  failure_redirect: '/login',
  //cb takes (3 argumensts, [req, res, callback])
  cb: (req, res, callback)=>{
    // if(cookie) return ValidateCookie(cookie, (result:String)=>{callback(null, {username: result.username)})

    // validate username and password using your own database
    if(req.body.username == "admin" && req.body.password == "admin"){
      return callback(null, {username: req.body.username, password: req.body.password})
    }
    
  }
}

// Apply the zerouth to your Express app
app.use(
  zerouth("/auth/google", GoogleConfig)
);

app.use(
  zerouth("/auth/local", LocalStrategy)
);

// Handle successful authentication
app.get('/success', (req, res) => {
  return res.send(req.user);
});

// Start your Express server
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});
```

## 支持的平台上

Zerouth目前支持以下平台的OAuth认证：

-   谷歌
-   不和
-   Reddit
-   脸谱网
-   GitHub
-   LinkedIn
-   Spotify
-   …和更多的

## 配置

Zerouth使用一个配置对象来设置OAuth参数。确保根据OAuth提供程序的要求提供必要的客户端信息和回调url。

### 配置选项

-   `client`： OAuth客户端的名称（例如，‘谷歌’，‘discord’）。
-   `client_id`： OAuth提供程序提供的客户端ID。
-   `client_secret`：由OAuth提供程序提供的客户端秘密。
-   `redirect_url`：认证成功后要重定向到的回调URL。
-   `success_redirect`：认证成功后要重定向到的URL。
-   `failure_redirect`：认证失败后重定向到的URL。
-   `scope`：（可选）用于请求特定权限的范围字符串数组。
-   …特定于每个OAuth提供程序的其他配置选项。

## 错误处理

Zerouth为各种场景提供了详细的错误处理，包括无效配置、不支持的客户端以及重复的路径或url。确保根据应用程序的需求适当地处理错误。

## 贡献

请随意用餐 `dev` 分支:)

## 许可证

本项目在 [MIT许可证](https://npmcn.com/package/0uth/LICENSE).

* * *

## 常见问题

### 如何安装 0uth？

在项目目录运行 npm install 0uth，或使用 pnpm add / yarn add / bun add。

### 0uth 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/0uth 是否存在。

### 0uth 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### 0uth 可以商用吗？

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

### 0uth 还在维护吗？

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

