Skip to content

Commit 6f0a6b1

Browse files
committed
chore: update next-storage example to use the new Realtime API
1 parent 170065c commit 6f0a6b1

File tree

5 files changed

+8205
-465
lines changed

5 files changed

+8205
-465
lines changed

example/next-storage/components/Account.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export default function Account({
3535
throw 'You must select an image to upload.'
3636
}
3737

38-
const user = supabase.auth.user()
38+
const { user } = await supabase.auth.getUser()
3939
const file = event.target.files[0]
4040
const fileExt = file.name.split('.').pop()
41-
const fileName = `${session?.user.id}${Math.random()}.${fileExt}`
41+
const fileName = `${session?.user?.id}${Math.random()}.${fileExt}`
4242
const filePath = `${fileName}`
4343

4444
let { error: uploadError } = await supabase.storage
@@ -127,7 +127,7 @@ export default function Account({
127127
<div className="account">
128128
<div>
129129
<label htmlFor="email">Email</label>
130-
<input id="email" type="text" value={session.user.email} disabled />
130+
<input id="email" type="text" value={session?.user?.email} disabled />
131131
</div>
132132
<div>
133133
<label htmlFor="username">Name</label>

example/next-storage/components/ProfileList.tsx

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
import ProfileCard from '../components/ProfileCard'
22
import { Profile } from '../lib/constants'
3-
import { Subscription, SupabaseRealtimePayload } from '@supabase/supabase-js'
3+
import { SupabaseRealtimePayload } from '@supabase/supabase-js'
44
import { supabase } from '../lib/api'
55
import { useState, useEffect } from 'react'
66

7-
var realtimeProfiles: Subscription | null = null
8-
97
export default function ProfileList() {
108
const [profiles, setProfiles] = useState<Profile[]>([])
119

1210
useEffect(() => {
1311
// getPublicProfiles()
1412
getUserProfile()
1513

16-
realtimeProfiles = supabase
17-
.from('profiles')
18-
.on('*', (payload: SupabaseRealtimePayload<Profile>) => profileUpdated(profiles, payload.new))
14+
const realtimeProfiles = supabase
15+
.channel('profiles-channel')
16+
.on(
17+
'realtime',
18+
{ event: '*', schema: 'public', table: 'profiles' },
19+
(payload: SupabaseRealtimePayload<Profile>) => profileUpdated(profiles, payload.new)
20+
)
1921
.subscribe()
2022

2123
return () => {
22-
if (realtimeProfiles) supabase.removeSubscription(realtimeProfiles)
24+
if (realtimeProfiles) supabase.removeChannel(realtimeProfiles)
2325
}
2426
}, [])
2527

@@ -29,12 +31,12 @@ export default function ProfileList() {
2931
}
3032

3133
async function getUserProfile() {
32-
const user = supabase.auth.user()
34+
const { user } = await supabase.auth.getUser()
3335
try {
3436
const { data, error } = await supabase
3537
.from('profiles')
3638
.select('id, username, avatar_url, website, updated_at')
37-
.eq('id', user.id)
39+
.eq('id', user?.id)
3840
.order('updated_at', { ascending: false })
3941
if (error) {
4042
throw error

0 commit comments

Comments
 (0)