Skip to content

Commit

Permalink
add logout functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
eriktaubeneck committed Jan 12, 2024
1 parent 808f7b8 commit 5a001df
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 27 deletions.
35 changes: 35 additions & 0 deletions server/app/auth/signout/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
import { createServerClient, type CookieOptions } from "@supabase/ssr";

export async function GET() {
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 {
data: { session },
} = await supabase.auth.getSession();

if (session) {
await supabase.auth.signOut();
}

redirect("/");
}
9 changes: 1 addition & 8 deletions server/app/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,14 @@ import Link from "next/link";
import Image from "next/image";
import { Disclosure, Menu, Transition } from "@headlessui/react";
import { Bars3Icon, BellIcon, XMarkIcon } from "@heroicons/react/24/outline";
import { createBrowserClient } from "@supabase/ssr";
import clsx from "clsx";
import beerTap from "@/public/beer-tap.png";

// const user = {
// name: "Tom Cook",
// email: "[email protected]",
// imageUrl:
// "https://images.unsplash.com/photo-1472099645785-5658abf4ff4e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=facearea&facepad=2&w=256&h=256&q=80",
// };
const navigation = [{ name: "Dashboard", href: "/query", current: true }];
const userNavigation = [
{ name: "Your Profile", href: "#" },
{ name: "Settings", href: "#" },
{ name: "Sign out", href: "#" },
{ name: "Sign out", href: "/auth/signout" },
];

export default function Header({ user }: { user: any }) {
Expand Down
2 changes: 1 addition & 1 deletion server/app/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default async function LoginPage() {
} = await supabase.auth.getSession();

if (session) {
redirect("/dashboard");
redirect("/query");
}

return (
Expand Down
45 changes: 27 additions & 18 deletions server/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,62 +1,71 @@
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server'
import { createServerClient, type CookieOptions } from "@supabase/ssr";
import { NextResponse, type NextRequest } from "next/server";

export async function middleware(request: NextRequest) {
let response = NextResponse.next({
request: {
headers: request.headers,
},
})
});

const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return request.cookies.get(name)?.value
return request.cookies.get(name)?.value;
},
set(name: string, value: string, options: CookieOptions) {
request.cookies.set({
name,
value,
...options,
})
});
response = NextResponse.next({
request: {
headers: request.headers,
},
})
});
response.cookies.set({
name,
value,
...options,
})
});
},
remove(name: string, options: CookieOptions) {
request.cookies.set({
name,
value: '',
value: "",
...options,
})
});
response = NextResponse.next({
request: {
headers: request.headers,
},
})
});
response.cookies.set({
name,
value: '',
value: "",
...options,
})
});
},
},
}
)
},
);

const {
data: { user },
} = await supabase.auth.getUser();

await supabase.auth.getUser()
if (!user && request.nextUrl.pathname !== "/") {
const url = request.nextUrl.clone();

return response
url.pathname = `/404`;
return NextResponse.rewrite(url);
}

return response;
}

export const config = {
Expand All @@ -68,6 +77,6 @@ export const config = {
* - favicon.ico (favicon file)
* Feel free to modify this pattern to include more paths.
*/
'/((?!_next/static|_next/image|favicon.ico).*)',
"/((?!_next/static|_next/image|favicon.ico).*)",
],
}
};

0 comments on commit 5a001df

Please sign in to comment.