Skip to content

Instantly share code, notes, and snippets.

@julianlam julianlam/library.js
Last active Sep 10, 2019

Embed
What would you like to do?
How to create a login override plugin
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;
{
"id": "nodebb-plugin-login-mysite",
"library": "./library.js",
"hooks": [
{ "hook": "action:auth.overrideLogin", "method": "login" }
]
}
@whittssg

This comment has been minimized.

Copy link

commented Dec 15, 2015

Hello,

How would you go about checking if the user already exists?

Thanks,

@julianlam

This comment has been minimized.

Copy link
Owner Author

commented Dec 15, 2015

The easiest way would be to check if the email as passed back via your service is associated with an existing user account:

var user = module.parent.require('../user');
user.getUidByEmail(email, function(err, uid) {
  // now you can use the uid. Likely uid is null if the email is not associated with any account
});
@whittssg

This comment has been minimized.

Copy link

commented Dec 15, 2015

Ah thanks.. Got it working. Authenticate against LDAP.. If error; return error, if success; check if user already exists, if so log in, if not create user.. I tried to add extra user settings at the same time based on stuff returned from LDAP but I couldnt get that to work: user.setUserField(uid, 'fullname', user.givenName + " " + user.sn);

Any idea why that wouldnt work? If not I will create a question on the nodebb forums.. Thanks again for all your help and this sample code.

@morwalz

This comment has been minimized.

Copy link

commented May 17, 2018

How can i just al toghter remove login screen and redirect user to sso screen.
I do not want to see this screen.
https://forums.examsbook.com/login
rather it get redirected to https://forums.examsbook.com/auth/examsbook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.