-
Notifications
You must be signed in to change notification settings - Fork 1
feat: support guest session #8
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { redirect } from 'next/navigation'; | ||
import { auth, signIn } from '@/app/(auth)/auth'; | ||
|
||
export async function GET() { | ||
const session = await auth(); | ||
|
||
if (!session?.user?.id) { | ||
await signIn('guest', { redirect: false }); | ||
redirect('/'); | ||
} | ||
|
||
return new Response('Unauthorized', { status: 401 }); | ||
} | ||
arvi18 marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,5 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
UIMessage, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type UIMessage, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
appendResponseMessages, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
createDataStreamResponse, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
smoothStream, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -10,6 +10,7 @@ import { systemPrompt } from '@/lib/ai/prompts'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
deleteChatById, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
getChatById, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
getMessageCountByUserId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
saveChat, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
saveMessages, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} from '@/lib/db/queries'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -23,8 +24,12 @@ import { createDocument } from '@/lib/ai/tools/create-document'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { updateDocument } from '@/lib/ai/tools/update-document'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { requestSuggestions } from '@/lib/ai/tools/request-suggestions'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { getWeather } from '@/lib/ai/tools/get-weather'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { isProductionEnvironment } from '@/lib/constants'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { anonymousRegex, isProductionEnvironment } from '@/lib/constants'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { myProvider } from '@/lib/ai/providers'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
entitlementsByMembershipTier, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
type MembershipTier, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} from '@/lib/ai/capabilities'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export const maxDuration = 60; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -42,10 +47,33 @@ export async function POST(request: Request) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const session = await auth(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!session || !session.user || !session.user.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!session?.user?.id) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return new Response('Unauthorized', { status: 401 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const membershipTier: MembershipTier = anonymousRegex.test( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
session.user.email ?? '', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
? 'guest' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
: 'free'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const messageCount = await getMessageCountByUserId({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
id: session.user.id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
differenceInHours: 24, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
messageCount > | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
entitlementsByMembershipTier[membershipTier].maxMessagesPerDay | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return new Response( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'You have exceeded your maximum number of messages for the day', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
status: 429, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
47
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Insufficient Rate Limiting for Guest UsersThe PR implements rate limiting based on message count per user ID, but lacks IP-based rate limiting for guest account creation. An attacker could create unlimited anonymous accounts (each with its own message quota) by repeatedly calling the guest authentication endpoint, effectively bypassing the per-user message limit and potentially causing a denial of service.
Suggested change
Standard: CWE-770 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
const userMessage = getMostRecentUserMessage(messages); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (!userMessage) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import type { ChatModel } from './models'; | ||
|
||
export type MembershipTier = 'guest' | 'free'; | ||
|
||
interface Entitlements { | ||
maxMessagesPerDay: number; | ||
chatModelsAvailable: Array<ChatModel['id']>; | ||
} | ||
|
||
export const entitlementsByMembershipTier: Record< | ||
MembershipTier, | ||
Entitlements | ||
> = { | ||
/* | ||
* For users without an account | ||
*/ | ||
guest: { | ||
maxMessagesPerDay: 20, | ||
chatModelsAvailable: ['chat-model', 'chat-model-reasoning'], | ||
}, | ||
|
||
/* | ||
* For user with an account | ||
*/ | ||
free: { | ||
maxMessagesPerDay: 100, | ||
chatModelsAvailable: ['chat-model', 'chat-model-reasoning'], | ||
}, | ||
|
||
/* | ||
* TODO: For users with an account and a paid membership | ||
*/ | ||
}; |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,3 +5,5 @@ export const isTestEnvironment = Boolean( | |||||
process.env.PLAYWRIGHT || | ||||||
process.env.CI_PLAYWRIGHT, | ||||||
); | ||||||
|
||||||
export const anonymousRegex = /^anonymous-\d+$/; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This regular expression is designed to match anonymous user emails created with A simple update could be to match any characters after
Suggested change
|
Uh oh!
There was an error while loading. Please reload this page.