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

Next.js auth integration package POC #3

Draft
wants to merge 11 commits into
base: feat/supabase
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 7 commits
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
2 changes: 1 addition & 1 deletion app/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { redirect } from 'next/navigation'

import { auth } from '@/auth'
import { type Chat } from '@/lib/types'
import { createClient } from '@/utils/supabase/server'
import { createClient } from '@supabase-labs/nextjs/server'

export async function getChats(userId?: string | null) {
const session = await auth()
Expand Down
23 changes: 0 additions & 23 deletions app/auth/confirm/route.ts

This file was deleted.

2 changes: 1 addition & 1 deletion app/login/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { z } from 'zod'
import { ResultCode } from '@/lib/utils'
import { createClient } from '@/utils/supabase/server'
import { createClient } from '@supabase-labs/nextjs/server'

interface Result {
type: string
Expand Down
9 changes: 0 additions & 9 deletions app/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { auth } from '@/auth'
import LoginForm from '@/components/login-form'
import { Session } from '@/lib/types'
import { redirect } from 'next/navigation'

export default async function LoginPage() {
const session = (await auth()) as Session

if (session) {
redirect('/')
}
Comment on lines -9 to -11
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's better to centralize this logic of public vs protected pages in the middleware.


return (
<main className="flex flex-col p-4">
<LoginForm />
Expand Down
2 changes: 1 addition & 1 deletion app/signup/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { ResultCode } from '@/lib/utils'
import { z } from 'zod'
import { createClient } from '@/utils/supabase/server'
import { createClient } from '@supabase-labs/nextjs/server'
import { headers } from 'next/headers'
interface Result {
type: string
Expand Down
9 changes: 0 additions & 9 deletions app/signup/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { auth } from '@/auth'
import SignupForm from '@/components/signup-form'
import { Session } from '@/lib/types'
import { redirect } from 'next/navigation'

export default async function SignupPage() {
const session = (await auth()) as Session

if (session) {
redirect('/')
}

return (
<main className="flex flex-col p-4">
<SignupForm />
Expand Down
2 changes: 1 addition & 1 deletion auth.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient } from '@/utils/supabase/server'
import { createClient } from '@supabase-labs/nextjs/server'
import { Session } from '@/lib/types'

export async function auth(): Promise<Session | null> {
Expand Down
2 changes: 1 addition & 1 deletion components/ui/codeblock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const CodeBlock: FC<Props> = memo(({ language, value }) => {
3,
true
)}${fileExtension}`
const fileName = window.prompt('Enter file name' || '', suggestedFileName)
const fileName = window.prompt('Enter file name', suggestedFileName)

if (!fileName) {
// User pressed cancel on prompt.
Expand Down
4 changes: 2 additions & 2 deletions components/user-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
DropdownMenuSeparator,
DropdownMenuTrigger
} from '@/components/ui/dropdown-menu'
import { createClient } from '@/utils/supabase/server'
import { createClient } from '@supabase-labs/nextjs/server'
import { redirect } from 'next/navigation'

export interface UserMenuProps {
Expand Down Expand Up @@ -42,7 +42,7 @@ export function UserMenu({ user }: UserMenuProps) {
'use server'
const supabase = createClient()
await supabase.auth.signOut()
redirect('/login')
redirect('/')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the middleware takes care of redirecting to the login page if the user is not authenticated. 👍

}}
>
<button className=" relative flex w-full cursor-pointer select-none items-center rounded-sm px-2 py-1.5 text-xs outline-none transition-colors hover:bg-red-500 hover:text-white focus:bg-accent focus:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50">
Expand Down
23 changes: 0 additions & 23 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { CoreMessage } from 'ai'
import { MergeDeep } from 'type-fest'
import { Database as SupabaseDatabase } from '@/utils/supabase/types'

export type Message = CoreMessage & {
id: string
Expand Down Expand Up @@ -41,24 +39,3 @@ export interface User extends Record<string, any> {
password: string
salt: string
}

export type Database = MergeDeep<
SupabaseDatabase,
{
public: {
Tables: {
chats: {
Row: {
payload: Chat | null
}
Insert: {
payload?: Chat | null
}
Update: {
payload?: Chat | null
}
}
}
}
}
>
28 changes: 23 additions & 5 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
import { updateSession } from '@/utils/supabase/middleware'
import { NextRequest } from 'next/server'
import { supabaseMiddleware, createRouteMatcher } from '@supabase-labs/nextjs/server'

export async function middleware(request: NextRequest) {
return await updateSession(request)
}
const isPublicRoute = createRouteMatcher(['/login(.*)', '/signup(.*)'])

export default supabaseMiddleware(
async (auth, request) => {
const session = await auth()

// protect all routes except public ones
if (!isPublicRoute(request) && !session.user) {
return session.redirectToSignIn()
}

// redirect to home if user is logged in and on public route
if (isPublicRoute(request) && session.user) {
return session.redirectToHome()
}
},
{
paths: {
signIn: '/login'
}
}
)

export const config = {
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)']
Expand Down
Loading