本文整理匯總了TypeScript中express.Request.sanitize方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Request.sanitize方法的具體用法?TypeScript Request.sanitize怎麽用?TypeScript Request.sanitize使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類express.Request
的用法示例。
在下文中一共展示了Request.sanitize方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: next
export let 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 });
const errors = req.validationErrors();
if (errors) {
req.flash("errors", errors);
return res.redirect("/signup");
}
const user = new User({
email: req.body.email,
password: req.body.password
});
User.findOne({ email: req.body.email }, (err, existingUser) => {
if (err) { return next(err); }
if (existingUser) {
req.flash("errors", { msg: "Account with that email address already exists." });
return res.redirect("/signup");
}
user.save((err) => {
if (err) { return next(err); }
req.logIn(user, (err) => {
if (err) {
return next(err);
}
res.redirect("/");
});
});
});
};
示例2:
export let 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 });
const errors = req.validationErrors();
if (errors) {
req.flash("errors", errors);
return res.redirect("/login");
}
passport.authenticate("local", (err: Error, user: UserModel, info: IVerifyOptions) => {
if (err) { return next(err); }
if (!user) {
req.flash("errors", info.message);
return res.redirect("/login");
}
req.logIn(user, (err) => {
if (err) { return next(err); }
req.flash("success", { msg: "Success! You are logged in." });
res.redirect(req.session.returnTo || "/");
});
})(req, res, next);
};
示例3: createRandomToken
export let 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 });
const errors = req.validationErrors();
if (errors) {
req.flash("errors", errors);
return res.redirect("/forgot");
}
async.waterfall([
function createRandomToken(done: Function) {
crypto.randomBytes(16, (err, buf) => {
const token = buf.toString("hex");
done(err, token);
});
},
function setRandomToken(token: AuthToken, done: Function) {
User.findOne({ email: req.body.email }, (err, user: any) => {
if (err) { return done(err); }
if (!user) {
req.flash("errors", { msg: "Account with that email address does not exist." });
return res.redirect("/forgot");
}
user.passwordResetToken = token;
user.passwordResetExpires = Date.now() + 3600000; // 1 hour
user.save((err: WriteError) => {
done(err, token, user);
});
});
},
function sendForgotPasswordEmail(token: AuthToken, user: UserModel, done: Function) {
const transporter = nodemailer.createTransport({
service: "SendGrid",
auth: {
user: process.env.SENDGRID_USER,
pass: process.env.SENDGRID_PASSWORD
}
});
const mailOptions = {
to: user.email,
from: "hackathon@starter.com",
subject: "Reset your password on Hackathon Starter",
text: `You are receiving this email because you (or someone else) have requested the reset of the password for your account.\n\n
Please click on the following link, or paste this into your browser to complete the process:\n\n
http://${req.headers.host}/reset/${token}\n\n
If you did not request this, please ignore this email and your password will remain unchanged.\n`
};
transporter.sendMail(mailOptions, (err) => {
req.flash("info", { msg: `An e-mail has been sent to ${user.email} with further instructions.` });
done(err);
});
}
], (err) => {
if (err) { return next(err); }
res.redirect("/forgot");
});
};