|
|
|
@ -1,7 +1,9 @@
@@ -1,7 +1,9 @@
|
|
|
|
|
import React, { useState, useContext, ReactNode } from "react" |
|
|
|
|
import useMount from "../hook/useMount" |
|
|
|
|
import React, { useState, useContext, ReactNode } from "react"; |
|
|
|
|
import { FullPageLoading } from "../components/lib"; |
|
|
|
|
import useAsync from "../hook/useAsync"; |
|
|
|
|
import useMount from "../hook/useMount"; |
|
|
|
|
import { request } from "../utils/request"; |
|
|
|
|
import * as auth from "../utils/token" |
|
|
|
|
import * as auth from "../utils/token"; |
|
|
|
|
|
|
|
|
|
interface User { |
|
|
|
|
id: string; |
|
|
|
@ -9,52 +11,69 @@ interface User {
@@ -9,52 +11,69 @@ interface User {
|
|
|
|
|
email: string; |
|
|
|
|
title: string; |
|
|
|
|
organization: string; |
|
|
|
|
token: string |
|
|
|
|
token: string; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
interface AuthForm { |
|
|
|
|
username: string, |
|
|
|
|
password: string |
|
|
|
|
username: string; |
|
|
|
|
password: string; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
interface AuthContext { |
|
|
|
|
user: User | null |
|
|
|
|
login: (form: AuthForm) => Promise<void> |
|
|
|
|
register: (form: AuthForm) => Promise<void> |
|
|
|
|
logout: () => Promise<void> |
|
|
|
|
user: User | null; |
|
|
|
|
login: (form: AuthForm) => Promise<void>; |
|
|
|
|
register: (form: AuthForm) => Promise<void>; |
|
|
|
|
logout: () => Promise<void>; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const AuthContext = React.createContext<AuthContext | undefined>(undefined) |
|
|
|
|
AuthContext.displayName = 'AuthContext' |
|
|
|
|
const AuthContext = React.createContext<AuthContext | undefined>(undefined); |
|
|
|
|
AuthContext.displayName = "AuthContext"; |
|
|
|
|
|
|
|
|
|
const bootstrapUser = async () => { |
|
|
|
|
let user = null |
|
|
|
|
const token = auth.getToken() |
|
|
|
|
let user = null; |
|
|
|
|
const token = auth.getToken(); |
|
|
|
|
if (token) { |
|
|
|
|
const data = await request('/me', { token }) |
|
|
|
|
user = data.user |
|
|
|
|
const data = await request("/me", { token }); |
|
|
|
|
user = data.user; |
|
|
|
|
} |
|
|
|
|
return Promise.resolve(user) |
|
|
|
|
} |
|
|
|
|
return Promise.resolve(user); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export const AuthProvider = ({ children }: { children: ReactNode }) => { |
|
|
|
|
const [user, setUser] = useState<User | null>(null) |
|
|
|
|
const { |
|
|
|
|
data: user, |
|
|
|
|
isLoading, |
|
|
|
|
isIdle, |
|
|
|
|
run, |
|
|
|
|
setData: setUser, |
|
|
|
|
} = useAsync<User | null>(); |
|
|
|
|
|
|
|
|
|
const login = (form: AuthForm) => auth.login(form).then(setUser) |
|
|
|
|
const register = (form: AuthForm) => auth.register(form).then(setUser) |
|
|
|
|
const logout = () => auth.logout().then(() => setUser(null)) |
|
|
|
|
const login = (form: AuthForm) => auth.login(form).then(setUser); |
|
|
|
|
const register = (form: AuthForm) => auth.register(form).then(setUser); |
|
|
|
|
const logout = () => auth.logout().then(() => setUser(null)); |
|
|
|
|
|
|
|
|
|
useMount(() => {bootstrapUser().then(setUser)}) |
|
|
|
|
useMount(() => { |
|
|
|
|
run(bootstrapUser()); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
return <AuthContext.Provider children={children} value={{ user, login, register, logout }} /> |
|
|
|
|
} |
|
|
|
|
if (isIdle || isLoading) { |
|
|
|
|
return <FullPageLoading />; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<AuthContext.Provider |
|
|
|
|
children={children} |
|
|
|
|
value={{ user, login, register, logout }} |
|
|
|
|
/> |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Auth Context hook |
|
|
|
|
* @returns |
|
|
|
|
* @returns |
|
|
|
|
*/ |
|
|
|
|
export const useAuth = () => { |
|
|
|
|
const context = useContext(AuthContext) |
|
|
|
|
if (!context) throw new Error("useAuth必须在AuthProvider中使用") |
|
|
|
|
return context |
|
|
|
|
} |
|
|
|
|
const context = useContext(AuthContext); |
|
|
|
|
if (!context) throw new Error("useAuth必须在AuthProvider中使用"); |
|
|
|
|
return context; |
|
|
|
|
}; |
|
|
|
|