Skip to content

Commit

Permalink
fix reset password
Browse files Browse the repository at this point in the history
  • Loading branch information
jordienr committed Jun 11, 2024
1 parent 44c6290 commit 463bd5e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
32 changes: 9 additions & 23 deletions apps/web/app/auth/confirm/route.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,32 @@
import { createServerClient, type CookieOptions } from "@supabase/ssr";
import { type EmailOtpType } from "@supabase/supabase-js";
import { cookies } from "next/headers";
import { NextRequest, NextResponse } from "next/server";
import { createClient } from "app/supa";
import { type NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url);
const token_hash = searchParams.get("token_hash");
const type = searchParams.get("type") as EmailOtpType | null;
const next = searchParams.get("next") ?? "/";

const redirectTo = request.nextUrl.clone();
redirectTo.pathname = next;
redirectTo.searchParams.delete("token_hash");
redirectTo.searchParams.delete("type");

if (token_hash && type) {
const cookieStore = cookies();
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return cookieStore.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
cookieStore.set({ name, value, ...options });
},
remove(name: string, options: CookieOptions) {
cookieStore.delete({ name, ...options });
},
},
}
);
const supabase = createClient();

const { error } = await supabase.auth.verifyOtp({
type,
token_hash,
});
if (!error) {
return NextResponse.redirect(redirectTo);
redirectTo.searchParams.delete("next");
return NextResponse.redirect(next);
}
}

// return the user to an error page with some instructions
redirectTo.pathname = "/auth/auth-code-error";
redirectTo.pathname = "/error";
return NextResponse.redirect(redirectTo);
}
14 changes: 12 additions & 2 deletions apps/web/src/pages/reset-password-confirmation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { getSupabaseBrowserClient } from "@/lib/supabase";
import { useRouter } from "next/router";
import { useState } from "react";
import { useEffect, useState } from "react";
import { toast } from "sonner";

export default function ResetPasswordConfirmation() {
const [loading, setLoading] = useState(false);
const supabase = getSupabaseBrowserClient();
const router = useRouter();

useEffect(() => {
supabase.auth.getUser().then((res) => {
if (!res.data.user) {
router.push("/sign-in");
}
});
}, []);

async function onSubmit(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();

Expand All @@ -31,7 +40,8 @@ export default function ResetPasswordConfirmation() {
}

alert("Password updated");
window.location.pathname = "/sign-in";
toast.success("Password updated");
router.push("/blogs");
setLoading(false);
}

Expand Down

0 comments on commit 463bd5e

Please sign in to comment.