GithubHelp home page GithubHelp logo

mantra27 / 0uth Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 404 KB

An npm package powered by typescript that simplifies Passport and OAuth as a express middleware ๐Ÿš€

License: MIT License

TypeScript 100.00%

0uth's Introduction


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's People

Watchers

 avatar

0uth's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.