home page

This commit is contained in:
秦秋旭 2023-02-19 15:35:28 +08:00
parent 5363ec8a68
commit 728bab5fee
3 changed files with 54 additions and 1 deletions

View File

@ -1 +1,2 @@
export * as auth from './auth' export * as auth from './auth'
export * as user from './user'

16
src/api/user.ts Normal file
View File

@ -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<UserEntity>('/api/user/profile')
}

View File

@ -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() { export default function Home() {
return <>hello world</> const router = useRouter()
const [user, setUser] = useState<UserEntity>()
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 (
<CircularProgress
sx={{
position: 'fixed',
top: '30%',
left: '50%',
transform: 'translate(-50%, -50%)',
}}
/>
)
}
return <>hello {user.email}</>
} }