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

replace firebase authentication with jwt #3

Open
wants to merge 2 commits into
base: master
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
24 changes: 24 additions & 0 deletions JWT/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const jwt = require("jsonwebtoken");
const config = require("../config");

const jwtToken = { auth: {} };

// Function to generate JWT token
const generateToken = (payload) => {
return jwt.sign(payload, config.get("jwt_secret"), { expiresIn: "1h" });
};

// Function to verify JWT token
const verifyToken = (token) => {
try {
return jwt.verify(token, config.get("jwt_secret"));
} catch (err) {
console.error("Token verification failed:", err);
return null;
}
};

jwtToken.generateToken = generateToken;
jwtToken.verifyToken = verifyToken;

module.exports = jwtToken;
27 changes: 0 additions & 27 deletions firebase/index.js

This file was deleted.

69 changes: 49 additions & 20 deletions middleware/authenticateUser.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
const { auth } = require("../firebase");
const jwt = require("jsonwebtoken");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should make use of the JWT/index.js instead of re doing the same flow again

const config = require("../config");

const authenticateUser = async (req, res, next) => {
// Function to generate JWT token
const generateToken = (payload) => {
return jwt.sign(payload, config.get("jwt_secret"), { expiresIn: "1h" });
};

// Function to verify JWT token
const verifyToken = (token) => {
Comment on lines +4 to +10
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should make use of the JWT/index.js instead of

try {
if (config.get("env") === "test_remote") next();
const headerToken = req.headers.authorization;
if (!headerToken) {
return res.status(401).send({ message: "No token provided" });
}

if (headerToken && headerToken.split(" ")[0] !== "Bearer") {
res.status(401).send({ message: "Invalid token" });
}

let token = headerToken.split(" ")[1];
await auth.verifyIdToken(token);

next();
} catch (error) {
req.log.error(error);
res.status(403).send({ message: "Could not authorize" });
return jwt.verify(token, config.get("jwt_secret"));
} catch (err) {
console.error("Token verification failed:", err);
return null;
}
};
Comment on lines +12 to +17
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should make use of the JWT/index.js instead of


// Example usage in authenticateUser function
const authenticateUser = (req, res) => {
const { username, password } = req.body;

// Replace this with your user authentication logic
const user = authenticateWithDatabase(username, password);

if (user) {
const token = generateToken({ id: user.id, username: user.username });
res.json({ token });
} else {
res.status(401).json({ message: "Authentication failed" });
Comment on lines +19 to +30
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these example should be part of the README or docs (which doesn't exist right now) so we can have it in the user controller instead

}
};

module.exports = authenticateUser;
// Example middleware to protect routes
const authenticateMiddleware = (req, res, next) => {
const token = req.headers["authorization"];

if (!token) {
return res.status(403).json({ message: "No token provided" });
}

const decoded = verifyToken(token);

if (!decoded) {
return res.status(401).json({ message: "Failed to authenticate token" });
}

req.user = decoded;
next();
};

module.exports = {
authenticateUser,
authenticateMiddleware,
};
Comment on lines +34 to +55
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Loading