-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,55 @@ | ||
const { auth } = require("../firebase"); | ||
const jwt = require("jsonwebtoken"); | ||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should make use of the |
||
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should make use of the |
||
|
||
// 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same |
There was a problem hiding this comment.
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