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,
baseContextWrap
} from 'base-context-provider'
import { LocalStorageToken, TokenInstance } from './TokenInstance'
import ResponseError from './ResponseError'
export interface AuthContextProps {
authUrl: string
@ -14,15 +16,17 @@ export interface AuthState<T> {
isLogin: boolean
isFetchingUser: boolean
user: T
tokenInstance?: TokenInstance
}
const Context = React.createContext({})
class AuthContextProvider extends BaseContextProvider<
AuthContextProps,
AuthState<any>
> {
state = {
state: AuthState<any> = {
isLogin: false,
isFetchingUser: false,
user: {
@ -30,16 +34,24 @@ class AuthContextProvider extends BaseContextProvider<
}
}
constructor(props: AuthContextProps) {
super(props)
this.state.tokenInstance = new LocalStorageToken()
}
getContext() {
return Context
}
fetchUser: (password: string) => void = async (password) => {
fetchUser: (username: string, password: string) => void = async (
username,
password
) => {
this.setState({
isFetchingUser: true
})
const { authUrl } = this.props
const { username } = this.state.user
const response = await fetch(authUrl, {
method: 'POST',
@ -50,24 +62,35 @@ class AuthContextProvider extends BaseContextProvider<
})
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 {
...oldState,
user: {
...oldState.user,
username
}
}
},
() => {
this.fetchUser(password)
isLogin: true,
isFetchingUser: false
}
)
})
return Promise.resolve("")
}
login = async ({
username,
password
}: {
username: string
password: string
}) => {
return this.fetchUser(username, password)
}
logout = async () => {
@ -77,15 +100,20 @@ class AuthContextProvider extends BaseContextProvider<
username: ''
}
})
this.state.tokenInstance?.setToken("")
return Promise.resolve(true)
}
getToken = ()=>{
return this.state.tokenInstance?.getToken()
}
getContextReturnValue() {
return {
...this.state,
login: this.login,
logout: this.logout
logout: this.logout,
getToken: this.getToken
}
}
}