Skip to content

Commit 806b235

Browse files
committed
Moving everything over to a profiles table
1 parent 08e6e7e commit 806b235

File tree

4 files changed

+82
-40
lines changed

4 files changed

+82
-40
lines changed

example/next-storage/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
create table public.profiles (
1111
id uuid references auth.users not null primary key,
1212
username text unique,
13-
full_name text,
14-
avatar_url text
13+
avatar_url text,
14+
dob date
1515
);
1616
alter table profiles enable row level security;
1717
create policy "Public profiles are viewable by everyone." on profiles for select using (true);
18-
create policy "Individuals update own user data." on profiles for update using (auth.uid() = id);
18+
create policy "Users can insert their own profile." on profiles for insert with check (auth.uid() = id);
19+
create policy "Users can update own profile." on profiles for update using (auth.uid() = id);
1920
```

example/next-storage/pages/index.tsx

Lines changed: 74 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import styles from '../styles/Home.module.css'
77
import { AuthUser, AuthSession } from '../../../dist/main'
88
import { DEFAULT_AVATARS_BUCKET } from '../lib/constants'
99

10+
type Profile = {
11+
avatar_url: string
12+
username: string
13+
dob: string
14+
}
15+
1016
export default function Home() {
1117
const [session, setSession] = useState<AuthSession | null>(null)
1218
const [avatar, setAvatar] = useState<string | null>(null)
@@ -15,80 +21,115 @@ export default function Home() {
1521

1622
useEffect(() => {
1723
setSession(supabase.auth.session())
18-
setProfile()
24+
1925
supabase.auth.onAuthStateChange((_event: string, session: AuthSession | null) => {
2026
setSession(session)
21-
if (session?.user) {
22-
setProfile()
23-
} else {
24-
setAvatar(null)
25-
setUsername(null)
26-
setDob(null)
27-
}
2827
})
2928
}, [])
3029

30+
useEffect(() => {
31+
if (session) {
32+
getProfile()
33+
} else {
34+
setAvatar(null)
35+
setUsername(null)
36+
setDob(null)
37+
}
38+
}, [session])
39+
3140
async function signOut() {
3241
const { error } = await supabase.auth.signOut()
3342
if (error) console.log('Error logging out:', error.message)
3443
}
3544

3645
async function uploadAvatar(event: ChangeEvent<HTMLInputElement>) {
3746
try {
47+
if (!session) {
48+
throw new Error('uploadAvatar() Not logged in.')
49+
}
50+
51+
const user = supabase.auth.user()
52+
3853
if (!event.target.files || event.target.files.length == 0) {
39-
alert('You must select an image to upload')
40-
return
54+
throw 'You must select an image to upload.'
4155
}
4256

4357
const file = event.target.files[0]
4458
const fileExt = file.name.split('.').pop()
4559
const fileName = `${session?.user.id}${Math.random()}.${fileExt}`
4660
const filePath = `${DEFAULT_AVATARS_BUCKET}/${fileName}`
4761

48-
let { data, error } = avatar
49-
? await supabase.storage.uploadFile(filePath, file) // change this to update
50-
: await supabase.storage.uploadFile(filePath, file)
62+
let { error: uploadError } = await supabase.storage.uploadFile(filePath, file)
5163

52-
if (error) {
53-
throw error
64+
if (uploadError) {
65+
throw uploadError
5466
}
5567

56-
// await supabase.from('profiles').update({ avatar_url: fileName })
57-
58-
await supabase.auth.update({
59-
data: {
60-
avatar_url: fileName,
61-
},
68+
let { error: updateError } = await supabase.from('profiles').upsert({
69+
id: user.id,
70+
avatar_url: fileName,
6271
})
6372

73+
if (updateError) {
74+
throw updateError
75+
}
76+
6477
setAvatar(null)
6578
setAvatar(fileName)
6679
} catch (error) {
6780
alert(error.message)
6881
}
6982
}
7083

71-
async function setProfile() {
84+
function setProfile(profile: Profile) {
85+
setAvatar(profile.avatar_url)
86+
setUsername(profile.username)
87+
setDob(profile.dob)
88+
}
89+
90+
async function getProfile() {
7291
try {
92+
if (!session) {
93+
throw new Error('getProfile() Not logged in.')
94+
}
95+
7396
const user = supabase.auth.user()
74-
if (user) {
75-
setAvatar(user.user_metadata.avatar_url)
76-
setUsername(user.user_metadata.username)
77-
setDob(user.user_metadata.dob)
97+
98+
let { data, error } = await supabase
99+
.from('profiles')
100+
.select(`username, dob, avatar_url`)
101+
.eq('id', user.id)
102+
.single()
103+
104+
if (error) {
105+
throw error
78106
}
107+
108+
setProfile(data)
79109
} catch (error) {
80110
console.log('error', error.message)
81111
}
82112
}
83113

84114
async function updateProfile() {
85115
try {
86-
await supabase.auth.update({
87-
data: {
88-
username,
89-
dob,
90-
},
116+
if (!session) {
117+
throw new Error('Not logged in.')
118+
}
119+
120+
const user = supabase.auth.user()
121+
122+
let { data: profile, error } = await supabase.from('profiles').upsert({
123+
id: user.id,
124+
username,
125+
dob,
91126
})
127+
128+
if (error) {
129+
throw error
130+
}
131+
132+
setProfile(profile)
92133
} catch (error) {
93134
console.log('error', error.message)
94135
}
@@ -140,13 +181,13 @@ export default function Home() {
140181
</div>
141182

142183
<div>
143-
<button className="button block primary" onClick={updateProfile}>
184+
<button className="button block primary" onClick={() => updateProfile()}>
144185
Update profile
145186
</button>
146187
</div>
147188

148189
<div>
149-
<button className="button block" onClick={signOut}>
190+
<button className="button block" onClick={() => signOut()}>
150191
Sign Out
151192
</button>
152193
</div>

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
},
3737
"dependencies": {
3838
"@supabase/gotrue-js": "^1.12.2",
39-
"@supabase/postgrest-js": "^0.26.1",
39+
"@supabase/postgrest-js": "^0.28.0",
4040
"@supabase/realtime-js": "^1.0.6",
4141
"cross-fetch": "^3.1.0",
4242
"gotrue-js": "^0.9.29"

0 commit comments

Comments
 (0)