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

update auth flow & database schema #111

Merged
merged 1 commit into from
May 3, 2024
Merged
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
4 changes: 1 addition & 3 deletions apps/privelte/src/lib/server/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ export const createToken = async (payload: JWTPayload) => {
return { jwt, expires }
}

export const verifyToken = async (token: string | undefined) => {
export const verifyToken = async (token: string) => {
try {
if (!token) throw Error('No token provided.')

const { payload } = await jwtVerify<User>(token, secret, {
algorithms: [algorithm]
})
Expand Down
14 changes: 9 additions & 5 deletions apps/privelte/src/lib/types/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ export interface Database {
users: {
Row: {
id: string
last_heartbeat: string
last_active: string
room_id: string
username: string
}
Insert: {
id?: string
last_heartbeat?: string
last_active?: string
room_id: string
username: string
}
Update: {
id?: string
last_heartbeat?: string
last_active?: string
room_id?: string
username?: string
}
Relationships: [
{
Expand Down Expand Up @@ -130,8 +133,9 @@ export type TablesUpdate<
: never

export type Enums<
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
PublicEnumNameOrOptions extends keyof PublicSchema['Enums'] | { schema: keyof Database },
PublicEnumNameOrOptions extends
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents
keyof PublicSchema['Enums'] | { schema: keyof Database },
EnumName extends PublicEnumNameOrOptions extends { schema: keyof Database }
? keyof Database[PublicEnumNameOrOptions['schema']]['Enums']
: never = never
Expand Down
4 changes: 4 additions & 0 deletions apps/privelte/src/routes/room/[id]/+layout.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export const load: LayoutServerLoad = async ({ params, cookies }) => {
if (room.data.participants === room.data.slots) {
const session = cookies.get('session')

if (!session) {
error(403, { message: 'This room is full.' })
}

try {
const { userId } = await verifyToken(session)

Expand Down
4 changes: 4 additions & 0 deletions apps/privelte/src/routes/room/[id]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import type { PageServerLoad } from './$types'
export const load: PageServerLoad = async ({ params, cookies }) => {
const session = cookies.get('session')

if (!session) {
return redirect(302, `/room/${params.id}/join`)
}

try {
const { userId, username } = await verifyToken(session)

Expand Down
4 changes: 2 additions & 2 deletions apps/privelte/src/routes/room/[id]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<script lang="ts">
import { onMount } from 'svelte'
import { supabase } from '$lib/supabaseClient'
// import { nanoid } from 'nanoid'
import { pendingMessages } from '$lib/stores'
import type { Payload, Presence } from '$lib/types/types'
import type { PageData } from './$types'
import { supabase } from '$lib/supabaseClient'
import Chat from '$lib/components/Chat.svelte'
import NewMessageForm from '$lib/components/NewMessageForm.svelte'

Expand Down Expand Up @@ -71,7 +71,7 @@
await fetch(data.roomId, {
method: 'PATCH'
})
}, 10000)
}, 12000)

channel
.on('broadcast', { event: 'message' }, ({ payload }: { payload: Payload }) => {
Expand Down
11 changes: 10 additions & 1 deletion apps/privelte/src/routes/room/[id]/+server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import type { RequestHandler } from './$types'

export const POST: RequestHandler = async ({ request, cookies, params }) => {
const session = cookies.get('session')

if (!session) {
error(401, 'Unauthorized.')
}

let userId: string
let username: string

Expand Down Expand Up @@ -66,12 +71,16 @@ export const POST: RequestHandler = async ({ request, cookies, params }) => {
export const PATCH: RequestHandler = async ({ cookies, params }) => {
const session = cookies.get('session')

if (!session) {
error(401, 'Unauthorized.')
}

try {
const { userId, username } = await verifyToken(session)

await supabase
.from('users')
.update({ last_heartbeat: new Date().toISOString() })
.update({ last_active: new Date().toISOString() })
.eq('id', userId)
.eq('room_id', params.id)
.select()
Expand Down
6 changes: 5 additions & 1 deletion apps/privelte/src/routes/room/[id]/join/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import type { PageServerLoad } from './$types'
export const load: PageServerLoad = async ({ params, cookies }) => {
const session = cookies.get('session')

if (!session) {
return { title: 'Join - Privelte' }
}

try {
const { userId } = await verifyToken(session)

Expand Down Expand Up @@ -47,7 +51,7 @@ export const actions: Actions = {

const user = await supabase
.from('users')
.insert({ room_id: room.data.id })
.insert({ room_id: room.data.id, username })
.select()
.single()
.throwOnError()
Expand Down
Loading