home page
This commit is contained in:
		
							parent
							
								
									5363ec8a68
								
							
						
					
					
						commit
						728bab5fee
					
				| @ -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
									
								
							
							
						
						
									
										16
									
								
								src/api/user.ts
									
									
									
									
									
										Normal 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') | ||||||
|  | } | ||||||
| @ -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}</> | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user