Created
July 11, 2023 07:25
-
-
Save Tribhuwan-Joshi/5b3ff70ad6bab024791aae93e132b3c4 to your computer and use it in GitHub Desktop.
Authentication passport
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require("express"); | |
const path = require("path"); | |
const session = require("express-session"); | |
const passport = require("passport"); | |
const LocalStrategy = require("passport-local").Strategy; | |
const mongoose = require("mongoose"); | |
const User = require("./models/user"); | |
const { | |
getUser, | |
logOut, | |
getHome, | |
signUp, | |
logIn, | |
} = require("./controllers/auth"); | |
async function main() { | |
await mongoose.connect( | |
"mongodb+srv://tjsm:[email protected]/?retryWrites=true&w=majority", | |
{ | |
useNewUrlParser: true, | |
useUnifiedTopology: true, | |
} | |
); | |
} | |
main().catch((err) => console.error(err)); | |
const db = mongoose.connection; | |
db.on("error", console.error.bind(console, "mongo connection error")); | |
const app = express(); | |
app.set("views", path.join(__dirname, "views")); | |
app.set("view engine", "pug"); | |
app.use(session({ secret: "cats", resave: false, saveUninitialized: true })); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
passport.use( | |
new LocalStrategy(async (username, password, done) => { | |
try { | |
const user = await User.findOne({ username: username }); | |
if (!user) { | |
console.log("user don't exist"); | |
return done(null, false, { message: "Incorrect username" }); | |
} | |
console.log("password and hash password is", password, user.password); | |
bcrypt.compare(password, user.password, (err, res) => { | |
if (res) { | |
// passwords match! log user in | |
console.log("password match"); | |
return done(null, user); | |
} else { | |
// passwords do not match! | |
console.log("password don't match"); | |
return done(null, false, { message: "Incorrect password" }); | |
} | |
}); | |
} catch (err) { | |
return done(err); | |
} | |
}) | |
); | |
passport.serializeUser(function (user, done) { | |
done(null, user.id); | |
}); | |
passport.deserializeUser(async function (id, done) { | |
try { | |
const user = await User.findById(id); | |
done(null, user); | |
} catch (err) { | |
done(err); | |
} | |
}); | |
// Put local users | |
app.use(function (req, res, next) { | |
res.locals.user = req.user; | |
next(); | |
}); | |
app.use(express.urlencoded({ extended: false })); | |
app.get("/", getHome); | |
app.post("/signup", signUp); | |
app.post("/login", logIn); | |
app.post("/logout", logOut); | |
app.get("/users/:id", getUser); | |
app.listen(3000, () => console.log("server running")); | |
exports.User = User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const passport = require("passport"); | |
const bcrypt = require("bcryptjs"); | |
const User = require("../models/user"); | |
exports.getUser = (req, res) => { | |
res.render("user"); | |
}; | |
exports.logOut = (req, res, next) => { | |
req.logout(function (err) { | |
if (err) { | |
return next(err); | |
} | |
res.redirect("/"); | |
}); | |
}; | |
exports.logIn = passport.authenticate("local", { | |
successRedirect: "/", | |
failureRedirect: "/", | |
}); | |
exports.signUp = async (req, res, next) => { | |
try { | |
const user = new User({ | |
username: req.body.username, | |
password: req.body.password, | |
}); | |
const existUser = await User.find({ username: user.username }); | |
if (existUser.length) { | |
return res.render("index", { | |
title: "Main", | |
errors: ["username already used"], | |
}); | |
} | |
bcrypt.hash(user.password, 10, async (err, hashedPassword) => { | |
if (err) next(err); | |
user.password = hashedPassword; | |
await user.save(); | |
req.login(user, function (err) { | |
if (err) { | |
return next(err); | |
} | |
return res.redirect("/users/" + req.user._id); | |
}); | |
}); | |
} catch (err) { | |
console.log(err); | |
} | |
}; | |
exports.getHome = async (req, res, next) => { | |
try { | |
const user = new User({ | |
username: req.body.username, | |
password: req.body.password, | |
}); | |
const existUser = await User.find({ username: user.username }); | |
if (existUser.length) { | |
return res.render("index", { | |
title: "Main", | |
errors: ["username already used"], | |
}); | |
} | |
bcrypt.hash(user.password, 10, async (err, hashedPassword) => { | |
if (err) next(err); | |
user.password = hashedPassword; | |
await user.save(); | |
req.login(user, function (err) { | |
if (err) { | |
return next(err); | |
} | |
return res.redirect("/users/" + req.user._id); | |
}); | |
}); | |
} catch (err) { | |
console.log(err); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require("mongoose"); | |
const Schema = mongoose.Schema; | |
const User = mongoose.model( | |
"User", | |
new Schema({ | |
username: { type: String, required: true }, | |
password: { type: String, required: true }, | |
}) | |
); | |
module.exports = User; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment