完善产品购买页面,抽取公共组件,优化导航链接
This commit is contained in:
41
src/components/providers/AuthProvider.tsx
Normal file
41
src/components/providers/AuthProvider.tsx
Normal file
@@ -0,0 +1,41 @@
|
||||
'use client'
|
||||
import {User} from '@/lib/models'
|
||||
import {createContext, ReactNode, useEffect, useState} from 'react'
|
||||
import {getProfile} from '@/actions/auth/auth'
|
||||
|
||||
type AuthContentType = {
|
||||
profile: User | null
|
||||
refreshProfile: () => Promise<void>
|
||||
}
|
||||
|
||||
export const AuthContext = createContext<AuthContentType>({
|
||||
profile: null,
|
||||
refreshProfile: async () => {
|
||||
throw new Error('Not implemented')
|
||||
},
|
||||
})
|
||||
|
||||
export type ProfileProviderProps = {
|
||||
children: ReactNode
|
||||
}
|
||||
|
||||
export default function AuthProvider(props: ProfileProviderProps) {
|
||||
|
||||
const [profile, setProfile] = useState<User | null>(null)
|
||||
|
||||
const refreshProfile = async () => {
|
||||
setProfile(await getProfile(true))
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
refreshProfile().then()
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<AuthContext.Provider value={{
|
||||
profile, refreshProfile,
|
||||
}}>
|
||||
{props.children}
|
||||
</AuthContext.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user