Playing with Netlify Identity
07 Dec 2020
http://my-site.netlify.com/.netlify/functions/helloNetlify
// /functions/helloNetlify.js
// Begin HTTP-GET handler
exports.handler = async (event, context) => {
// "clientContext" is the magic of turning on "Identity" in Netlify -- all function calls from Netlify-hosted pages w/ the "widget" in them have it
const { user } = context.clientContext;
const roles = user ? user.app_metadata.roles : false;
// Begin bad-login short-circuit
if ( !roles || !roles.some((role) => ['fammy'].includes(role)) ) { // PRODUCTION LINE
//if (roles) { // DEBUG LINE ONLY
return {
statusCode: 402,
body: JSON.stringify({
message: `This content requires authentication.`,
}),
};
} // End bad-login short-circuit
// Begin returning secret content
return {
statusCode: 200,
body: JSON.stringify({
message: `HELLO, FRIEND OR FAMILY`,
}),
}; // End returning secret content
}; // End HTTP-GET handler