Created
November 28, 2020 15:15
-
-
Save mirsahib/c305cd2f4e9083fd342405701e31bc39 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({ | |
| msg: "Unauthorized,JWT cookie is missing", | |
| }), | |
| }; | |
| } | |
| try { | |
| const payload = jwt.verify(cookies.jwt, process.env.REACT_APP_JWT_TOKEN); | |
| return { | |
| statusCode: 200, | |
| headers: { | |
| "Content-Type": "application/json", | |
| }, | |
| body: JSON.stringify({ userId: payload.userId, email: payload.email }), | |
| }; | |
| } 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