getters for bound functions

This commit is contained in:
Erik 2022-11-09 21:04:52 +02:00
parent 4ca16c78f9
commit e358f7fc97
Signed by: Navy.gif
GPG Key ID: 811EC0CD80E7E5FB

View File

@ -49,23 +49,22 @@ class Authenticator {
saveUninitialized: true saveUninitialized: true
})); }));
server.app.use(Passport.initialize()); this.passport = Passport;
server.app.use(Passport.session()); server.app.use(this.passport.initialize());
server.app.use(this.passport.session());
Passport.serializeUser((user, callback) => { this.passport.serializeUser((user, callback) => {
this.logger.debug(`Serialising ${user.name} (${user.id})`);
callback(null, user.id); callback(null, user.id);
}); });
Passport.deserializeUser(async (id, callback) => { this.passport.deserializeUser(async (id, callback) => {
this.logger.debug(`Deserialising ${id}`);
const user = await this.userdb.fetchUser(id); const user = await this.userdb.fetchUser(id);
callback(null, user); callback(null, user);
}); });
// TODO: Should probably allow injection of strategies instead of hardcoding this this through a function // TODO: Should probably allow injection of strategies instead of hardcoding this this through a function
Passport.use('discord', new Strategy({ this.passport.use('discord', new Strategy({
clientID: discordID, clientSecret: discordSecret, callbackURL, scope: discordScope, version: discordVersion clientID: discordID, clientSecret: discordSecret, callbackURL: callbackURL + '/discord', scope: discordScope, version: discordVersion
}, async (accessToken, refreshToken, profile, callback) => { }, async (accessToken, refreshToken, profile, callback) => {
this.logger.info(`${profile.username} (${profile.id}) is logging in.`); this.logger.info(`${profile.username} (${profile.id}) is logging in.`);
const user = await this.userdb.userFromDiscord(profile); const user = await this.userdb.userFromDiscord(profile);
@ -74,29 +73,27 @@ class Authenticator {
} }
// For API requests, does not redirect to a login page // Getters for bound functions to avoid having to bind them elsewhere
async _auth (req, res, next) {
this.logger.debug('Non-redirect auth');
if (await this._authenticate(req, res)) return next();
}
get authenticate () { get authenticate () {
return this._auth.bind(this); return this._auth.bind(this);
} }
get authenticateRedirect () {
return this._authenticateRedirect.bind(this);
}
// For API requests, does not redirect to a login page
async _auth (req, res, next) {
if (await this._authenticate(req, res)) return next();
}
// Meant for non-api paths // Meant for non-api paths
_authenticateRedirect (req, res, next) { _authenticateRedirect (req, res, next) {
this.logger.debug('Redirected auth'); this.logger.debug('Redirected auth');
if (!req.isAuthenticated()) return res.redirect('/login'); if (!req.isAuthenticated()) return res.redirect('/login');
next(); next();
} }
get authenticateRedirect () {
return this._authenticateRedirect.bind(this);
}
async _authenticate (req, res) { async _authenticate (req, res) {
if (req.isAuthenticated()) return true; if (req.isAuthenticated()) return true;