Last active
February 22, 2019 09:28
-
-
Save GenesisSam/e2f689383198e18d2adf08bf9120c1f0 to your computer and use it in GitHub Desktop.
Using Hook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import * as CookieHandler from "js-cookie"; | |
import { Base64 } from "js-base64"; | |
const getAuthToken = () => { | |
const authToken = CookieHandler.get("auth_token"); | |
if (authToken) { | |
return Base64.decode(authToken); | |
} | |
return ""; | |
}; | |
function useAuthState( | |
state?: boolean, | |
): [boolean, (params: { id: string; password: string }) => void] { | |
const [loginState, setLogin] = React.useState(false); | |
function dispatch(params: { id: string; password: string }) { | |
try { | |
const encodedParams = Base64.encode(JSON.stringify(params)); | |
CookieHandler.set("auth_token", encodedParams); | |
setLogin(true); | |
} catch (err) { | |
setLogin(false); | |
} | |
} | |
if (state) { | |
setLogin(state); | |
return [state, dispatch]; | |
} else { | |
const authToken = CookieHandler.get("auth_token"); | |
return [Boolean(authToken), dispatch]; | |
} | |
} | |
export { getAuthToken, useAuthState }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import { useAuthState } from "../../helpers/authHelper"; | |
const Home = () => { | |
const [authState] = useAuthState(); | |
return ( | |
<div> | |
HELLO WORLD! We R Vingle Developers!! | |
<br /> | |
{authState ? "로그인을 환영합니다!!" : null} | |
</div> | |
); | |
}; | |
export default Home; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as React from "react"; | |
import { useAuthState } from "../../helpers/authHelper"; | |
import { Redirect } from "react-router"; | |
const SignIn = () => { | |
const [username, setUsername] = React.useState(""); | |
const [password, setPassword] = React.useState(""); | |
const [isSignedIn, setAuth] = useAuthState(); | |
if (isSignedIn) { | |
return <Redirect to="/home" />; | |
} | |
const changeUsername: React.ChangeEventHandler<HTMLInputElement> = e => { | |
setUsername(e.currentTarget.value); | |
}; | |
const changePassword: React.ChangeEventHandler<HTMLInputElement> = e => { | |
setPassword(e.currentTarget.value); | |
}; | |
const handleSubmit = () => { | |
setAuth({ | |
id: username, | |
password, | |
}); | |
}; | |
return ( | |
<div> | |
<div> | |
<input | |
placeholder="Username" | |
type="text" | |
value={username} | |
onChange={changeUsername} | |
/> | |
</div> | |
<div> | |
<input | |
placeholder="Password" | |
type="password" | |
value={password} | |
onChange={changePassword} | |
/> | |
</div> | |
<input type="button" onClick={handleSubmit} value="Sign In" /> | |
</div> | |
); | |
}; | |
export default SignIn; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment