Last active
July 19, 2022 07:37
-
-
Save julianlam/19deafaa4dbd624ceecd to your computer and use it in GitHub Desktop.
How to create a login override plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var passport = module.parent.require('passport'), | |
passportLocal = module.parent.require('passport-local').Strategy, | |
plugin = {}; | |
plugin.login = function() { | |
winston.info('[login] Registering new local login strategy'); | |
passport.use(new passportLocal({passReqToCallback: true}, plugin.continueLogin)); | |
}; | |
plugin.continueLogin = function(req, username, password, next) { | |
// Do your stuff here (query API or SQL db, etc...) | |
// If the login was successful: | |
next(null, { | |
uid: uid | |
}, '[[success:authentication-successful]]'); | |
// But if the login was unsuccessful, pass an error back, like so: | |
next(new Error('[[error:invalid-username-or-password]]')); | |
/* | |
You'll probably want to add login in this method to determine whether a login | |
refers to an existing user (in which case log in as above), or a new user, in | |
which case you'd want to create the user by calling User.create. For your | |
convenience, this is how you'd create a user: | |
var user = module.parent.require('./user'); | |
user.create({ | |
username: 'someuser', | |
email: 'someuser@example.com' | |
}); | |
Acceptable values are: username, email, password | |
*/ | |
}; | |
module.exports = plugin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"id": "nodebb-plugin-login-mysite", | |
"library": "./library.js", | |
"hooks": [ | |
{ "hook": "action:auth.overrideLogin", "method": "login" } | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi @morwalz, you can do like this (plugin.json):
the library.js