Coder Social home page Coder Social logo

mystpi / scratch-auth-express Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 60 KB

Scratch Auth integration for Express. Just plug and play, minimal setup required

License: MIT License

JavaScript 100.00%
auth express-middleware expressjs middleware scratch authentication cookie express cookie-session scratchauth

scratch-auth-express's Introduction

Scratch Auth integration for Express

  • Plug and play, minimal setup required
  • Easy to use

Installation

npm install sa-express

Usage

const express = require('express');
const scratchauth = require('sa-express');

const app = express();

const needsAuth = scratchauth(app, {
  secret: 'SuperSecret1234',
  appName: 'My Cool Express App',
  succeeded(req, res) {
    res.redirect('/welcome');
  },
  failed(req, res) {
    res.redirect('/authfailed');
  },
});

Options

Name Description Default
secret Secret that cookie-session will use. It should be stored securely in an environment variable. No default; this option is required
appName Name for Scratch Auth to use on the login page. ''
loginRoute Route for redirecting the user to Scratch Auth. '/auth/login'
verifyRoute Route for verifying Scratch Auth's repsonse. '/auth/verify'
logoutRoute Route for logging the user out. '/auth/logout'
logoutRedirect Route to redirect to after logging out. '/'
domain The domain of your app. This is only needed if your app unexpectedly redirects to localhost instead of your app's domain. It should not include http[s]:// or a trailing slash. ''
succeeded Called when the user has been logged in successfully. (req, res) => res.redirect('/')
failed Called when auth has failed. (req, res) => res.send('Auth failed')
cookie More options here. By default lasts 7 days with sameSite: lax.

Using Auth/Protected Routes

Calling scratchauth returns a middleware for protected routes. It will redirect the user if they are not logged in. By default, the redirect route is whatever you passed for loginRoute.

app.get('/dashboard', needsAuth(), (req, res) => {
  res.send(`Welcome to your dashboard, ${res.locals.username}!`);
});

You can manually implement protected routes by using res.locals.loggedIn:

app.get('/dashboard', (req, res) => {
  if (res.locals.loggedIn) {
    res.send(`Welcome to your dashboard, ${res.locals.username}!`);
  } else {
    res.redirect('/auth/login');
  }
});

In fact, needsAuth uses res.locals.loggedIn under the hood, so both of the methods are equivalent.

Demo Application

A demo can be found in demo/.

scratch-auth-express's People

Contributors

dependabot[bot] avatar mystpi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.