Code Monkey home page Code Monkey logo

auth's Introduction

Rill
@rill/auth
API stability Standard NPM version Downloads Gitter Chat

Simple session authentication with login and logout for Rill with support for timeouts, refreshes and more.

Installation

npm install @rill/session @rill/auth

Example

const rill = require('rill')
const app = rill()
const session = require('@rill/session')
const auth = require('@rill/auth')

// Setup middleware
app.use(session()) // A session is required
app.use(auth())

// Work with authentication.
app.use((ctx, next)=> {
  var user = ...

  // A user can be anything.
  ctx.login(user, {
    ttl: '30 minutes', // optionally override ttl option
    refresh: false // optionally override refresh option
  })

  // User is attached to and a cookie created.
  ctx.locals.user === user //-> true

  // Test if a user is logged in.
  ctx.isLoggedIn() //-> true
  ctx.isLoggedOut() //-> false

  // Removes the user cookie.
  ctx.logout()
});

// Route that only allows logged in users.
app.get('/a', auth.isLoggedIn(), ...)

// Route that only allows logged out in users.
app.get('/b', auth.isLoggedOut(), ...)

Options

// To enable a login that automatically refreshes and expires after 1 hour of inactivity you can use:
{
  "key": "different-cookie-key", // change cookie name
  "ttl": "1 hour", // change when the auth expires.
  "refresh": true // automatically reset auth expiry on page load.
}

Utilities

auth.isLoggedIn({ fail, redirect, fallback })

Creates a middleware that will only continue if a user is logged in.

If the fail option is supplied it will throw a 401 error with the provided message when the user is not logged in.

app.use(auth.isLoggedIn({ fail: 'You must be logged in to access the api.' }))

If the redirect option is supplied it will redirect when the user is not logged in.

app.use(auth.isLoggedIn({ redirect: '/login' }))

If the fallback option is supplied it will call the fallback function when the user is not logged in.

app.use(auth.isLoggedIn({ fallback: handleUserNotLoggedIn }))
function handleUserNotLoggedIn (ctx, next) {...}

Otherwise nothing will happen but the next middleware will not be called.

auth.isLoggedOut({ fail, redirect, fallback })

If the fail option is supplied it will throw a 401 error with the provided message when the user is logged in.

app.use(auth.isLoggedOut({ fail: 'This page is only accessable when not logged in' }))

If the redirect option is supplied it will redirect when the user is logged in.

app.use(auth.isLoggedOut({ redirect: '/dashboard' }))

If the fallback option is supplied it will call the fallback function when the user is logged in.

app.use(auth.isLoggedOut({ fallback: handleUserLoggedIn }))
function handleUserLoggedIn (ctx, next) {...}

Otherwise nothing will happen but the next middleware will not be called.

Contributions

  • Use npm test to run tests.

Please feel free to create a PR!

auth's People

Contributors

dylanpiercey avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

auth's Issues

Update docs

Docs still use ctx instead of ctx.locals.

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.