0uth

中文文档
1.2.1发布于 2023/08/25MITCommonJS

GitHub npm Node.js GitHub last commit

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Supported Platforms
  4. Configuration
  5. Configuration Options
  6. Error Handling
  7. Contribution
  8. License

Zerouth

Zerouth is a module for handling OAuth authentication in Node.js applications. It provides a middleware that simplifies the integration of various OAuth providers. With Zerouth, you can quickly set up OAuth authentication for popular platforms like Google, Discord, Reddit, Facebook, and more coming soon.

Installation

You can install Zerouth using either npm or yarn:

Using npm:

npm install 0uth

Using yarn:

yarn add 0uth

Usage

Zerouth is designed to make OAuth authentication easier in your Node.js applications. Below is an example of how to use Zerouth to handle OAuth authentication with different platforms.

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');
});

Supported Platforms

Zerouth currently supports OAuth authentication for the following platforms:

  • Google
  • Discord
  • Reddit
  • Facebook
  • GitHub
  • LinkedIn
  • Spotify
  • ...and more

Configuration

Zerouth uses a configuration object to set up OAuth parameters. Make sure to provide the necessary client information and callback URLs as required by the OAuth providers.

Configuration Options

  • client: The name of the OAuth client (e.g., 'google', 'discord').
  • client_id: The client ID provided by the OAuth provider.
  • client_secret: The client secret provided by the OAuth provider.
  • redirect_url: The callback URL to redirect to after successful authentication.
  • success_redirect: The URL to redirect to after successful authentication.
  • failure_redirect: The URL to redirect to after failed authentication.
  • scope: (Optional) An array of scope strings for requesting specific permissions.
  • ...other configuration options specific to each OAuth provider.

Error Handling

Zerouth provides detailed error handling for various scenarios, including invalid configuration, unsupported clients, and duplicate paths or URLs. Make sure to handle errors appropriately based on your application's requirements.

Contribution

Feel free to fork dev branch :)

License

This project is licensed under the MIT License.


版本与发布记录

0uth 共发布 12 个版本,首次发布于 2023/08/10;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.1latest2023/08/253 年前
1.2.02023/08/253 年前
1.1.42023/08/103 年前
1.1.32023/08/103 年前
1.1.22023/08/103 年前
1.1.12023/08/103 年前

关于 0uth 的常见问题

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