54
loading...
This website collects cookies to deliver better user experience
username
, avatar_url
, and website
.components/Profile.js
file and add the following function.async function getProfile() {
try {
const user = supabase.auth.user();
let { data, error, status } = await supabase
.from('profiles')
.select(`username`)
.eq('id', user.id)
.single();
if (error && status !== 406) {
throw error;
}
if (data) {
setUsername(data.username);
}
} catch (error) {
alert(error.message);
}
}
<input className='my-4 border-2 border-gray-500 rounded-xl p-4 w-full' type='username' placeholder='Enter a username' value={username} onChange={(e) => setUsername(e.target.value)} />
<button onClick={(e) => { e.preventDefault(); updateProfile();}} className='w-full mt-4 p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-lg rounded-lg focus:border-4 border-blue-300'>
<span>Update profile</span>
</button>
updateProfile
method, so let's go ahead and create that.async function updateProfile() {
try {
const user = supabase.auth.user();
const updates = {
id: user.id,
username,
updated_at: new Date(),
};
let { error } = await supabase.from('profiles').upsert(updates);
if (error) {
throw error;
}
} catch (error) {
alert(error.message);
}
}