path = ('^' path (strict ? '' : path[path.length - 1] === '/' ? '?' : '/?'))
Any ideas? Thanks in advance for anyone that helps. I have a project due in 6 days so really need help
server.js
import express from 'express';
import { routes } from '../routes/index';
import { initializeDbConnection } from './db';
const PORT = process.env.PORT || 8080;
const app = express();
//This allows access the body of POST/PUT requests in our route handlers as req.body
app.use(express.json());
//Add all the routes to Express server exported from routes/index.js
routes.forEach(route => {
app[route.method](route.path, route.handler);
});
// Connect to the database, then start the server.
// This prevents from having to create a new DB
// connection for every request
initializeDbConnection()
.then(() => {
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});
});
index.js
import { signUpRoute } from './signUpRoute';
import { testRoute } from "./testRoute";
import { logInRoute } from "./logInRoute"
import { updateUserInfoRoute } from './updateUserInfoRoute';
import { verifyEmailRoute } from './verifyEmailRoute';
import { forgotPasswordRoute } from './forgotPasswordRoute';
import { resetPasswordRoute } from './resetPasswordRoute';
import { getGoogleOauthUrlRoute } from './getGoogleOauthUrlRoute';
import { googleOauthCallbackRoute } from './googleOauthCallbackRoute';
// import { testEmailRoute } from './testEmailRoute';
export const routes = [
getGoogleOauthUrlRoute,
googleOauthCallbackRoute,
resetPasswordRoute,
forgotPasswordRoute,
logInRoute,
signUpRoute,
// testEmailRoute,
verifyEmailRoute,
testRoute,
updateUserInfoRoute,
];
This is the code for index.js These are the routes for the backend. Thanks everyone for the help. I still am pretty green on the backend of things so I hope this helps.
signuproutes.js
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { v4 as uuid } from 'uuid';
import { getDbConnection } from '../src/db';
import { sendEmail } from '../src/util/sendEmail';
export const signUpRoute = {
method: 'post',
handler: async (req, res) => {
const { email, password } = req.body;
// Make sure there's no existing user with that email - no verification yet
const db = getDbConnection('react-auth-db');
const user = await db.collection('users').findOne({ email });
if (user) {
return res.sendStatus(409); // 409 is the "conflict" error code
}
// Generate the salt first
const salt = uuid();
const pepper = process.env.PEPPER_STRING;
// Encrypt the password
const passwordHash = await bcrypt.hash(salt password pepper, 10);
const verificationString = uuid();
// Store email and password hash in database (i.e. create user) - you'll also want to get the id
const startingInfo = {
hairColor: '',
favoriteFood: '',
bio: '',
}
const result = await db.collection('users').insertOne({
email,
isVerified: false,
verificationString,
passwordHash,
salt,
info: startingInfo,
});
const { insertedId } = result;
try {
await sendEmail({
to: email,
from: '[email protected]',
subject: 'Please verify your email',
text: `
Thanks for signing up! To verify your email, you just need to click the link below:
http://localhost:3000/verify-email/${verificationString}
`
});
} catch (e) {
console.log(e);
throw new Error(e);
}
jwt.sign({
id: insertedId,
isVerified: false,
email, // Do NOT send the salt back to the user
info: startingInfo,
},
process.env.JWT_SECRET,
{
expiresIn: '2d',
},
(err, token) => {
if (err) {
return res.status(500).send(err);
}
return res.status(200).send({ token });
});
},
};
loginroute.js
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken';
import { getDbConnection } from '../src/db';
export const logInRoute = {
path: '/api/login',
method: 'post',
handler: async (req, res) => {
const { email, password } = req.body;
const db = getDbConnection('react-auth-db');
const user = await db.collection('users').findOne({ email });
if (!user) return res.sendStatus(401);
// There is no user with that email. We want to return the same
// status code so that people can't fish around for emails to see
// who has an account and who doesn't
const { _id: id, isVerified, passwordHash, salt, info } = user;
const pepper = process.env.PEPPER_STRING;
const isCorrect = await bcrypt.compare(salt password pepper, passwordHash);
if (isCorrect) {
jwt.sign({ id, isVerified, email, info }, process.env.JWT_SECRET, { expiresIn: '2d' }, (err, token) => {
if (err) {
return res.sendStatus(500)
}
res.status(200).send({ token });
});
} else {
res.sendStatus(401); // This is the "not authenticated" status code - remember it!
}
}
}
CodePudding user response: