generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from agiledev-students-fall2023/passport_jwt
Passport jwt
- Loading branch information
Showing
6 changed files
with
47 additions
and
32 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import passportJWT from "passport-jwt"; | ||
import User from "../models/User.mjs"; | ||
|
||
const ExtractJwt = passportJWT.ExtractJwt | ||
const JwtStrategy = passportJWT.ExtractJwt | ||
|
||
// how the token is extracted and verified from the request | ||
const jwtOptions = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme("jwt"), //fromAuthHeaderAsBearerToken() | ||
secretOrKey: process.env.JWT_SECRET, | ||
} | ||
|
||
const jwtVerifyToken = async function (jwt_payload, done) { | ||
console.log("JWT payload received", jwt_payload) // debugging | ||
|
||
// token expiration | ||
|
||
// match user in database | ||
try { | ||
const user = await User.findOne({ uuid: jwt_payload.uuid }) | ||
if (!user) throw {jwtMessage: "user not found"} | ||
return done(null, user) | ||
|
||
} catch (error) { | ||
return done(null, false, {message: error.jwtMessage}) | ||
} | ||
} | ||
|
||
const CustomJwtStrategy = () => { | ||
return new JwtStrategy(jwtOptions, jwtVerifyToken) | ||
} | ||
|
||
export default CustomJwtStrategy |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ const AccountEdit = (props) => { | |
const storedUserData = JSON.parse(localStorage.getItem('user') || '{}'); | ||
const [username, setUsername] = useState(storedUserData.name || 'John Doe'); | ||
const [email, setEmail] = useState(storedUserData.email || '[email protected]'); | ||
// console.log(storedUserData.email) | ||
|
||
// Set username and email on the screen | ||
useEffect(() => { | ||
|
@@ -63,12 +62,7 @@ const AccountEdit = (props) => { | |
|
||
if(response?.data?.user){ | ||
setUsername(response.data.user.name); | ||
const userData = { | ||
uuid: response.data.user.uuid, | ||
name: response.data.user.name, | ||
email: response.data.user.email | ||
}; | ||
localStorage.setItem('user', JSON.stringify(userData)) | ||
localStorage.setItem('user', JSON.stringify(response.data.user)) | ||
// localStorage.setItem('username', response.data.user.name); | ||
|
||
}else{ | ||
|
@@ -100,12 +94,7 @@ const AccountEdit = (props) => { | |
|
||
if(response?.data?.user){ | ||
setEmail(response.data.user.email); | ||
const userData = { | ||
uuid: response.data.user.uuid, | ||
name: response.data.user.name, | ||
email: response.data.user.email | ||
}; | ||
localStorage.setItem('user', JSON.stringify(userData)) | ||
localStorage.setItem('user', JSON.stringify(response.data.user)) | ||
}else{ | ||
console.log("Error!!!!!"); | ||
} | ||
|
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