@@ -7,6 +7,12 @@ import styles from '../styles/Home.module.css'
7
7
import { AuthUser , AuthSession } from '../../../dist/main'
8
8
import { DEFAULT_AVATARS_BUCKET } from '../lib/constants'
9
9
10
+ type Profile = {
11
+ avatar_url : string
12
+ username : string
13
+ dob : string
14
+ }
15
+
10
16
export default function Home ( ) {
11
17
const [ session , setSession ] = useState < AuthSession | null > ( null )
12
18
const [ avatar , setAvatar ] = useState < string | null > ( null )
@@ -15,80 +21,115 @@ export default function Home() {
15
21
16
22
useEffect ( ( ) => {
17
23
setSession ( supabase . auth . session ( ) )
18
- setProfile ( )
24
+
19
25
supabase . auth . onAuthStateChange ( ( _event : string , session : AuthSession | null ) => {
20
26
setSession ( session )
21
- if ( session ?. user ) {
22
- setProfile ( )
23
- } else {
24
- setAvatar ( null )
25
- setUsername ( null )
26
- setDob ( null )
27
- }
28
27
} )
29
28
} , [ ] )
30
29
30
+ useEffect ( ( ) => {
31
+ if ( session ) {
32
+ getProfile ( )
33
+ } else {
34
+ setAvatar ( null )
35
+ setUsername ( null )
36
+ setDob ( null )
37
+ }
38
+ } , [ session ] )
39
+
31
40
async function signOut ( ) {
32
41
const { error } = await supabase . auth . signOut ( )
33
42
if ( error ) console . log ( 'Error logging out:' , error . message )
34
43
}
35
44
36
45
async function uploadAvatar ( event : ChangeEvent < HTMLInputElement > ) {
37
46
try {
47
+ if ( ! session ) {
48
+ throw new Error ( 'uploadAvatar() Not logged in.' )
49
+ }
50
+
51
+ const user = supabase . auth . user ( )
52
+
38
53
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.'
41
55
}
42
56
43
57
const file = event . target . files [ 0 ]
44
58
const fileExt = file . name . split ( '.' ) . pop ( )
45
59
const fileName = `${ session ?. user . id } ${ Math . random ( ) } .${ fileExt } `
46
60
const filePath = `${ DEFAULT_AVATARS_BUCKET } /${ fileName } `
47
61
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 )
51
63
52
- if ( error ) {
53
- throw error
64
+ if ( uploadError ) {
65
+ throw uploadError
54
66
}
55
67
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 ,
62
71
} )
63
72
73
+ if ( updateError ) {
74
+ throw updateError
75
+ }
76
+
64
77
setAvatar ( null )
65
78
setAvatar ( fileName )
66
79
} catch ( error ) {
67
80
alert ( error . message )
68
81
}
69
82
}
70
83
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 ( ) {
72
91
try {
92
+ if ( ! session ) {
93
+ throw new Error ( 'getProfile() Not logged in.' )
94
+ }
95
+
73
96
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
78
106
}
107
+
108
+ setProfile ( data )
79
109
} catch ( error ) {
80
110
console . log ( 'error' , error . message )
81
111
}
82
112
}
83
113
84
114
async function updateProfile ( ) {
85
115
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,
91
126
} )
127
+
128
+ if ( error ) {
129
+ throw error
130
+ }
131
+
132
+ setProfile ( profile )
92
133
} catch ( error ) {
93
134
console . log ( 'error' , error . message )
94
135
}
@@ -140,13 +181,13 @@ export default function Home() {
140
181
</ div >
141
182
142
183
< div >
143
- < button className = "button block primary" onClick = { updateProfile } >
184
+ < button className = "button block primary" onClick = { ( ) => updateProfile ( ) } >
144
185
Update profile
145
186
</ button >
146
187
</ div >
147
188
148
189
< div >
149
- < button className = "button block" onClick = { signOut } >
190
+ < button className = "button block" onClick = { ( ) => signOut ( ) } >
150
191
Sign Out
151
192
</ button >
152
193
</ div >
0 commit comments