From 25036fe018977af31c1b50d59e5aac0a69897ed4 Mon Sep 17 00:00:00 2001 From: jihye Date: Thu, 9 Dec 2021 17:24:57 +0000 Subject: [PATCH] fetch public data from profiles table Co-authored-by: moggach <43313455+Moggach@users.noreply.github.com> --- components/Account.js | 62 ++++++++++++++++++++++-------------------- components/Greeting.js | 5 +++- pages/myProfile.js | 30 ++++++++++++++++---- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/components/Account.js b/components/Account.js index 607d857..b074ad0 100644 --- a/components/Account.js +++ b/components/Account.js @@ -2,42 +2,42 @@ import { useState, useEffect } from 'react'; import { supabase } from '../utils/supabaseClient'; import Image from 'next/image'; -export default function Account({ userData, setUserData }) { +export default function Account({ userProfile, setUserProfile }) { const [loading, setLoading] = useState(true); const [email, setEmail] = useState(null); const [avatar, setAvatar] = useState(null); const [username, setUsername] = useState(null); const [loggedinAt, setLoggedinAt] = useState(null); - useEffect(() => { - getProfile(); - }, []); + // useEffect(() => { + // getProfile(); + // }, []); - async function getProfile() { - try { - setLoading(true); - const user = supabase.auth.user(); + // async function getProfile() { + // try { + // setLoading(true); + // const user = supabase.auth.user(); - let { data, error, status } = await supabase - .from('profiles') - .select('username, avatar') - .eq('id', user.id) - .single(); + // let { data, error, status } = await supabase + // .from('profiles') + // .select('username, avatar') + // .eq('id', user.id) + // .single(); - if (error && status !== 406) { - throw error; - } + // if (error && status !== 406) { + // throw error; + // } - if (data) { - setUsername(data.username); - setAvatar(data.avatar); - } - } catch (error) { - alert(error.message); - } finally { - setLoading(false); - } - } + // if (data) { + // setUsername(data.username); + // setAvatar(data.avatar); + // } + // } catch (error) { + // alert(error.message); + // } finally { + // setLoading(false); + // } + // } // async function updateProfile({ username }) { // try { @@ -62,15 +62,17 @@ export default function Account({ userData, setUserData }) { // setLoading(false); // } // } - console.log(avatar); + console.log('userProfile on Account :', userProfile); return ( <> -
Welcome {username}!
+ Hello + {/*
Welcome {username}!
You last signed in at
{/* {username} */} -
+ {/*
-
+
*/}{' '} + */ ); } diff --git a/components/Greeting.js b/components/Greeting.js index a9abbc3..bc16af5 100644 --- a/components/Greeting.js +++ b/components/Greeting.js @@ -8,7 +8,10 @@ const Greeting = ({ user }) => {
-

Hello{user}

+

+

Hello

+

{user}

+

); }; diff --git a/pages/myProfile.js b/pages/myProfile.js index ef28f1e..660cdf0 100644 --- a/pages/myProfile.js +++ b/pages/myProfile.js @@ -5,15 +5,22 @@ import Greeting from '../components/Greeting'; import Account from '../components/Account'; import { BsFillArrowRightCircleFill } from 'react-icons/bs'; import Main from '../components/Main'; +import { supabase } from '../utils/supabaseClient'; const MyProfile = ({ supabase }) => { - const [userData, setUserData] = React.useState(null); - + const [userProfile, setUserProfile] = React.useState(null); async function fetchData() { const user = await supabase.auth.user(); - setUserData(user); + + const { data, error, status } = await supabase + .from('profiles') + .select('username, avatar') + .eq('id', user.id) + .single(); + + setUserProfile(data); } React.useEffect(() => { @@ -28,8 +35,7 @@ const MyProfile = ({ supabase }) => {

My Profile

- - +
  • @@ -58,4 +64,18 @@ const MyProfile = ({ supabase }) => { ); }; +// export async function getServerSideProps({ session, supabase }) { +// const user = supabase.auth.user(); +// const { userProfile, error, status } = await supabase +// .from('profiles') +// .select('username, avatar') +// .eq('id', user.id) +// .single(); + +// console.log(userProfile); +// return { +// props: { userProfile, error }, +// }; +// } + export default MyProfile;