eggjs/egg-passport-local

Name: egg-passport-local

Owner: egg

Description: wrap passport-local strategy for egg-passport

Created: 2017-05-09 06:48:08.0

Updated: 2018-05-13 16:42:57.0

Pushed: 2018-02-11 01:20:53.0

Homepage:

Size: 11

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

egg-passport-local

NPM version build status Test coverage David deps Known Vulnerabilities

Install
m i egg-passport --save
m i egg-passport-local --save

Note: also need egg-passport .

Usage
app_root}/config/plugin.js
rts.passport = {
able: true,
ckage: 'egg-passport',


rts.passportLocal = {
able: true,
ckage: 'egg-passport-local',

Configuration
app_root}/config/config.default.js
rts.passportLocal = {
 usernameField: 'username',
 passwordField: 'password',

The available options are:

Both fields define the name of the properties in the POST body that are sent to the server.

see config/config.default.js for more detail.

after login successful, we can redirect to origin url by using ctx.session.returnTo before go to login page, for example:

sessioin.returnTo = ctx.path;
Example

see fixture for more detail.

/controller/home.js
s HomeController extends Controller {
ync index() {
const ctx = this.ctx;
ctx.body = `
  <div>
    <h2>${ctx.path}</h2>
    <a href="/admin">admin</a>
  </div>
`;


ync admin() {
const { ctx } = this;
if (ctx.isAuthenticated()) {
  // show user info
} else {
  // redirect to origin url by ctx.session.returnTo
  ctx.session.returnTo = ctx.path;
  await ctx.render('login.html');
}


ync logout() {
const ctx = this.ctx;

ctx.logout();
ctx.redirect(ctx.get('referer') || '/');


s
outer.js
le.exports = app => {
p.router.get('/', 'home.render');
p.router.get('/admin', 'home.admin');

nst localStrategy = app.passport.authenticate('local');
p.router.post('/passport/local', localStrategy);

p.router.get('/logout', 'user.logout');

see passport example for more detail.

Questions & Suggestions

Please open an issue here.

License

MIT


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.