Initial commit v1
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
'use server'
|
||||
|
||||
import { z } from 'zod'
|
||||
import { createClient, createAdminClient } from '@/lib/supabase/server'
|
||||
|
||||
const InviteSchema = z.object({
|
||||
roundId: z.string().uuid(),
|
||||
email: z.string().email(),
|
||||
})
|
||||
|
||||
export async function inviteFromLobby(roundId: string, email: string) {
|
||||
const parsed = InviteSchema.safeParse({ roundId, email })
|
||||
if (!parsed.success) return { error: 'Invalid email address' }
|
||||
|
||||
const supabase = await createClient()
|
||||
const adminClient = await createAdminClient()
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const db = supabase as any
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser()
|
||||
if (!user) return { error: 'Not authenticated' }
|
||||
|
||||
const { data: round } = await db
|
||||
.from('rounds')
|
||||
.select('status, created_by')
|
||||
.eq('id', parsed.data.roundId)
|
||||
.single()
|
||||
|
||||
if (!round) return { error: 'Round not found' }
|
||||
if (round.created_by !== user.id) return { error: 'Only the round creator can invite players' }
|
||||
if (round.status !== 'lobby') return { error: 'Cannot invite after the round has started' }
|
||||
|
||||
const redirectTo = `${process.env.SITE_URL}/auth/confirm?next=${encodeURIComponent(`/rounds/${parsed.data.roundId}/join`)}`
|
||||
|
||||
const { error: inviteError } = await (adminClient as any).auth.admin.inviteUserByEmail(
|
||||
parsed.data.email,
|
||||
{ redirectTo },
|
||||
)
|
||||
if (inviteError) return { error: inviteError.message }
|
||||
|
||||
await db.from('round_invites').insert({
|
||||
round_id: parsed.data.roundId,
|
||||
email: parsed.data.email,
|
||||
invited_by: user.id,
|
||||
})
|
||||
|
||||
return { success: true }
|
||||
}
|
||||
Reference in New Issue
Block a user