diff --git a/src/api/index.ts b/src/api/index.ts index 02e3b86..8d10197 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1 +1,2 @@ export * as auth from './auth' +export * as user from './user' diff --git a/src/api/user.ts b/src/api/user.ts new file mode 100644 index 0000000..fa6650c --- /dev/null +++ b/src/api/user.ts @@ -0,0 +1,16 @@ +import axios from '@/utils/axios' + +export interface UserEntity { + id: string + email: string + username: string | null + password: string + /** @format date-time */ + createdAt: string + /** @format date-time */ + updatedAt: string +} + +export async function getUserInfo() { + return axios.get('/api/user/profile') +} diff --git a/src/pages/index.tsx b/src/pages/index.tsx index 39a4136..1363ca1 100644 --- a/src/pages/index.tsx +++ b/src/pages/index.tsx @@ -1,3 +1,39 @@ +import { useEffect, useState } from 'react' +import * as api from '@/api' +import type { UserEntity } from '@/api/user' +import CircularProgress from '@mui/material/CircularProgress' +import { AxiosError } from 'axios' +import { useRouter } from 'next/router' + export default function Home() { - return <>hello world + const router = useRouter() + + const [user, setUser] = useState() + useEffect(() => { + console.log('useEffect') + api.user + .getUserInfo() + .then((res) => { + setUser(res.data) + }) + .catch((err: AxiosError) => { + console.error(err.response) + router.push('/login') + }) + }, [router]) + + if (!user) { + return ( + + ) + } + + return <>hello {user.email} }