Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First commit #302

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions final_project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,48 @@ app.use("/customer",session({secret:"fingerprint_customer",resave: true, saveUni

app.use("/customer/auth/*", function auth(req,res,next){
//Write the authenication mechanism here
// Check if user is logged in and has valid access token
if (req.session.authorization) {
let token = req.session.authorization['accessToken'];
// Verify JWT token
jwt.verify(token, "access", (err, user) => {
if (!err) {
req.user = user;
next(); // Proceed to the next middleware
} else {
return res.status(403).json({ message: "User not authenticated" });
}
});
} else {
return res.status(403).json({ message: "User not logged in" });
}
});

// Login endpoint
app.post("/login", (req, res) => {
const username = req.body.username;
const password = req.body.password;
// Check if username or password is missing
if (!username || !password) {
return res.status(404).json({ message: "Error logging in" });
}
// Authenticate user
if (authenticatedUser(username, password)) {
// Generate JWT access token
let accessToken = jwt.sign({
data: password
}, 'access', { expiresIn: 60 * 60 });
// Store access token and username in session
req.session.authorization = {
accessToken, username
}
return res.status(200).send("User successfully logged in");
} else {
return res.status(208).json({ message: "Invalid Login. Check username and password" });
}
});



const PORT =5000;

Expand Down
Loading