diff --git a/src/config/passport.ts b/src/config/passport.ts index fc808e80..3d0a231a 100644 --- a/src/config/passport.ts +++ b/src/config/passport.ts @@ -2,11 +2,8 @@ import passport from "passport"; import passportLocal from "passport-local"; import passportFacebook from "passport-facebook"; import _ from "lodash"; - -// import { User, UserType } from '../models/User'; import { User, UserDocument } from "../models/User"; -import {genericExpressMethod} from "express-request-with-user"; - +import { Request, Response, NextFunction } from "express"; const LocalStrategy = passportLocal.Strategy; const FacebookStrategy = passportFacebook.Strategy; @@ -120,7 +117,7 @@ passport.use(new FacebookStrategy({ /** * Login Required middleware. */ -export const isAuthenticated: genericExpressMethod = (req, res, next) => { +export const isAuthenticated = (req: Request, res: Response, next: NextFunction) => { if (req.isAuthenticated()) { return next(); } @@ -130,10 +127,9 @@ export const isAuthenticated: genericExpressMethod = (req, res, next) => { /** * Authorization Required middleware. */ -export const isAuthorized: genericExpressMethod = (req, res, next) => { +export const isAuthorized = (req: Request, res: Response, next: NextFunction) => { const provider = req.path.split("/").slice(-1)[0]; - // const user = req.user as UserDocument; if (_.find(req.user.tokens, { kind: provider })) { next(); } else { diff --git a/src/controllers/api.ts b/src/controllers/api.ts index 9f4b5fd7..284bd56d 100644 --- a/src/controllers/api.ts +++ b/src/controllers/api.ts @@ -1,6 +1,5 @@ import graph from "fbgraph"; -import { Response, Request } from "express"; -import {genericExpressMethod} from "express-request-with-user"; +import { Response, Request, NextFunction } from "express"; /** @@ -17,8 +16,7 @@ export const getApi = (req: Request, res: Response) => { * GET /api/facebook * Facebook API example. */ -export const getFacebook: genericExpressMethod = (req, res, next) => { - // const user = req.user /*as UserDocument*/; +export const getFacebook = (req: Request, res: Response, next: NextFunction) => { const token = req.user.tokens.find(token => token.kind === "facebook"); graph.setAccessToken(token.accessToken); graph.get(`${req.user.facebook}?fields=id,name,email,first_name,last_name,gender,link,locale,timezone`, (err: Error, results: graph.FacebookUser) => { diff --git a/src/controllers/user.ts b/src/controllers/user.ts index 764babe0..72c716fb 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -2,13 +2,12 @@ import async from "async"; import crypto from "crypto"; import nodemailer from "nodemailer"; import passport from "passport"; -import {AuthToken, User, UserDocument} from "../models/User"; -import {Request, Response} from "express"; -import {IVerifyOptions} from "passport-local"; -import {WriteError} from "mongodb"; -import {check, sanitize, validationResult} from "express-validator"; +import { User, UserDocument, AuthToken } from "../models/User"; +import { Request, Response, NextFunction } from "express"; +import { IVerifyOptions } from "passport-local"; +import { WriteError } from "mongodb"; +import { check, sanitize, validationResult } from "express-validator"; import "../config/passport"; -import {genericExpressMethod} from "express-request-with-user"; /** * GET /login @@ -27,8 +26,7 @@ export const getLogin = (req: Request, res: Response) => { * POST /login * Sign in using email and password. */ - -export const postLogin: genericExpressMethod = (req, res, next) => { +export const postLogin = (req: Request, res: Response, next: NextFunction) => { check("email", "Email is not valid").isEmail(); check("password", "Password cannot be blank").isLength({min: 1}); // eslint-disable-next-line @typescript-eslint/camelcase @@ -81,7 +79,7 @@ export const getSignup = (req: Request, res: Response) => { * POST /signup * Create a new local account. */ -export const postSignup: genericExpressMethod = (req, res, next) => { +export const postSignup = (req: Request, res: Response, next: NextFunction) => { check("email", "Email is not valid").isEmail(); check("password", "Password must be at least 4 characters long").isLength({ min: 4 }); check("confirmPassword", "Passwords do not match").equals(req.body.password); @@ -132,7 +130,7 @@ export const getAccount = (req: Request, res: Response) => { * POST /account/profile * Update profile information. */ -export const postUpdateProfile: genericExpressMethod = (req, res, next) => { +export const postUpdateProfile = (req: Request, res: Response, next: NextFunction) => { check("email", "Please enter a valid email address.").isEmail(); // eslint-disable-next-line @typescript-eslint/camelcase sanitize("email").normalizeEmail({ gmail_remove_dots: false }); @@ -144,7 +142,6 @@ export const postUpdateProfile: genericExpressMethod = (req, res, next) => { return res.redirect("/account"); } - // const user = req.user as UserDocument; User.findById(req.user.id, (err, user) => { if (err) { return next(err); } user.email = req.body.email || ""; @@ -170,7 +167,7 @@ export const postUpdateProfile: genericExpressMethod = (req, res, next) => { * POST /account/password * Update current password. */ -export const postUpdatePassword: genericExpressMethod = (req, res, next) => { +export const postUpdatePassword = (req: Request, res: Response, next: NextFunction) => { check("password", "Password must be at least 4 characters long").isLength({ min: 4 }); check("confirmPassword", "Passwords do not match").equals(req.body.password); @@ -181,7 +178,6 @@ export const postUpdatePassword: genericExpressMethod = (req, res, next) => { return res.redirect("/account"); } - // const user = req.user as UserDocument; User.findById(req.user.id, (err, user) => { if (err) { return next(err); } user.password = req.body.password; @@ -197,8 +193,7 @@ export const postUpdatePassword: genericExpressMethod = (req, res, next) => { * POST /account/delete * Delete user account. */ -export const postDeleteAccount: genericExpressMethod = (req, res, next) => { - // const user = req.user as UserDocument; +export const postDeleteAccount = (req: Request, res: Response, next: NextFunction) => { User.remove({ _id: req.user.id }, (err) => { if (err) { return next(err); } req.logout(); @@ -211,12 +206,10 @@ export const postDeleteAccount: genericExpressMethod = (req, res, next) => { * GET /account/unlink/:provider * Unlink OAuth provider. */ -export const getOauthUnlink: genericExpressMethod = (req, res, next) => { +export const getOauthUnlink = (req: Request, res: Response, next: NextFunction) => { const provider = req.params.provider; - // const user = req.user as UserDocument; User.findById(req.user.id, (err, user) => { if (err) { return next(err); } - // user[provider] = undefined; user.tokens = user.tokens.filter((token: AuthToken) => token.kind !== provider); user.save((err: WriteError) => { if (err) { return next(err); } @@ -230,7 +223,7 @@ export const getOauthUnlink: genericExpressMethod = (req, res, next) => { * GET /reset/:token * Reset Password page. */ -export const getReset: genericExpressMethod = (req, res, next) => { +export const getReset = (req: Request, res: Response, next: NextFunction) => { if (req.isAuthenticated()) { return res.redirect("/"); } @@ -253,7 +246,7 @@ export const getReset: genericExpressMethod = (req, res, next) => { * POST /reset/:token * Process the reset password request. */ -export const postReset: genericExpressMethod = (req, res, next) => { +export const postReset = (req: Request, res: Response, next: NextFunction) => { check("password", "Password must be at least 4 characters long.").isLength({ min: 4 }); check("confirm", "Passwords must match.").equals(req.body.password); @@ -328,7 +321,7 @@ export const getForgot = (req: Request, res: Response) => { * POST /forgot * Create a random token, then the send user an email with a reset link. */ -export const postForgot: genericExpressMethod = (req, res, next) => { +export const postForgot = (req: Request, res: Response, next: NextFunction) => { check("email", "Please enter a valid email address.").isEmail(); // eslint-disable-next-line @typescript-eslint/camelcase sanitize("email").normalizeEmail({ gmail_remove_dots: false }); diff --git a/src/models/User.ts b/src/models/User.ts index 7753a7ce..94cccf0b 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -6,7 +6,6 @@ export type UserDocument = mongoose.Document & { email: string; password: string; passwordResetToken: AuthToken; - // passwordResetToken: string; passwordResetExpires: Date; facebook: string; @@ -22,8 +21,6 @@ export type UserDocument = mongoose.Document & { comparePassword: comparePasswordFunction; gravatar: (size: number) => string; - - // [provider: string]: any; }; type comparePasswordFunction = (candidatePassword: string, cb: (err: Error, isMatch: boolean) => void) => void; diff --git a/src/types/express-request-with-user.d.ts b/src/types/express-request-with-user.d.ts index 20333e73..25051458 100644 --- a/src/types/express-request-with-user.d.ts +++ b/src/types/express-request-with-user.d.ts @@ -1,5 +1,3 @@ -/// - import {UserDocument} from "../models/User"; import {NextFunction, Request, Response} from "express"; @@ -8,7 +6,4 @@ declare module 'express' { export interface Request { user?: User; } -} - -/** To avoid duplication */ -export type genericExpressMethod = (req: Request, res: Response, next: NextFunction) => void \ No newline at end of file +} \ No newline at end of file