aboutsummaryrefslogtreecommitdiff
path: root/src/server/authentication
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/authentication')
-rw-r--r--src/server/authentication/AuthenticationManager.ts44
-rw-r--r--src/server/authentication/DashUserModel.ts87
-rw-r--r--src/server/authentication/Passport.ts13
3 files changed, 72 insertions, 72 deletions
diff --git a/src/server/authentication/AuthenticationManager.ts b/src/server/authentication/AuthenticationManager.ts
index 0cc1553c0..3c7858a72 100644
--- a/src/server/authentication/AuthenticationManager.ts
+++ b/src/server/authentication/AuthenticationManager.ts
@@ -26,21 +26,12 @@ export const getSignup = (req: Request, res: Response) => {
return undefined;
};
-const tryRedirectToTarget = (req: Request, res: Response) => {
- const target = (req.session as any)?.target;
- if (req.session && target) {
- res.redirect(target);
- } else {
- res.redirect('/home');
- }
-};
-
/**
* POST /signup
* Create a new local account.
*/
export const postSignup = (req: Request, res: Response, next: NextFunction) => {
- const email = req.body.email as String;
+ const email = req.body.email as string;
check('email', 'Email is not valid').isEmail().run(req);
check('password', 'Password must be at least 4 characters long').isLength({ min: 4 }).run(req);
check('confirmPassword', 'Passwords do not match').equals(req.body.password).run(req);
@@ -66,7 +57,7 @@ export const postSignup = (req: Request, res: Response, next: NextFunction) => {
const user = new User(model);
User.findOne({ email })
- .then((existingUser: any) => {
+ .then((existingUser: DashUserModel | null) => {
if (existingUser) {
return res.redirect('/login');
}
@@ -74,13 +65,15 @@ export const postSignup = (req: Request, res: Response, next: NextFunction) => {
.then(() => {
req.logIn(user, err => {
if (err) return next(err);
- tryRedirectToTarget(req, res);
+ res.redirect('/home');
return undefined;
});
})
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
.catch((err: any) => next(err));
return undefined;
})
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
.catch((err: any) => next(err));
return undefined;
};
@@ -108,7 +101,8 @@ export const getLogin = (req: Request, res: Response) => {
export const postLogin = (req: Request, res: Response, next: NextFunction) => {
if (req.body.email === '') {
User.findOne({ email: 'guest' })
- .then((user: any) => !user && initializeGuest())
+ .then((user: DashUserModel | null) => !user && initializeGuest())
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
.catch((err: any) => err);
req.body.email = 'guest';
req.body.password = 'guest';
@@ -132,7 +126,7 @@ export const postLogin = (req: Request, res: Response, next: NextFunction) => {
req.logIn(user, loginErr => {
if (loginErr) {
next(loginErr);
- } else tryRedirectToTarget(req, res);
+ } else res.redirect('/home');
});
return undefined;
};
@@ -163,15 +157,15 @@ export const postForgot = function (req: Request, res: Response, next: NextFunct
const { email } = req.body;
async.waterfall(
[
- function (done: any) {
- c.randomBytes(20, (err: any, buffer: Buffer) => {
+ function (done: (arg: null, token?: string) => void) {
+ c.randomBytes(20, (err: Error | null, buffer: Buffer) => {
if (err) {
done(null);
} else done(null, buffer.toString('hex'));
});
},
- function (token: string, done: any) {
- User.findOne({ email }).then((user: any) => {
+ function (token: string, done: (arg: null, token: string, user: DashUserModel) => void) {
+ User.findOne({ email }).then((user: DashUserModel | null) => {
if (!user) {
// NO ACCOUNT WITH SUBMITTED EMAIL
res.redirect('/forgotPassword');
@@ -182,7 +176,7 @@ export const postForgot = function (req: Request, res: Response, next: NextFunct
user.save().then(() => done(null, token, user));
});
},
- function (token: Uint16Array, user: DashUserModel, done: any) {
+ function (token: Uint16Array, user: DashUserModel, done: (arg: null, token: Error | null, data: string) => void) {
const smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
@@ -220,7 +214,7 @@ export const postForgot = function (req: Request, res: Response, next: NextFunct
export const getReset = function (req: Request, res: Response) {
User.findOne({ passwordResetToken: req.params.token, passwordResetExpires: { $gt: Date.now() } })
- .then((user: any) => {
+ .then((user: DashUserModel | null) => {
if (!user) return res.redirect('/forgotPassword');
res.render('reset.pug', {
title: 'Reset Password',
@@ -234,9 +228,9 @@ export const getReset = function (req: Request, res: Response) {
export const postReset = function (req: Request, res: Response) {
async.waterfall(
[
- function (done: any) {
+ function (done: (args: null, user: DashUserModel) => void) {
User.findOne({ passwordResetToken: req.params.token, passwordResetExpires: { $gt: Date.now() } })
- .then((user: any) => {
+ .then((user: DashUserModel | null) => {
if (!user) return res.redirect('back');
check('password', 'Password must be at least 4 characters long').isLength({ min: 4 }).run(req);
@@ -250,8 +244,8 @@ export const postReset = function (req: Request, res: Response) {
user.save()
.then(
- () => (req as any).logIn(user),
- (err: any) => err
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ () => req.logIn(user, (err: any) => err)
)
.catch(() => res.redirect('/login'));
done(null, user);
@@ -259,7 +253,7 @@ export const postReset = function (req: Request, res: Response) {
})
.catch(() => res.redirect('back'));
},
- function (user: DashUserModel, done: any) {
+ function (user: DashUserModel, done: (args: null, error: Error | null) => void) {
const smtpTransport = nodemailer.createTransport({
service: 'Gmail',
auth: {
diff --git a/src/server/authentication/DashUserModel.ts b/src/server/authentication/DashUserModel.ts
index 6fd8dd593..7aa7f2598 100644
--- a/src/server/authentication/DashUserModel.ts
+++ b/src/server/authentication/DashUserModel.ts
@@ -3,30 +3,31 @@ import * as mongoose from 'mongoose';
import { Utils } from '../../Utils';
type comparePasswordFunction = (candidatePassword: string, cb: (err: Error, isMatch: boolean) => void) => void;
-type mongooseDocument = { id: string }; // & mongoose.Document;
-export type DashUserModel = mongooseDocument & {
- email: string;
- password: string;
- passwordResetToken?: string;
- passwordResetExpires?: Date;
+export type DashUserModel = mongoose.Document & {
+ email?: string | null | undefined;
+ password?: string | null | undefined;
+ passwordResetToken?: string | null | undefined;
+ passwordResetExpires?: Date | null | undefined;
- dropboxRefresh?: string;
- dropboxToken?: string;
+ dropboxRefresh?: string | null | undefined;
+ dropboxToken?: string | null | undefined;
- userDocumentId: string;
- sharingDocumentId: string;
- linkDatabaseId: string;
- cacheDocumentIds: string;
+ userDocumentId?: string | null | undefined;
+ sharingDocumentId?: string | null | undefined;
+ linkDatabaseId?: string | null | undefined;
+ cacheDocumentIds?: string | null | undefined;
+ profile?:
+ | {
+ name?: string | null | undefined;
+ gender?: string | null | undefined;
+ location?: string | null | undefined;
+ website?: string | null | undefined;
+ picture?: string | null | undefined;
+ }
+ | null
+ | undefined;
- profile: {
- name: string;
- gender: string;
- location: string;
- website: string;
- picture: string;
- };
-
- comparePassword: comparePasswordFunction;
+ comparePassword?: comparePasswordFunction | null | undefined;
};
export type AuthToken = {
@@ -68,36 +69,38 @@ const userSchema = new mongoose.Schema(
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
userSchema.pre('save', function save(next: any) {
- const user = this;
- if (!user.isModified('password')) {
+ if (!this.isModified('password')) {
return next();
}
- bcrypt.genSalt(10, (err: Error, salt: string) => {
- if (err) {
- return next(err);
- }
- bcrypt.hash(
- user.password ?? '',
- salt,
- () => {},
- (cryptErr: mongoose.Error, hash: string) => {
- if (cryptErr) {
- return next(cryptErr);
- }
- user.password = hash;
- next();
- return undefined;
+ bcrypt.genSalt(
+ 10,
+ ((err: Error, salt: string) => {
+ if (err) {
+ return next(err);
}
- );
- return undefined;
- });
+ bcrypt.hash(
+ this.password ?? '',
+ salt,
+ () => {},
+ (cryptErr: mongoose.Error, hash: string) => {
+ if (cryptErr) {
+ return next(cryptErr);
+ }
+ this.password = hash;
+ next();
+ return undefined;
+ }
+ );
+ return undefined;
+ }).bind(this)
+ );
return undefined;
});
const comparePassword: comparePasswordFunction = function (this: DashUserModel, candidatePassword, cb) {
// Choose one of the following bodies for authentication logic.
// secure (expected, default)
- bcrypt.compare(candidatePassword, this.password, cb);
+ this.password && bcrypt.compare(candidatePassword, this.password, cb);
// bypass password (debugging)
// cb(undefined, true);
};
diff --git a/src/server/authentication/Passport.ts b/src/server/authentication/Passport.ts
index a62d38e3e..38a99bd45 100644
--- a/src/server/authentication/Passport.ts
+++ b/src/server/authentication/Passport.ts
@@ -1,25 +1,28 @@
import * as passport from 'passport';
import * as passportLocal from 'passport-local';
import User, { DashUserModel } from './DashUserModel';
+import { IncomingMessage } from 'webpack-dev-middleware';
const LocalStrategy = passportLocal.Strategy;
-passport.serializeUser<any, any>((req, user, done) => {
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+passport.serializeUser<any, IncomingMessage & DashUserModel>((req, user, done) => {
done(undefined, (user as DashUserModel)?.id);
});
-passport.deserializeUser<any, any>((id, done) => {
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+passport.deserializeUser<any, IncomingMessage & DashUserModel>((id, done) => {
User.findById(id)
.exec()
- .then((user: DashUserModel) => done(undefined, user));
+ .then((user: DashUserModel | null) => user && done(undefined, user));
});
// AUTHENTICATE JUST WITH EMAIL AND PASSWORD
passport.use(
new LocalStrategy({ usernameField: 'email', passReqToCallback: true }, (req, email, password, done) => {
User.findOne({ email: email.toLowerCase() })
- .then((user: DashUserModel) => {
- if (!user) {
+ .then((user: DashUserModel | null) => {
+ if (!user?.comparePassword) {
done(undefined, false, { message: 'Invalid email or password' }); // invalid email
} else {
user.comparePassword(password, (error: Error, isMatch: boolean) => {