Skip to content

Instantly share code, notes, and snippets.

@mirsahib
Last active November 28, 2020 15:13
Show Gist options
  • Save mirsahib/4997650d2f0e840877562745a9354ad4 to your computer and use it in GitHub Desktop.
Save mirsahib/4997650d2f0e840877562745a9354ad4 to your computer and use it in GitHub Desktop.
React_Serverless
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