Skip to content

Instantly share code, notes, and snippets.

@Kernix13
Created March 30, 2024 18:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kernix13/d28d08cf2e4fe23afcec00614fcfbb9d to your computer and use it in GitHub Desktop.
Save Kernix13/d28d08cf2e4fe23afcec00614fcfbb9d to your computer and use it in GitHub Desktop.
Set server cookie
// npm install cookie
const cookie = require('cookie');
const handler = async (event) => {
const body = JSON.parse(event.body);
// pick a generic username and password for now
if (body.username == "some_name" && body.password == "some_password") {
const myCookie = cookie.serialize("petadoption", "apsodifugyhtjrkelwqzmxncbv0918273645", {
httpOnly: true,
path: '/',
sameSite: 'strict',
maxAge: 60 * 60 * 24 // set for 24 hours
})
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Set-Cookie': myCookie,
'Location': '/'
},
body: JSON.stringify({ success: true })
};
}
return {
statusCode: 200,
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({ success: false })
};
};
module.exports = { handler };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment