All Projects → koajs → Userauth

koajs / Userauth

Licence: mit
koa user auth middleware

Programming Languages

javascript
184084 projects - #8 most used programming language

Labels

Projects that are alternatives of or similar to Userauth

Ashen Blog
使用koa 2 + vue 2搭建自己的博客系统
Stars: ✭ 104 (-18.75%)
Mutual labels:  koa
Koalerplate
Simple Koa Boilerplate for APIs
Stars: ✭ 118 (-7.81%)
Mutual labels:  koa
Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+1416.41%)
Mutual labels:  koa
Strapi
🚀 Open source Node.js Headless CMS to easily build customisable APIs
Stars: ✭ 41,786 (+32545.31%)
Mutual labels:  koa
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+8467.97%)
Mutual labels:  koa
Awilix Koa
Awilix helpers/middleware for Koa 2
Stars: ✭ 121 (-5.47%)
Mutual labels:  koa
Chatroom
vue2聊天室,图灵机器人,node爬虫
Stars: ✭ 103 (-19.53%)
Mutual labels:  koa
Last Blog
仿GitHub风格个人博客, vue+vuex+koa+mongodb
Stars: ✭ 128 (+0%)
Mutual labels:  koa
Node Frameworks Benchmark
Simple HTTP benchmark for different nodejs frameworks using wrk
Stars: ✭ 117 (-8.59%)
Mutual labels:  koa
Koa Proxies
a [email protected] proxy middleware
Stars: ✭ 125 (-2.34%)
Mutual labels:  koa
Koa React Universal
lightweight React-Koa2 universal boilerplate, only what is essential
Stars: ✭ 112 (-12.5%)
Mutual labels:  koa
Herostory
王者荣耀故事站--小程序
Stars: ✭ 115 (-10.16%)
Mutual labels:  koa
Cash
HTTP response caching for Koa. Supports Redis, in-memory store, and more!
Stars: ✭ 122 (-4.69%)
Mutual labels:  koa
Koa Pug
A Pug middleware for Koa
Stars: ✭ 105 (-17.97%)
Mutual labels:  koa
Koa Vue Fullstack
A lightweight boilerplate for a universal webapp based on koa, mongodb, node, vue, and webpack
Stars: ✭ 126 (-1.56%)
Mutual labels:  koa
Cottage
Simple, fast HTTP router on koa.js.
Stars: ✭ 103 (-19.53%)
Mutual labels:  koa
Koa2 Easy
Easy way to use koa2
Stars: ✭ 118 (-7.81%)
Mutual labels:  koa
Koa React Isomorphic
Boilerplate for Koa & React
Stars: ✭ 128 (+0%)
Mutual labels:  koa
Electrode Csrf Jwt
Stateless Cross-Site Request Forgery (CSRF) protection with JWT
Stars: ✭ 127 (-0.78%)
Mutual labels:  koa
Node Rate Limiter Flexible
Node.js rate limit requests by key with atomic increments in single process or distributed environment.
Stars: ✭ 1,950 (+1423.44%)
Mutual labels:  koa

koa-userauth

NPM version build status Coveralls David deps

koa user auth abstraction layer middleware.

Install

$ npm install koa-userauth

Usage

koa-userauth is dependent on koa-session or koa-generic-session.

var koa = require('koa');
var userauth = require('koa-userauth');
var session = require('koa-generic-session');

var app = new koa();
app.keys = ['i m secret'];

app.use(session());
app.use(userauth({
  match: '/user',
  // auth system login url
  loginURLFormatter: function (url) {
    return 'http://login.demo.com/login?redirect=' + url;
  },
  // login callback and getUser info handler
  getUser: async ctx => {
    var token = this.query.token;
    // get user
    return user;
  }
}));

Arguments

If options.match or options.ignore is String instance, we will use path-match transfer it to Regex instance.

/**
 * User auth middleware.
 *
 * @param {Object} [options]
 *  - {String|Regex|Function(pathname, ctx)} match, detect which url need to check user auth.
 *      `''` empty string meaning match all, @see `path-match` package.
 *  - {String|Regex|Function(pathname, ctx)} ignore, detect which url no need to check user auth.
 *      If `match` exists, this argument will be ignored.
 *  - {Function(url, rootPath, ctx)} loginURLFormatter, format the login url.
 *  - {String} [rootPath], custom app url root path, default is '/'.
 *  - {String} [loginPath], default is '/login'.
 *  - {String} [loginCallbackPath], default is `options.loginPath + '/callback'`.
 *  - {String} [logoutPath], default is '/logout'.
 *  - {String} [userField], logined user field name on `this.session`, default is 'user', `this.session.user`.
 *  - {Async Function (ctx)} getUser, get user function, must get user info with `req`.
 *  - {Async Function (ctx, user)} [loginCallback], you can handle user login logic here,return [user, redirectUrl]
 *  - {Function(ctx)} [loginCheck], return true meaning logined. default is `true`.
 *  - {Async Function (ctx, user)} [logoutCallback], you can handle user logout logic here.return redirectUrl
 *  - {Function(ctx)} [getRedirectTarget], customize how to get the redirect target after login
 * @return {Async Function (next)} userauth middleware
 * @public
 */

Login flow

  1. unauth user, redirect to $loginPath?redirect=$currentURL
  2. user visit $loginPath, redirect to options.loginURLFormatter() return login url.
  3. user visit $loginCallbackPath, handler login callback logic.
  4. If user login callback check success, will set req.session[userField], and redirect to $currentURL.
  5. If login check callback error, next(err).
  6. user visit $logoutPath, set req.session[userField] = null, and redirect back.

userauth flow

Source image file

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].