Skip to content

Commit f57aa93

Browse files
authored
Initial Commit
Backend focused
0 parents  commit f57aa93

File tree

9 files changed

+645
-0
lines changed

9 files changed

+645
-0
lines changed

README.md

Whitespace-only changes.

package-lock.json

Lines changed: 291 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "full-stack-mfa",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "src/index.js",
6+
"type": "module",
7+
"dependencies": {
8+
"passport-local": "^1.0.0",
9+
"passport-strategy": "^1.0.0"
10+
},
11+
"devDependencies": {
12+
"autoprefixer": "^10.4.21",
13+
"postcss": "^8.5.6",
14+
"tailwindcss": "^4.1.11"
15+
},
16+
"scripts": {
17+
"start": "node .",
18+
"dev": "nodemon src/index.js",
19+
"test": "echo \"Error: no test specified\" && exit 1"
20+
},
21+
"keywords": [],
22+
"author": "",
23+
"license": "ISC"
24+
}

src/config/dbConnect.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { connect } from "mongoose";
2+
const dbConnect = async () => {
3+
try {
4+
await connect(process.env.MONGODB_URI);
5+
console.log("MongoDB connected successfully");
6+
} catch (error) {
7+
console.error("MongoDB connection error:", error);
8+
process.exit(1); // You want to "Fail Fast" on connection error to signal process managers like Docker or PM2 that there is a critical error
9+
}
10+
}
11+
12+
export default dbConnect;

src/config/passportConfig.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import passport from "passport";
2+
import { Strategy as LocalStrategy } from "passport-local";
3+
import User from "../models/User.js";
4+
import bcrypt from "bcryptjs";
5+
6+
passport.use(
7+
new LocalStrategy(async (username, password, done) => {
8+
try {
9+
// Find the user by username
10+
const user = await User.findOne({ username });
11+
12+
// If no user is found with that username, fail authentication
13+
if (!user) {
14+
return done(null, false, { message: 'User not found' });
15+
}
16+
17+
// Compare the submitted password with the hashed password in the database
18+
const isMatch = await bcrypt.compare(password, user.password);
19+
20+
if (isMatch) {
21+
// Passwords match. Authentication is successful.
22+
// Pass the user object to the next middleware.
23+
return done(null, user); // CORRECT: Return the user object on a successful match.
24+
} else {
25+
// Passwords do not match. Authentication fails.
26+
return done(null, false, { message: 'Incorrect password.' }); // CORRECT: Return false on a mismatch.
27+
}
28+
} catch (error) {
29+
// If any other error occurs (e.g., database connection issue), pass it along.
30+
return done(error);
31+
}
32+
})
33+
);
34+
35+
passport.serializeUser((user, done) => {
36+
// Serialize the user ID to store in the session
37+
console.log("Serializing user:", user._id);
38+
done(null, user._id);
39+
});
40+
41+
passport.deserializeUser(async (_id, done) => {
42+
try {
43+
// Find the user by ID and return the user object
44+
const user = await User.findById(_id);
45+
done(null, user);
46+
} catch (error) {
47+
// If an error occurs while finding the user, pass it along
48+
done(error);
49+
}
50+
});

0 commit comments

Comments
 (0)