diff --git a/src/server/Server.js b/src/server/Server.js index dcdbd66..9009d6c 100644 --- a/src/server/Server.js +++ b/src/server/Server.js @@ -75,7 +75,7 @@ class Server extends EventEmitter { this.app.use(helmet()); this.app.use(express.json({ limit: '10mb' })); - this.app.use(express.urlencoded()); + this.app.use(express.urlencoded({ extended: true })); this.app.use(this.logRequest.bind(this)); // Logs every request this.app.use(this.logError.bind(this)); // Logs endpoints that error and sends a 500 this.app.use(this.ready.bind(this)); // denies requests before the server is ready @@ -167,7 +167,7 @@ class Server extends EventEmitter { this.logger.info(`${profile.username} (${profile.id}) is logging in.`); const user = await this.userDatabase.userFromDiscord(profile); callback(null, user); - })); + }), { successRedirect: '/api/login/discord/finalise' }); this.authenticator.addStrategy('local', new LocalStrategy(async (username, password, callback) => { const user = await this.userDatabase.findUser(username); diff --git a/src/server/middleware/Authenticator.js b/src/server/middleware/Authenticator.js index df1422b..5a6a3d2 100644 --- a/src/server/middleware/Authenticator.js +++ b/src/server/middleware/Authenticator.js @@ -60,13 +60,13 @@ class Authenticator { } - addStrategy (name, strategy) { + addStrategy (name, strategy, { failureRedirect = '/login/fail', successRedirect = '/home' } = {}) { this.logger.info(`Adding ${name} authentication strategy`); this.passport.use(name, strategy); // Quick access getter to get the middleware for authenticating Object.defineProperty(this, name, { get: () => { - return this.passport.authenticate(name, { failureRedirect: '/login/fail', successRedirect: '/home' }); + return this.passport.authenticate(name, { failureRedirect, successRedirect }); } }); } diff --git a/src/server/structures/User.js b/src/server/structures/User.js index 138df78..903f89c 100644 --- a/src/server/structures/User.js +++ b/src/server/structures/User.js @@ -31,10 +31,10 @@ class User { return this._passwordHash !== null; } - async setPassword (passwd) { + async setPassword (passwd, save = false) { const hash = await Argon2.hash(passwd); this._passwordHash = hash; - await this.save(); + if (save) await this.save(); } async authenticate (passwd) { @@ -67,6 +67,10 @@ class User { return this.permissions[perm]; } + hasExternalProfile (name) { + return Boolean(this.externalProfiles[name]); + } + save () { return this._db.updateUser(this); }