Last active
November 28, 2020 15:13
-
-
Save mirsahib/4997650d2f0e840877562745a9354ad4 to your computer and use it in GitHub Desktop.
React_Serverless
This file contains hidden or 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
import cookie from "cookie"; | |
import jwt from "jsonwebtoken"; | |
export async function handler(event) { | |
const cookies = event.headers.cookie && cookie.parse(event.headers.cookie); | |
if (!cookies || !cookies.jwt) { | |
return { | |
statusCode: 401, | |
body: JSON.stringify({ | |
auth: false, | |
msg: "Missing Authorization token", | |
}), | |
}; | |
} | |
try { | |
const verified = jwt.verify(cookies.jwt, process.env.REACT_APP_JWT_TOKEN); | |
if (!verified) { | |
return { | |
statusCode: 401, | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ | |
auth: false, | |
msg: "Invalid Authorization token", | |
}), | |
}; | |
} else { | |
return { | |
statusCode: 200, | |
headers: { | |
"Content-Type": "application/json", | |
}, | |
body: JSON.stringify({ auth: true }), | |
}; | |
} | |
} catch (err) { | |
return { | |
statusCode: 401, | |
body: JSON.stringify({ msg: err.message }), | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment