mirror of
https://github.com/microsoft/TypeScript-Node-Starter.git
synced 2025-11-09 15:37:27 +00:00
Migrate TSLint to ESLint. Closes #209
This commit rewrites linting support in the project to be based solely on the ESLint as advised by the TSLint tool authors: https://medium.com/palantir/tslint-in-2019-1a144c2317a9 The migration is based on default, recommended settings for TypeScript in ESLint and is expected to be updated in future to better fit project goals. All references has been updated and replaced with relevant ESLint context: - dependencies migration from TSLint to ESLint - VSCode configuration changes to support ESLint exension - VSCode extensions recommendation changes - `.eslintrc` and `.eslintignore` configuration files added - all error level problems in the source files are covered by this migration Thanks!
This commit is contained in:
44
src/app.ts
44
src/app.ts
@@ -29,13 +29,13 @@ const app = express();
|
||||
|
||||
// Connect to MongoDB
|
||||
const mongoUrl = MONGODB_URI;
|
||||
(<any>mongoose).Promise = bluebird;
|
||||
mongoose.Promise = bluebird;
|
||||
|
||||
mongoose.connect(mongoUrl, { useNewUrlParser: true} ).then(
|
||||
() => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ },
|
||||
() => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ },
|
||||
).catch(err => {
|
||||
console.log("MongoDB connection error. Please make sure MongoDB is running. " + err);
|
||||
// process.exit();
|
||||
console.log("MongoDB connection error. Please make sure MongoDB is running. " + err);
|
||||
// process.exit();
|
||||
});
|
||||
|
||||
// Express configuration
|
||||
@@ -46,13 +46,13 @@ app.use(compression());
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(session({
|
||||
resave: true,
|
||||
saveUninitialized: true,
|
||||
secret: SESSION_SECRET,
|
||||
store: new MongoStore({
|
||||
url: mongoUrl,
|
||||
autoReconnect: true
|
||||
})
|
||||
resave: true,
|
||||
saveUninitialized: true,
|
||||
secret: SESSION_SECRET,
|
||||
store: new MongoStore({
|
||||
url: mongoUrl,
|
||||
autoReconnect: true
|
||||
})
|
||||
}));
|
||||
app.use(passport.initialize());
|
||||
app.use(passport.session());
|
||||
@@ -60,26 +60,26 @@ app.use(flash());
|
||||
app.use(lusca.xframe("SAMEORIGIN"));
|
||||
app.use(lusca.xssProtection(true));
|
||||
app.use((req, res, next) => {
|
||||
res.locals.user = req.user;
|
||||
next();
|
||||
res.locals.user = req.user;
|
||||
next();
|
||||
});
|
||||
app.use((req, res, next) => {
|
||||
// After successful login, redirect back to the intended page
|
||||
if (!req.user &&
|
||||
// After successful login, redirect back to the intended page
|
||||
if (!req.user &&
|
||||
req.path !== "/login" &&
|
||||
req.path !== "/signup" &&
|
||||
!req.path.match(/^\/auth/) &&
|
||||
!req.path.match(/\./)) {
|
||||
req.session.returnTo = req.path;
|
||||
} else if (req.user &&
|
||||
req.session.returnTo = req.path;
|
||||
} else if (req.user &&
|
||||
req.path == "/account") {
|
||||
req.session.returnTo = req.path;
|
||||
}
|
||||
next();
|
||||
req.session.returnTo = req.path;
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
app.use(
|
||||
express.static(path.join(__dirname, "public"), { maxAge: 31557600000 })
|
||||
express.static(path.join(__dirname, "public"), { maxAge: 31557600000 })
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -114,7 +114,7 @@ app.get("/api/facebook", passportConfig.isAuthenticated, passportConfig.isAuthor
|
||||
*/
|
||||
app.get("/auth/facebook", passport.authenticate("facebook", { scope: ["email", "public_profile"] }));
|
||||
app.get("/auth/facebook/callback", passport.authenticate("facebook", { failureRedirect: "/login" }), (req, res) => {
|
||||
res.redirect(req.session.returnTo || "/");
|
||||
res.redirect(req.session.returnTo || "/");
|
||||
});
|
||||
|
||||
export default app;
|
||||
|
||||
@@ -11,13 +11,13 @@ const LocalStrategy = passportLocal.Strategy;
|
||||
const FacebookStrategy = passportFacebook.Strategy;
|
||||
|
||||
passport.serializeUser<any, any>((user, done) => {
|
||||
done(undefined, user.id);
|
||||
done(undefined, user.id);
|
||||
});
|
||||
|
||||
passport.deserializeUser((id, done) => {
|
||||
User.findById(id, (err, user) => {
|
||||
done(err, user);
|
||||
});
|
||||
User.findById(id, (err, user) => {
|
||||
done(err, user);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -25,19 +25,19 @@ passport.deserializeUser((id, done) => {
|
||||
* Sign in using Email and Password.
|
||||
*/
|
||||
passport.use(new LocalStrategy({ usernameField: "email" }, (email, password, done) => {
|
||||
User.findOne({ email: email.toLowerCase() }, (err, user: any) => {
|
||||
if (err) { return done(err); }
|
||||
if (!user) {
|
||||
return done(undefined, false, { message: `Email ${email} not found.` });
|
||||
}
|
||||
user.comparePassword(password, (err: Error, isMatch: boolean) => {
|
||||
if (err) { return done(err); }
|
||||
if (isMatch) {
|
||||
return done(undefined, user);
|
||||
}
|
||||
return done(undefined, false, { message: "Invalid email or password." });
|
||||
User.findOne({ email: email.toLowerCase() }, (err, user: any) => {
|
||||
if (err) { return done(err); }
|
||||
if (!user) {
|
||||
return done(undefined, false, { message: `Email ${email} not found.` });
|
||||
}
|
||||
user.comparePassword(password, (err: Error, isMatch: boolean) => {
|
||||
if (err) { return done(err); }
|
||||
if (isMatch) {
|
||||
return done(undefined, user);
|
||||
}
|
||||
return done(undefined, false, { message: "Invalid email or password." });
|
||||
});
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
|
||||
@@ -61,81 +61,81 @@ passport.use(new LocalStrategy({ usernameField: "email" }, (email, password, don
|
||||
* Sign in with Facebook.
|
||||
*/
|
||||
passport.use(new FacebookStrategy({
|
||||
clientID: process.env.FACEBOOK_ID,
|
||||
clientSecret: process.env.FACEBOOK_SECRET,
|
||||
callbackURL: "/auth/facebook/callback",
|
||||
profileFields: ["name", "email", "link", "locale", "timezone"],
|
||||
passReqToCallback: true
|
||||
clientID: process.env.FACEBOOK_ID,
|
||||
clientSecret: process.env.FACEBOOK_SECRET,
|
||||
callbackURL: "/auth/facebook/callback",
|
||||
profileFields: ["name", "email", "link", "locale", "timezone"],
|
||||
passReqToCallback: true
|
||||
}, (req: any, accessToken, refreshToken, profile, done) => {
|
||||
if (req.user) {
|
||||
User.findOne({ facebook: profile.id }, (err, existingUser) => {
|
||||
if (err) { return done(err); }
|
||||
if (existingUser) {
|
||||
req.flash("errors", { msg: "There is already a Facebook account that belongs to you. Sign in with that account or delete it, then link it with your current account." });
|
||||
done(err);
|
||||
} else {
|
||||
User.findById(req.user.id, (err, user: any) => {
|
||||
if (err) { return done(err); }
|
||||
user.facebook = profile.id;
|
||||
user.tokens.push({ kind: "facebook", accessToken });
|
||||
user.profile.name = user.profile.name || `${profile.name.givenName} ${profile.name.familyName}`;
|
||||
user.profile.gender = user.profile.gender || profile._json.gender;
|
||||
user.profile.picture = user.profile.picture || `https://graph.facebook.com/${profile.id}/picture?type=large`;
|
||||
user.save((err: Error) => {
|
||||
req.flash("info", { msg: "Facebook account has been linked." });
|
||||
done(err, user);
|
||||
});
|
||||
if (req.user) {
|
||||
User.findOne({ facebook: profile.id }, (err, existingUser) => {
|
||||
if (err) { return done(err); }
|
||||
if (existingUser) {
|
||||
req.flash("errors", { msg: "There is already a Facebook account that belongs to you. Sign in with that account or delete it, then link it with your current account." });
|
||||
done(err);
|
||||
} else {
|
||||
User.findById(req.user.id, (err, user: any) => {
|
||||
if (err) { return done(err); }
|
||||
user.facebook = profile.id;
|
||||
user.tokens.push({ kind: "facebook", accessToken });
|
||||
user.profile.name = user.profile.name || `${profile.name.givenName} ${profile.name.familyName}`;
|
||||
user.profile.gender = user.profile.gender || profile._json.gender;
|
||||
user.profile.picture = user.profile.picture || `https://graph.facebook.com/${profile.id}/picture?type=large`;
|
||||
user.save((err: Error) => {
|
||||
req.flash("info", { msg: "Facebook account has been linked." });
|
||||
done(err, user);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
} else {
|
||||
User.findOne({ facebook: profile.id }, (err, existingUser) => {
|
||||
if (err) { return done(err); }
|
||||
if (existingUser) {
|
||||
return done(undefined, existingUser);
|
||||
}
|
||||
User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
|
||||
if (err) { return done(err); }
|
||||
if (existingEmailUser) {
|
||||
req.flash("errors", { msg: "There is already an account using this email address. Sign in to that account and link it with Facebook manually from Account Settings." });
|
||||
done(err);
|
||||
} else {
|
||||
const user: any = new User();
|
||||
user.email = profile._json.email;
|
||||
user.facebook = profile.id;
|
||||
user.tokens.push({ kind: "facebook", accessToken });
|
||||
user.profile.name = `${profile.name.givenName} ${profile.name.familyName}`;
|
||||
user.profile.gender = profile._json.gender;
|
||||
user.profile.picture = `https://graph.facebook.com/${profile.id}/picture?type=large`;
|
||||
user.profile.location = (profile._json.location) ? profile._json.location.name : "";
|
||||
user.save((err: Error) => {
|
||||
done(err, user);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
} else {
|
||||
User.findOne({ facebook: profile.id }, (err, existingUser) => {
|
||||
if (err) { return done(err); }
|
||||
if (existingUser) {
|
||||
return done(undefined, existingUser);
|
||||
}
|
||||
User.findOne({ email: profile._json.email }, (err, existingEmailUser) => {
|
||||
if (err) { return done(err); }
|
||||
if (existingEmailUser) {
|
||||
req.flash("errors", { msg: "There is already an account using this email address. Sign in to that account and link it with Facebook manually from Account Settings." });
|
||||
done(err);
|
||||
} else {
|
||||
const user: any = new User();
|
||||
user.email = profile._json.email;
|
||||
user.facebook = profile.id;
|
||||
user.tokens.push({ kind: "facebook", accessToken });
|
||||
user.profile.name = `${profile.name.givenName} ${profile.name.familyName}`;
|
||||
user.profile.gender = profile._json.gender;
|
||||
user.profile.picture = `https://graph.facebook.com/${profile.id}/picture?type=large`;
|
||||
user.profile.location = (profile._json.location) ? profile._json.location.name : "";
|
||||
user.save((err: Error) => {
|
||||
done(err, user);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}));
|
||||
|
||||
/**
|
||||
* Login Required middleware.
|
||||
*/
|
||||
export const isAuthenticated = (req: Request, res: Response, next: NextFunction) => {
|
||||
if (req.isAuthenticated()) {
|
||||
return next();
|
||||
}
|
||||
res.redirect("/login");
|
||||
if (req.isAuthenticated()) {
|
||||
return next();
|
||||
}
|
||||
res.redirect("/login");
|
||||
};
|
||||
|
||||
/**
|
||||
* Authorization Required middleware.
|
||||
*/
|
||||
export const isAuthorized = (req: Request, res: Response, next: NextFunction) => {
|
||||
const provider = req.path.split("/").slice(-1)[0];
|
||||
const provider = req.path.split("/").slice(-1)[0];
|
||||
|
||||
if (_.find(req.user.tokens, { kind: provider })) {
|
||||
next();
|
||||
} else {
|
||||
res.redirect(`/auth/${provider}`);
|
||||
}
|
||||
if (_.find(req.user.tokens, { kind: provider })) {
|
||||
next();
|
||||
} else {
|
||||
res.redirect(`/auth/${provider}`);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -9,9 +9,9 @@ import { Response, Request, NextFunction } from "express";
|
||||
* List of API examples.
|
||||
*/
|
||||
export const getApi = (req: Request, res: Response) => {
|
||||
res.render("api/index", {
|
||||
title: "API Examples"
|
||||
});
|
||||
res.render("api/index", {
|
||||
title: "API Examples"
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -19,13 +19,13 @@ export const getApi = (req: Request, res: Response) => {
|
||||
* Facebook API example.
|
||||
*/
|
||||
export const getFacebook = (req: Request, res: Response, next: NextFunction) => {
|
||||
const token = req.user.tokens.find((token: any) => 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) => {
|
||||
if (err) { return next(err); }
|
||||
res.render("api/facebook", {
|
||||
title: "Facebook API",
|
||||
profile: results
|
||||
const token = req.user.tokens.find((token: any) => 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) => {
|
||||
if (err) { return next(err); }
|
||||
res.render("api/facebook", {
|
||||
title: "Facebook API",
|
||||
profile: results
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -3,11 +3,11 @@ import { Request, Response } from "express";
|
||||
import { check, validationResult } from "express-validator";
|
||||
|
||||
const transporter = nodemailer.createTransport({
|
||||
service: "SendGrid",
|
||||
auth: {
|
||||
user: process.env.SENDGRID_USER,
|
||||
pass: process.env.SENDGRID_PASSWORD
|
||||
}
|
||||
service: "SendGrid",
|
||||
auth: {
|
||||
user: process.env.SENDGRID_USER,
|
||||
pass: process.env.SENDGRID_PASSWORD
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -15,9 +15,9 @@ const transporter = nodemailer.createTransport({
|
||||
* Contact form page.
|
||||
*/
|
||||
export const getContact = (req: Request, res: Response) => {
|
||||
res.render("contact", {
|
||||
title: "Contact"
|
||||
});
|
||||
res.render("contact", {
|
||||
title: "Contact"
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -25,30 +25,30 @@ export const getContact = (req: Request, res: Response) => {
|
||||
* Send a contact form via Nodemailer.
|
||||
*/
|
||||
export const postContact = (req: Request, res: Response) => {
|
||||
check("name", "Name cannot be blank").not().isEmpty();
|
||||
check("email", "Email is not valid").isEmail();
|
||||
check("message", "Message cannot be blank").not().isEmpty();
|
||||
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 = validationResult(req);
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/contact");
|
||||
}
|
||||
|
||||
const mailOptions = {
|
||||
to: "your@email.com",
|
||||
from: `${req.body.name} <${req.body.email}>`,
|
||||
subject: "Contact Form",
|
||||
text: req.body.message
|
||||
};
|
||||
|
||||
transporter.sendMail(mailOptions, (err) => {
|
||||
if (err) {
|
||||
req.flash("errors", { msg: err.message });
|
||||
return res.redirect("/contact");
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/contact");
|
||||
}
|
||||
req.flash("success", { msg: "Email has been sent successfully!" });
|
||||
res.redirect("/contact");
|
||||
});
|
||||
|
||||
const mailOptions = {
|
||||
to: "your@email.com",
|
||||
from: `${req.body.name} <${req.body.email}>`,
|
||||
subject: "Contact Form",
|
||||
text: req.body.message
|
||||
};
|
||||
|
||||
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");
|
||||
});
|
||||
};
|
||||
|
||||
@@ -5,7 +5,7 @@ import { Request, Response } from "express";
|
||||
* Home page.
|
||||
*/
|
||||
export const index = (req: Request, res: Response) => {
|
||||
res.render("home", {
|
||||
title: "Home"
|
||||
});
|
||||
res.render("home", {
|
||||
title: "Home"
|
||||
});
|
||||
};
|
||||
|
||||
@@ -14,12 +14,12 @@ import "../config/passport";
|
||||
* Login page.
|
||||
*/
|
||||
export const getLogin = (req: Request, res: Response) => {
|
||||
if (req.user) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
res.render("account/login", {
|
||||
title: "Login"
|
||||
});
|
||||
if (req.user) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
res.render("account/login", {
|
||||
title: "Login"
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -27,29 +27,30 @@ export const getLogin = (req: Request, res: Response) => {
|
||||
* Sign in using email and password.
|
||||
*/
|
||||
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});
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
check("email", "Email is not valid").isEmail();
|
||||
check("password", "Password cannot be blank").isLength({min: 1});
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = validationResult(req);
|
||||
const errors = validationResult(req);
|
||||
|
||||
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", {msg: info.message});
|
||||
return res.redirect("/login");
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
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);
|
||||
|
||||
passport.authenticate("local", (err: Error, user: UserDocument, info: IVerifyOptions) => {
|
||||
if (err) { return next(err); }
|
||||
if (!user) {
|
||||
req.flash("errors", {msg: 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);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -57,8 +58,8 @@ export const postLogin = (req: Request, res: Response, next: NextFunction) => {
|
||||
* Log out.
|
||||
*/
|
||||
export const logout = (req: Request, res: Response) => {
|
||||
req.logout();
|
||||
res.redirect("/");
|
||||
req.logout();
|
||||
res.redirect("/");
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -66,12 +67,12 @@ export const logout = (req: Request, res: Response) => {
|
||||
* Signup page.
|
||||
*/
|
||||
export const getSignup = (req: Request, res: Response) => {
|
||||
if (req.user) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
res.render("account/signup", {
|
||||
title: "Create Account"
|
||||
});
|
||||
if (req.user) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
res.render("account/signup", {
|
||||
title: "Create Account"
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -79,39 +80,40 @@ export const getSignup = (req: Request, res: Response) => {
|
||||
* Create a new local account.
|
||||
*/
|
||||
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);
|
||||
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);
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = validationResult(req);
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
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");
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/signup");
|
||||
}
|
||||
user.save((err) => {
|
||||
if (err) { return next(err); }
|
||||
req.logIn(user, (err) => {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
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("/");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -119,9 +121,9 @@ export const postSignup = (req: Request, res: Response, next: NextFunction) => {
|
||||
* Profile page.
|
||||
*/
|
||||
export const getAccount = (req: Request, res: Response) => {
|
||||
res.render("account/profile", {
|
||||
title: "Account Management"
|
||||
});
|
||||
res.render("account/profile", {
|
||||
title: "Account Management"
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -129,35 +131,36 @@ export const getAccount = (req: Request, res: Response) => {
|
||||
* Update profile information.
|
||||
*/
|
||||
export const postUpdateProfile = (req: Request, res: Response, next: NextFunction) => {
|
||||
check("email", "Please enter a valid email address.").isEmail();
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
check("email", "Please enter a valid email address.").isEmail();
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = validationResult(req);
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/account");
|
||||
}
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/account");
|
||||
}
|
||||
|
||||
User.findById(req.user.id, (err, user: UserDocument) => {
|
||||
if (err) { return next(err); }
|
||||
user.email = req.body.email || "";
|
||||
user.profile.name = req.body.name || "";
|
||||
user.profile.gender = req.body.gender || "";
|
||||
user.profile.location = req.body.location || "";
|
||||
user.profile.website = req.body.website || "";
|
||||
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");
|
||||
User.findById(req.user.id, (err, user: UserDocument) => {
|
||||
if (err) { return next(err); }
|
||||
user.email = req.body.email || "";
|
||||
user.profile.name = req.body.name || "";
|
||||
user.profile.gender = req.body.gender || "";
|
||||
user.profile.location = req.body.location || "";
|
||||
user.profile.website = req.body.website || "";
|
||||
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");
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -165,25 +168,25 @@ export const postUpdateProfile = (req: Request, res: Response, next: NextFunctio
|
||||
* Update current password.
|
||||
*/
|
||||
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);
|
||||
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 = validationResult(req);
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/account");
|
||||
}
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/account");
|
||||
}
|
||||
|
||||
User.findById(req.user.id, (err, user: UserDocument) => {
|
||||
if (err) { return next(err); }
|
||||
user.password = req.body.password;
|
||||
user.save((err: WriteError) => {
|
||||
if (err) { return next(err); }
|
||||
req.flash("success", { msg: "Password has been changed." });
|
||||
res.redirect("/account");
|
||||
User.findById(req.user.id, (err, user: UserDocument) => {
|
||||
if (err) { return next(err); }
|
||||
user.password = req.body.password;
|
||||
user.save((err: WriteError) => {
|
||||
if (err) { return next(err); }
|
||||
req.flash("success", { msg: "Password has been changed." });
|
||||
res.redirect("/account");
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -191,12 +194,12 @@ export const postUpdatePassword = (req: Request, res: Response, next: NextFuncti
|
||||
* Delete user account.
|
||||
*/
|
||||
export const postDeleteAccount = (req: Request, res: Response, next: NextFunction) => {
|
||||
User.remove({ _id: req.user.id }, (err) => {
|
||||
if (err) { return next(err); }
|
||||
req.logout();
|
||||
req.flash("info", { msg: "Your account has been deleted." });
|
||||
res.redirect("/");
|
||||
});
|
||||
User.remove({ _id: req.user.id }, (err) => {
|
||||
if (err) { return next(err); }
|
||||
req.logout();
|
||||
req.flash("info", { msg: "Your account has been deleted." });
|
||||
res.redirect("/");
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -204,17 +207,17 @@ export const postDeleteAccount = (req: Request, res: Response, next: NextFunctio
|
||||
* Unlink OAuth provider.
|
||||
*/
|
||||
export const getOauthUnlink = (req: Request, res: Response, next: NextFunction) => {
|
||||
const provider = req.params.provider;
|
||||
User.findById(req.user.id, (err, user: any) => {
|
||||
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); }
|
||||
req.flash("info", { msg: `${provider} account has been unlinked.` });
|
||||
res.redirect("/account");
|
||||
const provider = req.params.provider;
|
||||
User.findById(req.user.id, (err, user: any) => {
|
||||
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); }
|
||||
req.flash("info", { msg: `${provider} account has been unlinked.` });
|
||||
res.redirect("/account");
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -222,22 +225,22 @@ export const getOauthUnlink = (req: Request, res: Response, next: NextFunction)
|
||||
* Reset Password page.
|
||||
*/
|
||||
export const getReset = (req: Request, res: Response, next: NextFunction) => {
|
||||
if (req.isAuthenticated()) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
User
|
||||
.findOne({ passwordResetToken: req.params.token })
|
||||
.where("passwordResetExpires").gt(Date.now())
|
||||
.exec((err, user) => {
|
||||
if (err) { return next(err); }
|
||||
if (!user) {
|
||||
req.flash("errors", { msg: "Password reset token is invalid or has expired." });
|
||||
return res.redirect("/forgot");
|
||||
}
|
||||
res.render("account/reset", {
|
||||
title: "Password Reset"
|
||||
});
|
||||
});
|
||||
if (req.isAuthenticated()) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
User
|
||||
.findOne({ passwordResetToken: req.params.token })
|
||||
.where("passwordResetExpires").gt(Date.now())
|
||||
.exec((err, user) => {
|
||||
if (err) { return next(err); }
|
||||
if (!user) {
|
||||
req.flash("errors", { msg: "Password reset token is invalid or has expired." });
|
||||
return res.redirect("/forgot");
|
||||
}
|
||||
res.render("account/reset", {
|
||||
title: "Password Reset"
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -245,61 +248,61 @@ export const getReset = (req: Request, res: Response, next: NextFunction) => {
|
||||
* Process the reset password request.
|
||||
*/
|
||||
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);
|
||||
check("password", "Password must be at least 4 characters long.").isLength({ min: 4 });
|
||||
check("confirm", "Passwords must match.").equals(req.body.password);
|
||||
|
||||
const errors = validationResult(req);
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
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: UserDocument, 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);
|
||||
});
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("back");
|
||||
}
|
||||
], (err) => {
|
||||
if (err) { return next(err); }
|
||||
res.redirect("/");
|
||||
});
|
||||
|
||||
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: UserDocument, 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("/");
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -307,12 +310,12 @@ export const postReset = (req: Request, res: Response, next: NextFunction) => {
|
||||
* Forgot Password page.
|
||||
*/
|
||||
export const getForgot = (req: Request, res: Response) => {
|
||||
if (req.isAuthenticated()) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
res.render("account/forgot", {
|
||||
title: "Forgot Password"
|
||||
});
|
||||
if (req.isAuthenticated()) {
|
||||
return res.redirect("/");
|
||||
}
|
||||
res.render("account/forgot", {
|
||||
title: "Forgot Password"
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -320,61 +323,62 @@ 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) => {
|
||||
check("email", "Please enter a valid email address.").isEmail();
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
check("email", "Please enter a valid email address.").isEmail();
|
||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||
sanitize("email").normalizeEmail({ gmail_remove_dots: false });
|
||||
|
||||
const errors = validationResult(req);
|
||||
const errors = validationResult(req);
|
||||
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
return res.redirect("/forgot");
|
||||
}
|
||||
if (!errors.isEmpty()) {
|
||||
req.flash("errors", errors.array());
|
||||
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: UserDocument, 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
|
||||
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: UserDocument, 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");
|
||||
});
|
||||
};
|
||||
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");
|
||||
});
|
||||
};
|
||||
|
||||
@@ -3,73 +3,73 @@ import crypto from "crypto";
|
||||
import mongoose from "mongoose";
|
||||
|
||||
export type UserDocument = mongoose.Document & {
|
||||
email: string,
|
||||
password: string,
|
||||
passwordResetToken: string,
|
||||
passwordResetExpires: Date,
|
||||
email: string;
|
||||
password: string;
|
||||
passwordResetToken: string;
|
||||
passwordResetExpires: Date;
|
||||
|
||||
facebook: string,
|
||||
tokens: AuthToken[],
|
||||
facebook: string;
|
||||
tokens: AuthToken[];
|
||||
|
||||
profile: {
|
||||
name: string,
|
||||
gender: string,
|
||||
location: string,
|
||||
website: string,
|
||||
picture: string
|
||||
},
|
||||
profile: {
|
||||
name: string;
|
||||
gender: string;
|
||||
location: string;
|
||||
website: string;
|
||||
picture: string;
|
||||
};
|
||||
|
||||
comparePassword: comparePasswordFunction,
|
||||
gravatar: (size: number) => string
|
||||
comparePassword: comparePasswordFunction;
|
||||
gravatar: (size: number) => string;
|
||||
};
|
||||
|
||||
type comparePasswordFunction = (candidatePassword: string, cb: (err: any, isMatch: any) => {}) => void;
|
||||
|
||||
export type AuthToken = {
|
||||
accessToken: string,
|
||||
kind: string
|
||||
};
|
||||
export interface AuthToken {
|
||||
accessToken: string;
|
||||
kind: string;
|
||||
}
|
||||
|
||||
const userSchema = new mongoose.Schema({
|
||||
email: { type: String, unique: true },
|
||||
password: String,
|
||||
passwordResetToken: String,
|
||||
passwordResetExpires: Date,
|
||||
email: { type: String, unique: true },
|
||||
password: String,
|
||||
passwordResetToken: String,
|
||||
passwordResetExpires: Date,
|
||||
|
||||
facebook: String,
|
||||
twitter: String,
|
||||
google: String,
|
||||
tokens: Array,
|
||||
facebook: String,
|
||||
twitter: String,
|
||||
google: String,
|
||||
tokens: Array,
|
||||
|
||||
profile: {
|
||||
name: String,
|
||||
gender: String,
|
||||
location: String,
|
||||
website: String,
|
||||
picture: String
|
||||
}
|
||||
profile: {
|
||||
name: String,
|
||||
gender: String,
|
||||
location: String,
|
||||
website: String,
|
||||
picture: String
|
||||
}
|
||||
}, { timestamps: true });
|
||||
|
||||
/**
|
||||
* Password hash middleware.
|
||||
*/
|
||||
userSchema.pre("save", function save(next) {
|
||||
const user = this as UserDocument;
|
||||
if (!user.isModified("password")) { return next(); }
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
if (err) { return next(err); }
|
||||
bcrypt.hash(user.password, salt, undefined, (err: mongoose.Error, hash) => {
|
||||
if (err) { return next(err); }
|
||||
user.password = hash;
|
||||
next();
|
||||
const user = this as UserDocument;
|
||||
if (!user.isModified("password")) { return next(); }
|
||||
bcrypt.genSalt(10, (err, salt) => {
|
||||
if (err) { return next(err); }
|
||||
bcrypt.hash(user.password, salt, undefined, (err: mongoose.Error, hash) => {
|
||||
if (err) { return next(err); }
|
||||
user.password = hash;
|
||||
next();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const comparePassword: comparePasswordFunction = function (candidatePassword, cb) {
|
||||
bcrypt.compare(candidatePassword, this.password, (err: mongoose.Error, isMatch: boolean) => {
|
||||
cb(err, isMatch);
|
||||
});
|
||||
bcrypt.compare(candidatePassword, this.password, (err: mongoose.Error, isMatch: boolean) => {
|
||||
cb(err, isMatch);
|
||||
});
|
||||
};
|
||||
|
||||
userSchema.methods.comparePassword = comparePassword;
|
||||
@@ -78,11 +78,11 @@ userSchema.methods.comparePassword = comparePassword;
|
||||
* Helper method for getting user's gravatar.
|
||||
*/
|
||||
userSchema.methods.gravatar = function (size: number = 200) {
|
||||
if (!this.email) {
|
||||
return `https://gravatar.com/avatar/?s=${size}&d=retro`;
|
||||
}
|
||||
const md5 = crypto.createHash("md5").update(this.email).digest("hex");
|
||||
return `https://gravatar.com/avatar/${md5}?s=${size}&d=retro`;
|
||||
if (!this.email) {
|
||||
return `https://gravatar.com/avatar/?s=${size}&d=retro`;
|
||||
}
|
||||
const md5 = crypto.createHash("md5").update(this.email).digest("hex");
|
||||
return `https://gravatar.com/avatar/${md5}?s=${size}&d=retro`;
|
||||
};
|
||||
|
||||
export const User = mongoose.model<UserDocument>("User", userSchema);
|
||||
|
||||
@@ -11,12 +11,12 @@ app.use(errorHandler());
|
||||
* Start Express server.
|
||||
*/
|
||||
const server = app.listen(app.get("port"), () => {
|
||||
console.log(
|
||||
" App is running at http://localhost:%d in %s mode",
|
||||
app.get("port"),
|
||||
app.get("env")
|
||||
);
|
||||
console.log(" Press CTRL-C to stop\n");
|
||||
console.log(
|
||||
" App is running at http://localhost:%d in %s mode",
|
||||
app.get("port"),
|
||||
app.get("env")
|
||||
);
|
||||
console.log(" Press CTRL-C to stop\n");
|
||||
});
|
||||
|
||||
export default server;
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { Logger, LoggerOptions, transports } from "winston";
|
||||
|
||||
const options: LoggerOptions = {
|
||||
transports: [
|
||||
new transports.Console({
|
||||
level: process.env.NODE_ENV === "production" ? "error" : "debug"
|
||||
}),
|
||||
new transports.File({ filename: "debug.log", level: "debug" })
|
||||
]
|
||||
transports: [
|
||||
new transports.Console({
|
||||
level: process.env.NODE_ENV === "production" ? "error" : "debug"
|
||||
}),
|
||||
new transports.File({ filename: "debug.log", level: "debug" })
|
||||
]
|
||||
};
|
||||
|
||||
const logger = new Logger(options);
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
logger.debug("Logging initialized at debug level");
|
||||
logger.debug("Logging initialized at debug level");
|
||||
}
|
||||
|
||||
export default logger;
|
||||
|
||||
Reference in New Issue
Block a user