mirror of
https://github.com/microsoft/TypeScript-Node-Starter.git
synced 2025-11-08 14:47:28 +00:00
Update express-validator usage to new version
This commit is contained in:
@@ -9,7 +9,6 @@ import flash from "express-flash";
|
||||
import path from "path";
|
||||
import mongoose from "mongoose";
|
||||
import passport from "passport";
|
||||
import expressValidator from "express-validator";
|
||||
import bluebird from "bluebird";
|
||||
import { MONGODB_URI, SESSION_SECRET } from "./util/secrets";
|
||||
|
||||
@@ -49,7 +48,6 @@ app.set("view engine", "pug");
|
||||
app.use(compression());
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(expressValidator());
|
||||
app.use(session({
|
||||
resave: true,
|
||||
saveUninitialized: true,
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import nodemailer from "nodemailer";
|
||||
import { Request, Response } from "express";
|
||||
import { check, validationResult } from "express-validator";
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: "SendGrid",
|
||||
@@ -24,14 +25,14 @@ export const getContact = (req: Request, res: Response) => {
|
||||
* Send a contact form via Nodemailer.
|
||||
*/
|
||||
export const postContact = (req: Request, res: Response) => {
|
||||
req.assert("name", "Name cannot be blank").notEmpty();
|
||||
req.assert("email", "Email is not valid").isEmail();
|
||||
req.assert("message", "Message cannot be blank").notEmpty();
|
||||
check("name", "Name cannot be blank").not().isEmpty();
|
||||
check("email", "Email is not valid").isEmail();
|
||||
check("message", "Message cannot be blank").not().isEmpty();
|
||||
|
||||
const errors = req.validationErrors();
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (errors) {
|
||||
req.flash("errors", errors);
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/contact");
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import { User, UserDocument, AuthToken } from "../models/User";
|
||||
import { Request, Response, NextFunction } from "express";
|
||||
import { IVerifyOptions } from "passport-local";
|
||||
import { WriteError } from "mongodb";
|
||||
import request from "express-validator";
|
||||
import { check, sanitize, validationResult } from "express-validator";
|
||||
import "../config/passport";
|
||||
|
||||
/**
|
||||
@@ -27,21 +27,21 @@ export const getLogin = (req: Request, res: Response) => {
|
||||
* Sign in using email and password.
|
||||
*/
|
||||
export const postLogin = (req: Request, res: Response, next: NextFunction) => {
|
||||
req.assert("email", "Email is not valid").isEmail();
|
||||
req.assert("password", "Password cannot be blank").notEmpty();
|
||||
req.sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
check("email", "Email is not valid").isEmail();
|
||||
check("password", "Password cannot be blank").isLength({min: 1});
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = req.validationErrors();
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (errors) {
|
||||
req.flash("errors", errors);
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/login");
|
||||
}
|
||||
|
||||
passport.authenticate("local", (err: Error, user: UserDocument, info: IVerifyOptions) => {
|
||||
if (err) { return next(err); }
|
||||
if (!user) {
|
||||
req.flash("errors", info.message);
|
||||
req.flash("errors", {msg: info.message});
|
||||
return res.redirect("/login");
|
||||
}
|
||||
req.logIn(user, (err) => {
|
||||
@@ -79,15 +79,15 @@ export const getSignup = (req: Request, res: Response) => {
|
||||
* Create a new local account.
|
||||
*/
|
||||
export const postSignup = (req: Request, res: Response, next: NextFunction) => {
|
||||
req.assert("email", "Email is not valid").isEmail();
|
||||
req.assert("password", "Password must be at least 4 characters long").len({ min: 4 });
|
||||
req.assert("confirmPassword", "Passwords do not match").equals(req.body.password);
|
||||
req.sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
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);
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = req.validationErrors();
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (errors) {
|
||||
req.flash("errors", errors);
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/signup");
|
||||
}
|
||||
|
||||
@@ -129,13 +129,13 @@ export const getAccount = (req: Request, res: Response) => {
|
||||
* Update profile information.
|
||||
*/
|
||||
export const postUpdateProfile = (req: Request, res: Response, next: NextFunction) => {
|
||||
req.assert("email", "Please enter a valid email address.").isEmail();
|
||||
req.sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
check("email", "Please enter a valid email address.").isEmail();
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = req.validationErrors();
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (errors) {
|
||||
req.flash("errors", errors);
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/account");
|
||||
}
|
||||
|
||||
@@ -165,13 +165,13 @@ export const postUpdateProfile = (req: Request, res: Response, next: NextFunctio
|
||||
* Update current password.
|
||||
*/
|
||||
export const postUpdatePassword = (req: Request, res: Response, next: NextFunction) => {
|
||||
req.assert("password", "Password must be at least 4 characters long").len({ min: 4 });
|
||||
req.assert("confirmPassword", "Passwords do not match").equals(req.body.password);
|
||||
check("password", "Password must be at least 4 characters long").isLength({ min: 4 });
|
||||
check("confirmPassword", "Passwords do not match").equals(req.body.password);
|
||||
|
||||
const errors = req.validationErrors();
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (errors) {
|
||||
req.flash("errors", errors);
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/account");
|
||||
}
|
||||
|
||||
@@ -245,13 +245,13 @@ export const getReset = (req: Request, res: Response, next: NextFunction) => {
|
||||
* Process the reset password request.
|
||||
*/
|
||||
export const postReset = (req: Request, res: Response, next: NextFunction) => {
|
||||
req.assert("password", "Password must be at least 4 characters long.").len({ min: 4 });
|
||||
req.assert("confirm", "Passwords must match.").equals(req.body.password);
|
||||
check("password", "Password must be at least 4 characters long.").isLength({ min: 4 });
|
||||
check("confirm", "Passwords must match.").equals(req.body.password);
|
||||
|
||||
const errors = req.validationErrors();
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (errors) {
|
||||
req.flash("errors", errors);
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("back");
|
||||
}
|
||||
|
||||
@@ -320,13 +320,13 @@ export const getForgot = (req: Request, res: Response) => {
|
||||
* Create a random token, then the send user an email with a reset link.
|
||||
*/
|
||||
export const postForgot = (req: Request, res: Response, next: NextFunction) => {
|
||||
req.assert("email", "Please enter a valid email address.").isEmail();
|
||||
req.sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
check("email", "Please enter a valid email address.").isEmail();
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = req.validationErrors();
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (errors) {
|
||||
req.flash("errors", errors);
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/forgot");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user