Skip to content

Commit

Permalink
auth fix
Browse files Browse the repository at this point in the history
  • Loading branch information
armintalaie committed Sep 18, 2024
1 parent e2ca7c2 commit 9f5efd6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/app/portal/admin/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Unauthorized } from "@/components/layouts/inaccessiblePageWrapper";
export default function Layout({ children }: { children: React.ReactNode }) {
const { user, userMetadata } = useContext(userContext);
if (!user) {
redirect("/portal/auth");
redirect("/auth");
}
if (
!userMetadata ||
Expand Down
2 changes: 1 addition & 1 deletion src/app/portal/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default async function Layout({
const { data, error } = await supabase.auth.getUser();

if (!data.user || error) {
redirect("/portal/auth");
redirect("/auth");
}

const userMetadata = await getUserMetadata(data.user.id);
Expand Down
81 changes: 43 additions & 38 deletions src/lib/utils/supabase/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,58 @@ export async function updateSession(request: NextRequest) {

let supabaseResponse = NextResponse.next({
request,
// headers,
});

const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) =>
request.cookies.set(name, value),
);
supabaseResponse = NextResponse.next({
request,
});
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options),
);
},
},
},
);

// check origin URL, if the same base URL, allow the request
if (request.nextUrl.origin.startsWith(process.env.NEXT_PUBLIC_BASE_URL!)) {
return supabaseResponse;
}
// // check origin URL, if the same base URL, allow the request
// if (request.nextUrl.origin.startsWith(process.env.NEXT_PUBLIC_BASE_URL!)) {
// return supabaseResponse;
// }

// IMPORTANT: Avoid writing any logic between createServerClient and
// supabase.auth.getUser(). A simple mistake could make it very hard to debug
// issues with users being randomly logged out.

const {
data: { user },
} = await supabase.auth.getUser();
try {
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) =>
request.cookies.set(name, value),
);
supabaseResponse = NextResponse.next({
request,
});
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options),
);
},
},
},
);
const {
data: { user },
} = await supabase.auth.getUser();

if (
!user &&
!request.nextUrl.pathname.startsWith("/login") &&
!request.nextUrl.pathname.startsWith("/auth")
) {
// no user, potentially respond by redirecting the user to the login page
if (
!user &&
!request.nextUrl.pathname.startsWith("/login") &&
!request.nextUrl.pathname.startsWith("/auth")
) {
// no user, potentially respond by redirecting the user to the login page
const url = request.nextUrl.clone();
url.pathname = "/auth";
return NextResponse.redirect(url);
}
} catch (error) {
console.error("Error fetching user", error);
const url = request.nextUrl.clone();
url.pathname = "/auth";
url.pathname = "/";
return NextResponse.redirect(url);
}

Expand Down

0 comments on commit 9f5efd6

Please sign in to comment.