Last active
January 22, 2023 23:11
-
-
Save johnforte/dce1cdf7a0fe3ee74afa509da59c7eaf to your computer and use it in GitHub Desktop.
A magic Auth Next Server Side Wrapper
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 {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