Skip to content

Instantly share code, notes, and snippets.

@johnforte
Last active January 22, 2023 23:11
Show Gist options
  • Select an option

  • Save johnforte/dce1cdf7a0fe3ee74afa509da59c7eaf to your computer and use it in GitHub Desktop.

Select an option

Save johnforte/dce1cdf7a0fe3ee74afa509da59c7eaf to your computer and use it in GitHub Desktop.
A magic Auth Next Server Side Wrapper
import {Magic as MagicAdmin} from "@magic-sdk/admin";
const magic = new MagicAdmin(process.env.MAGIC_SECRET_KEY);
export const withMagicAuth = (notAuthUrl:string, func: Function): Function =>{
return async function(context){
const {__auth} = context.req.cookies;
try {
if (__auth) {
// Magic Validator throws an error when not valid but just void if valid.
await magic.token.validate(__auth);
const user = await magic.users.getMetadataByToken(__auth);
context.user = user;
return await func(context);
}
}catch(e){
console.log(e);
}
return {
redirect: {
destination: notAuthUrl,
permanent: false,
},
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment