mirror of
https://github.com/supabase/supabase.git
synced 2026-09-09 03:19:36 +08:00
## I have read the [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) file. YES <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Documentation** * Simplified React getting-started tutorial: concise, action-oriented steps, inline code replaced by external sample references, removed the Bonus: Profile photos section, and expanded README guidance and setup steps. * **Examples** * User-management example converted to a user-focused auth flow with safer unmount handling, adjusted profile/account interactions, and updated sample references. * **Chores** * Upgraded example dependencies and tooling to align with newer React and ecosystem versions. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
112 lines
2.5 KiB
JavaScript
112 lines
2.5 KiB
JavaScript
import { useState, useEffect } from 'react'
|
|
import { supabase } from './supabaseClient'
|
|
import Avatar from './Avatar'
|
|
|
|
export default function Account({ user }) {
|
|
const [loading, setLoading] = useState(true)
|
|
const [username, setUsername] = useState(null)
|
|
const [website, setWebsite] = useState(null)
|
|
const [avatar_url, setAvatarUrl] = useState(null)
|
|
|
|
useEffect(() => {
|
|
let ignore = false
|
|
async function getProfile() {
|
|
setLoading(true)
|
|
|
|
const { data, error } = await supabase
|
|
.from('profiles')
|
|
.select(`username, website, avatar_url`)
|
|
.eq('id', user.id)
|
|
.single()
|
|
|
|
if (!ignore) {
|
|
if (error) {
|
|
console.warn(error)
|
|
} else if (data) {
|
|
setUsername(data.username)
|
|
setWebsite(data.website)
|
|
setAvatarUrl(data.avatar_url)
|
|
}
|
|
}
|
|
|
|
setLoading(false)
|
|
}
|
|
|
|
getProfile()
|
|
|
|
return () => {
|
|
ignore = true
|
|
}
|
|
}, [user])
|
|
|
|
async function updateProfile(event, avatarUrl) {
|
|
event.preventDefault()
|
|
|
|
setLoading(true)
|
|
|
|
const updates = {
|
|
id: user.id,
|
|
username,
|
|
website,
|
|
avatar_url: avatarUrl,
|
|
updated_at: new Date(),
|
|
}
|
|
|
|
const { error } = await supabase.from('profiles').upsert(updates)
|
|
|
|
if (error) {
|
|
alert(error.message)
|
|
} else {
|
|
setAvatarUrl(avatarUrl)
|
|
}
|
|
setLoading(false)
|
|
}
|
|
|
|
return (
|
|
<form onSubmit={updateProfile} className="form-widget">
|
|
<Avatar
|
|
url={avatar_url}
|
|
size={150}
|
|
onUpload={(event, url) => {
|
|
updateProfile(event, url)
|
|
}}
|
|
/>
|
|
<div>
|
|
<label htmlFor="email">Email</label>
|
|
<input id="email" type="text" value={user.email} disabled />
|
|
</div>
|
|
<div>
|
|
<label htmlFor="username">Name</label>
|
|
<input
|
|
id="username"
|
|
type="text"
|
|
required
|
|
value={username || ''}
|
|
onChange={(e) => setUsername(e.target.value)}
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label htmlFor="website">Website</label>
|
|
<input
|
|
id="website"
|
|
type="url"
|
|
value={website || ''}
|
|
onChange={(e) => setWebsite(e.target.value)}
|
|
/>
|
|
</div>
|
|
|
|
<div>
|
|
<button className="button block primary" type="submit" disabled={loading}>
|
|
{loading ? 'Loading ...' : 'Update'}
|
|
</button>
|
|
</div>
|
|
|
|
<div>
|
|
<button className="button block" type="button" onClick={() => supabase.auth.signOut()}>
|
|
Sign Out
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)
|
|
}
|