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
Size: 11
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
m i egg-passport --save
m i egg-passport-local --save
Note: also need egg-passport .
app_root}/config/plugin.js
rts.passport = {
able: true,
ckage: 'egg-passport',
rts.passportLocal = {
able: true,
ckage: 'egg-passport-local',
app_root}/config/config.default.js
rts.passportLocal = {
usernameField: 'username',
passwordField: 'password',
The available options are:
usernameField
- Optional, defaults to username
passwordField
- Optional, defaults to password
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;
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.
Please open an issue here.