Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Get message type & email confirm redirect #71

Merged
merged 5 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
cr
  • Loading branch information
bracesproul committed Oct 19, 2024
commit 99d0b2ad5248e24cbb7f5e4bbbe46c1fc926d9df
42 changes: 42 additions & 0 deletions src/app/auth/signup/success/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
"use client";

import { useUser } from "@/hooks/useUser";
import { useEffect, useState } from "react";
import { redirect, RedirectType } from "next/navigation";

export default function Page() {
const { getUser, user } = useUser();
const [isChecking, setIsChecking] = useState(true);

useEffect(() => {
if (user) {
return;
}
const startTime = Date.now();
const checkDuration = 3 * 60 * 1000; // 3 minutes in milliseconds
const interval = 4000; // 4 seconds

const checkUser = async () => {
await getUser();
if (Date.now() - startTime >= checkDuration) {
setIsChecking(false);
}
};

const intervalId = setInterval(checkUser, interval);

// Initial check
checkUser();

// Cleanup function
return () => clearInterval(intervalId);
}, [getUser]);

useEffect(() => {
if (user) {
redirect("/", RedirectType.push);
}
}, [user]);

return (
<div className="flex flex-col items-center justify-center min-h-screen bg-gray-100">
<div className="max-w-md w-full bg-white shadow-md rounded-lg p-8 text-center">
Expand All @@ -12,6 +49,11 @@ export default function Page() {
<p className="text-sm text-gray-500">
If you don&apos;t see the email, please check your spam folder.
</p>
{isChecking && (
<p className="text-sm text-blue-500 mt-4">
Waiting for email confirmation...
</p>
)}
</div>
</div>
);
Expand Down
17 changes: 9 additions & 8 deletions src/hooks/useUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@ export function useUser() {
const [user, setUser] = useState<User | null>(null);
const [loading, setLoading] = useState(true);

useEffect(() => {
async function getUser() {
const supabase = createSupabaseClient();

async function getUser() {
const {
data: { user },
} = await supabase.auth.getUser();
setUser(user);
setLoading(false);
}
const {
data: { user },
} = await supabase.auth.getUser();
setUser(user);
setLoading(false);
}

useEffect(() => {
getUser();
}, []);

return {
getUser,
user,
loading,
};
Expand Down
19 changes: 10 additions & 9 deletions src/lib/supabase/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createServerClient } from "@supabase/ssr";
import { NextResponse, type NextRequest } from "next/server";
import { NextRequest, NextResponse } from "next/server";

export async function updateSession(request: NextRequest) {
if (!process.env.NEXT_PUBLIC_SUPABASE_URL) {
Expand Down Expand Up @@ -51,14 +51,15 @@ export async function updateSession(request: NextRequest) {
return NextResponse.redirect(url);
}

if (
user &&
request.nextUrl.pathname.startsWith("/auth") &&
!request.nextUrl.pathname.startsWith("/auth/signout")
) {
// user is logged in, respond by redirecting the user to the home page
const url = new URL("/", request.url);
return NextResponse.redirect(url);
if (user) {
if (
request.nextUrl.pathname.startsWith("/auth") &&
!request.nextUrl.pathname.startsWith("/auth/signout")
) {
// user is logged in, respond by redirecting the user to the home page
const url = new URL("/", request.url);
return NextResponse.redirect(url);
}
}

// IMPORTANT: You *must* return the supabaseResponse object as it is. If you're
Expand Down