Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Removed comments, removed genericExpressMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
GrayStrider committed Oct 1, 2019
1 parent c828b3a commit ba08d21
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 41 deletions.
10 changes: 3 additions & 7 deletions src/config/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand All @@ -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 {
Expand Down
6 changes: 2 additions & 4 deletions src/controllers/api.ts
Original file line number Diff line number Diff line change
@@ -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";


/**
Expand All @@ -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) => {
Expand Down
35 changes: 14 additions & 21 deletions src/controllers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 });
Expand All @@ -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 || "";
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -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); }
Expand All @@ -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("/");
}
Expand All @@ -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);

Expand Down Expand Up @@ -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 });
Expand Down
3 changes: 0 additions & 3 deletions src/models/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export type UserDocument = mongoose.Document & {
email: string;
password: string;
passwordResetToken: AuthToken;
// passwordResetToken: string;
passwordResetExpires: Date;

facebook: string;
Expand All @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions src/types/express-request-with-user.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/// <reference types="express" />

import {UserDocument} from "../models/User";
import {NextFunction, Request, Response} from "express";

Expand All @@ -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
}

0 comments on commit ba08d21

Please sign in to comment.