Last active
October 14, 2021 20:08
-
-
Save m5r/4845f651610bdac9f8071f7c00bb1d64 to your computer and use it in GitHub Desktop.
Magic login link generation and verification
This file contains 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 jwt from "jsonwebtoken"; | |
import { User } from "./entities/user"; | |
export function generateSignInUrl(user: User) { | |
const token = generateSignInToken(user); | |
return `${process.env.APP_URL}/auth/verify?token=${token}`; | |
} | |
function generateSignInToken(user: User) { | |
const payload = { | |
type: "magic", | |
id: user.id, | |
email: user.email, | |
}; | |
return jwt.sign( | |
payload, | |
process.env.JWT_SECRET, | |
{ | |
issuer: "capsulecorp.dev", | |
algorithm: "HS256", | |
expiresIn: "30m", | |
}, | |
); | |
} | |
export function verifyToken(token: string) { | |
return jwt.verify( | |
token, | |
process.env.JWT_SECRET, | |
{ | |
issuer: "capsulecorp.dev", | |
algorithms: ["HS256"], | |
}, | |
) as Pick<User, "id" | "email">; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment