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