-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add password flow and password reset
- Loading branch information
Showing
18 changed files
with
474 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
import Resend from "@auth/core/providers/resend"; | ||
import { Password } from "@convex-dev/auth/providers/Password"; | ||
import { convexAuth } from "@convex-dev/auth/server"; | ||
import type { MutationCtx } from "./_generated/server"; | ||
import { ResendOTPPasswordReset } from "./passwordReset"; | ||
import { getUserByEmail } from "./users"; | ||
|
||
export const { auth, signIn, signOut, store } = convexAuth({ | ||
providers: [ | ||
Resend({ | ||
apiKey: process.env.AUTH_RESEND_KEY, | ||
from: process.env.AUTH_EMAIL ?? "Namesake <[email protected]>", | ||
}), | ||
], | ||
providers: [Password({ reset: ResendOTPPasswordReset })], | ||
|
||
callbacks: { | ||
async createOrUpdateUser(ctx: MutationCtx, args) { | ||
|
@@ -34,5 +30,9 @@ export const { auth, signIn, signOut, store } = convexAuth({ | |
theme: "system", | ||
}); | ||
}, | ||
|
||
async redirect({ redirectTo }) { | ||
return redirectTo; | ||
}, | ||
}, | ||
}); |
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,23 @@ | ||
import Resend from "@auth/core/providers/resend"; | ||
import { alphabet, generateRandomString } from "oslo/crypto"; | ||
import { Resend as ResendAPI } from "resend"; | ||
|
||
export const ResendOTPPasswordReset = Resend({ | ||
id: "resend-otp", | ||
apiKey: process.env.AUTH_RESEND_KEY, | ||
async generateVerificationToken() { | ||
return generateRandomString(8, alphabet("0-9")); | ||
}, | ||
async sendVerificationRequest({ identifier: email, provider, token }) { | ||
const resend = new ResendAPI(provider.apiKey); | ||
const { error } = await resend.emails.send({ | ||
from: "Namesake <[email protected]>", | ||
to: [email], | ||
subject: "Reset your Namesake password", | ||
text: `Your password reset code is: ${token}`, | ||
}); | ||
if (error) { | ||
throw new Error("Failed to send password reset email"); | ||
} | ||
}, | ||
}); |
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.