本文整理汇总了TypeScript中express.Request.flash方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Request.flash方法的具体用法?TypeScript Request.flash怎么用?TypeScript Request.flash使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类express.Request
的用法示例。
在下文中一共展示了Request.flash方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1:
transporter.sendMail(mailOptions, (err) => {
if (err) {
req.flash("errors", { msg: err.message });
return res.redirect("/contact");
}
req.flash("success", { msg: "Email has been sent successfully!" });
res.redirect("/contact");
});
示例2: next
user.save((err: WriteError) => {
if (err) {
if (err.code === 11000) {
req.flash("errors", { msg: "The email address you have entered is already associated with an account." });
return res.redirect("/account");
}
return next(err);
}
req.flash("success", { msg: "Profile information has been updated." });
res.redirect("/account");
});
示例3: User
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("/");
});
});
});
};
示例4:
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);
};
示例5: 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");
});
};
示例6: resetPassword
export let 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);
const errors = req.validationErrors();
if (errors) {
req.flash("errors", errors);
return res.redirect("back");
}
async.waterfall([
function resetPassword(done: Function) {
User
.findOne({ passwordResetToken: req.params.token })
.where("passwordResetExpires").gt(Date.now())
.exec((err, user: any) => {
if (err) { return next(err); }
if (!user) {
req.flash("errors", { msg: "Password reset token is invalid or has expired." });
return res.redirect("back");
}
user.password = req.body.password;
user.passwordResetToken = undefined;
user.passwordResetExpires = undefined;
user.save((err: WriteError) => {
if (err) { return next(err); }
req.logIn(user, (err) => {
done(err, user);
});
});
});
},
function sendResetPasswordEmail(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: "express-ts@starter.com",
subject: "Your password has been changed",
text: `Hello,\n\nThis is a confirmation that the password for your account ${user.email} has just been changed.\n`
};
transporter.sendMail(mailOptions, (err) => {
req.flash("success", { msg: "Success! Your password has been changed." });
done(err);
});
}
], (err) => {
if (err) { return next(err); }
res.redirect("/");
});
};
示例7: done
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);
});
});