add get token

This commit is contained in:
Vut Pov 2020-07-16 10:39:05 +07:00
parent decc6acca9
commit 0b41355439
3 changed files with 76 additions and 21 deletions

9
src/ResponseError.ts Normal file
View File

@ -0,0 +1,9 @@
class ResponseError extends Error{
response = null
constructor(message: string, response: any) {
super(message)
this.response = response
}
}
export default ResponseError

18
src/TokenInstance.ts Normal file
View File

@ -0,0 +1,18 @@
interface TokenInstance {
getToken: () => string
setToken: (token: string) => void
}
const tokenKey = 'token'
class LocalStorageToken implements TokenInstance {
getToken = () => {
return localStorage.getItem(tokenKey) || ''
}
setToken = (token: string) => {
localStorage.setItem(tokenKey, token)
}
}
export { LocalStorageToken, TokenInstance }

View File

@ -4,6 +4,8 @@ import BaseContextProvider, {
baseWithContext, baseWithContext,
baseContextWrap baseContextWrap
} from 'base-context-provider' } from 'base-context-provider'
import { LocalStorageToken, TokenInstance } from './TokenInstance'
import ResponseError from './ResponseError'
export interface AuthContextProps { export interface AuthContextProps {
authUrl: string authUrl: string
@ -14,15 +16,17 @@ export interface AuthState<T> {
isLogin: boolean isLogin: boolean
isFetchingUser: boolean isFetchingUser: boolean
user: T user: T
tokenInstance?: TokenInstance
} }
const Context = React.createContext({}) const Context = React.createContext({})
class AuthContextProvider extends BaseContextProvider< class AuthContextProvider extends BaseContextProvider<
AuthContextProps, AuthContextProps,
AuthState<any> AuthState<any>
> { > {
state = { state: AuthState<any> = {
isLogin: false, isLogin: false,
isFetchingUser: false, isFetchingUser: false,
user: { user: {
@ -30,16 +34,24 @@ class AuthContextProvider extends BaseContextProvider<
} }
} }
constructor(props: AuthContextProps) {
super(props)
this.state.tokenInstance = new LocalStorageToken()
}
getContext() { getContext() {
return Context return Context
} }
fetchUser: (password: string) => void = async (password) => { fetchUser: (username: string, password: string) => void = async (
username,
password
) => {
this.setState({ this.setState({
isFetchingUser: true isFetchingUser: true
}) })
const { authUrl } = this.props const { authUrl } = this.props
const { username } = this.state.user
const response = await fetch(authUrl, { const response = await fetch(authUrl, {
method: 'POST', method: 'POST',
@ -50,24 +62,35 @@ class AuthContextProvider extends BaseContextProvider<
}) })
const data = await response.json() const data = await response.json()
localStorage.setItem('token', data.token) if (response.status >= 400) {
return Promise.reject(new ResponseError("Error while fetching data", data ))
} }
// eslint-disable-next-line no-unused-expressions
this.state.tokenInstance?.setToken(data.token)
login = ({ username, password }: { username: string; password: string }) => { this.setState((oldState) => {
this.setState(
(oldState) => {
return { return {
...oldState, ...oldState,
user: { user: {
...oldState.user, ...oldState.user,
username username
}
}
}, },
() => { isLogin: true,
this.fetchUser(password) isFetchingUser: false
} }
) })
return Promise.resolve("")
}
login = async ({
username,
password
}: {
username: string
password: string
}) => {
return this.fetchUser(username, password)
} }
logout = async () => { logout = async () => {
@ -77,15 +100,20 @@ class AuthContextProvider extends BaseContextProvider<
username: '' username: ''
} }
}) })
this.state.tokenInstance?.setToken("")
return Promise.resolve(true) return Promise.resolve(true)
} }
getToken = ()=>{
return this.state.tokenInstance?.getToken()
}
getContextReturnValue() { getContextReturnValue() {
return { return {
...this.state, ...this.state,
login: this.login, login: this.login,
logout: this.logout logout: this.logout,
getToken: this.getToken
} }
} }
} }