setting up files
This commit is contained in:
43
lib/auth-helpers.ts
Normal file
43
lib/auth-helpers.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
import { createClient } from "@/lib/supabase/server";
|
||||
import { routes } from "@/lib/routes";
|
||||
import type { User } from "@supabase/supabase-js";
|
||||
|
||||
export function hasCompletedOnboarding(user: User) {
|
||||
return user.user_metadata?.onboarding_completed === true;
|
||||
}
|
||||
|
||||
export async function getSessionUser() {
|
||||
const supabase = await createClient();
|
||||
const {
|
||||
data: { user },
|
||||
error,
|
||||
} = await supabase.auth.getUser();
|
||||
|
||||
if (error || !user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
export async function requireSessionUser() {
|
||||
const user = await getSessionUser();
|
||||
|
||||
if (!user) {
|
||||
redirect(routes.login);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
export async function requireOnboardingComplete() {
|
||||
const user = await requireSessionUser();
|
||||
|
||||
if (!hasCompletedOnboarding(user)) {
|
||||
redirect(routes.onboardingQuiz);
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
28
lib/routes.ts
Normal file
28
lib/routes.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
export const routes = {
|
||||
home: "/",
|
||||
login: "/auth/login",
|
||||
signUp: "/auth/sign-up",
|
||||
signUpSuccess: "/auth/sign-up-success",
|
||||
onboardingQuiz: "/onboarding/quiz",
|
||||
avatar: "/avatar",
|
||||
dailyQuiz: "/daily/quiz",
|
||||
shop: "/shop",
|
||||
profile: "/profile",
|
||||
} as const;
|
||||
|
||||
export type AppRoute = (typeof routes)[keyof typeof routes];
|
||||
|
||||
export const appNavItems = [
|
||||
{ href: routes.avatar, label: "Pet", shortLabel: "Pet" },
|
||||
{ href: routes.dailyQuiz, label: "Daily Quiz", shortLabel: "Quiz" },
|
||||
{ href: routes.shop, label: "Shop", shortLabel: "Shop" },
|
||||
{ href: routes.profile, label: "Profile", shortLabel: "Profile" },
|
||||
] as const;
|
||||
|
||||
export function isAuthPath(pathname: string) {
|
||||
return pathname.startsWith("/auth");
|
||||
}
|
||||
|
||||
export function isPublicPath(pathname: string) {
|
||||
return pathname === routes.home || isAuthPath(pathname);
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
import { createServerClient } from "@supabase/ssr";
|
||||
import { NextResponse, type NextRequest } from "next/server";
|
||||
import { hasEnvVars } from "../utils";
|
||||
import { isPublicPath, routes } from "@/lib/routes";
|
||||
import { getSupabaseEnv } from "@/lib/supabase/env";
|
||||
import { hasEnvVars } from "../utils";
|
||||
|
||||
export async function updateSession(request: NextRequest) {
|
||||
let supabaseResponse = NextResponse.next({
|
||||
@@ -50,15 +51,11 @@ export async function updateSession(request: NextRequest) {
|
||||
const { data } = await supabase.auth.getClaims();
|
||||
const user = data?.claims;
|
||||
|
||||
if (
|
||||
request.nextUrl.pathname !== "/" &&
|
||||
!user &&
|
||||
!request.nextUrl.pathname.startsWith("/login") &&
|
||||
!request.nextUrl.pathname.startsWith("/auth")
|
||||
) {
|
||||
// no user, potentially respond by redirecting the user to the login page
|
||||
const { pathname } = request.nextUrl;
|
||||
|
||||
if (!user && !isPublicPath(pathname)) {
|
||||
const url = request.nextUrl.clone();
|
||||
url.pathname = "/auth/login";
|
||||
url.pathname = routes.login;
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user