Skip to content

Commit

Permalink
Add password flow and password reset
Browse files Browse the repository at this point in the history
  • Loading branch information
evadecker committed Oct 3, 2024
1 parent ac2bfda commit fb1a017
Show file tree
Hide file tree
Showing 18 changed files with 474 additions and 93 deletions.
2 changes: 2 additions & 0 deletions convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type * as constants from "../constants.js";
import type * as forms from "../forms.js";
import type * as helpers from "../helpers.js";
import type * as http from "../http.js";
import type * as passwordReset from "../passwordReset.js";
import type * as questFields from "../questFields.js";
import type * as questSteps from "../questSteps.js";
import type * as quests from "../quests.js";
Expand All @@ -43,6 +44,7 @@ declare const fullApi: ApiFromModules<{
forms: typeof forms;
helpers: typeof helpers;
http: typeof http;
passwordReset: typeof passwordReset;
questFields: typeof questFields;
questSteps: typeof questSteps;
quests: typeof quests;
Expand Down
14 changes: 7 additions & 7 deletions convex/auth.ts
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) {
Expand All @@ -34,5 +30,9 @@ export const { auth, signIn, signOut, store } = convexAuth({
theme: "system",
});
},

async redirect({ redirectTo }) {
return redirectTo;
},
},
});
23 changes: 23 additions & 0 deletions convex/passwordReset.ts
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");
}
},
});
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"convex": "^1.16.3",
"convex-helpers": "^0.1.58",
"next-themes": "^0.3.0",
"oslo": "^1.2.1",
"postcss": "^8.4.47",
"pretty-bytes": "^6.1.1",
"react": "^18.3.1",
Expand All @@ -52,6 +53,7 @@
"react-dom": "^18.3.1",
"react-helmet-async": "^2.0.5",
"react-markdown": "^9.0.1",
"resend": "^4.0.0",
"storybook": "^8.3.4",
"tailwind-variants": "^0.2.1",
"tailwindcss": "^3.4.13",
Expand Down
Loading

0 comments on commit fb1a017

Please sign in to comment.