-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from moinulmoin/magic-link
feat: ✨ magic link login using lucia v3
- Loading branch information
Showing
24 changed files
with
366 additions
and
94 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library"; | ||
import { cookies } from "next/headers"; | ||
import { type NextRequest } from "next/server"; | ||
import { isWithinExpirationDate } from "oslo"; | ||
import db from "~/lib/db"; | ||
import { lucia } from "~/lib/lucia"; | ||
|
||
export const GET = async (req: NextRequest) => { | ||
const url = new URL(req.url); | ||
const verificationToken = url.searchParams.get("token"); | ||
|
||
if (!verificationToken) { | ||
return new Response("Invalid token", { | ||
status: 400, | ||
}); | ||
} | ||
|
||
try { | ||
const [token] = await db.$transaction([ | ||
db.emailVerificationToken.findFirst({ | ||
where: { | ||
id: verificationToken!, | ||
}, | ||
}), | ||
db.emailVerificationToken.delete({ | ||
where: { | ||
id: verificationToken!, | ||
}, | ||
}), | ||
]); | ||
|
||
if (!token || !isWithinExpirationDate(token.expiresAt)) { | ||
return new Response("Token expired", { | ||
status: 400, | ||
}); | ||
} | ||
const user = await db.user.findFirst({ | ||
where: { | ||
id: token.userId, | ||
}, | ||
}); | ||
if (!user || user.email !== token.email) { | ||
return new Response("Invalid token", { | ||
status: 400, | ||
}); | ||
} | ||
|
||
await lucia.invalidateUserSessions(user.id); | ||
await db.user.upsert({ | ||
where: { | ||
id: user.id, | ||
emailVerified: false, | ||
}, | ||
update: { | ||
emailVerified: true, | ||
}, | ||
create: {}, | ||
}); | ||
|
||
const session = await lucia.createSession(user.id, {}); | ||
const sessionCookie = lucia.createSessionCookie(session.id); | ||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes | ||
); | ||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: "/dashboard", | ||
}, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
if (error instanceof PrismaClientKnownRequestError) { | ||
if ( | ||
error.code === "P2025" || | ||
error.meta?.cause === "Record to delete does not exist." | ||
) { | ||
return new Response("Token already used", { | ||
status: 404, | ||
}); | ||
} | ||
} | ||
|
||
return new Response("Something went wrong.", { | ||
status: 500, | ||
}); | ||
} | ||
}; |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import db from "~/lib/db"; | ||
import { sendVerificationEmail } from "~/lib/resend"; | ||
import { appUrl } from "~/lib/utils"; | ||
import { createEmailVerificationToken } from "~/server/auth"; | ||
|
||
export const POST = async (req: Request) => { | ||
const body = await req.json(); | ||
|
||
try { | ||
const user = await db.user.upsert({ | ||
where: { | ||
email: body.email, | ||
}, | ||
update: {}, | ||
create: { | ||
email: body.email, | ||
emailVerified: false, | ||
}, | ||
}); | ||
|
||
const verificationToken = await createEmailVerificationToken( | ||
user.id, | ||
body.email | ||
); | ||
const verificationUrl = | ||
appUrl + "/api/auth/email-verify?token=" + verificationToken; | ||
await sendVerificationEmail({ | ||
toMail: body.email, | ||
verificationUrl, | ||
userName: user.name?.split(" ")[0] || "", | ||
}); | ||
|
||
return new Response(null, { | ||
status: 200, | ||
}); | ||
} catch (error) { | ||
console.log(error); | ||
|
||
return new Response(null, { | ||
status: 500, | ||
}); | ||
} | ||
}; |
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
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
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
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
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
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
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
Oops, something went wrong.
aa1f509
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
chadnext – ./
chadnext-git-main-moinulmoin.vercel.app
chadnext.vercel.app
chadnext-moinulmoin.vercel.app
chadnext.moinulmoin.com