Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
10 changes: 5 additions & 5 deletions 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 All @@ -30,7 +30,7 @@ export async function getChats(userId?: string | null) {
.eq('user_id', userId)
.throwOnError()

return data?.map(chat => chat.payload as Chat) ?? []
return data?.map(chat => chat.payload).filter(chat => chat !== null) ?? []
} catch (error) {
return []
}
Expand All @@ -53,7 +53,7 @@ export async function getChat(id: string, userId: string) {
.eq('id', id)
.maybeSingle()

return (data?.payload as Chat) ?? null
return data?.payload ?? null
}

export async function removeChat({ id, path }: { id: string; path: string }) {
Expand Down Expand Up @@ -114,7 +114,7 @@ export async function getSharedChat(id: string) {
.not('payload->sharePath', 'is', null)
.maybeSingle()

return (data?.payload as Chat) ?? null
return data?.payload ?? null
}

export async function shareChat(id: string) {
Expand All @@ -134,7 +134,7 @@ export async function shareChat(id: string) {
.eq('id', id)
.maybeSingle()

const chat = data?.payload as Chat | null
const chat = data?.payload ?? null

if (!chat || chat.userId !== session.user.id) {
return {
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
}
}
}
}
}
>
27 changes: 22 additions & 5 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
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(
(auth, request) => {
if (!isPublicRoute(request)) {
auth().protect(has => has({ user_metadata: { roles: ['admin'] } }))
}

if (isPublicRoute(request) && auth().user) {
auth().redirect('/')
}
},
{
paths: {
signIn: '/login'
}
}
)

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