Created
November 11, 2020 23:52
-
-
Save blogcacanid/6135c76dd1b129670f7665a11bb613d5 to your computer and use it in GitHub Desktop.
Login.js Authentication JWT React JS Lumen 7
This file contains 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 React, { useState, useRef } from "react"; | |
import { useDispatch, useSelector } from "react-redux"; | |
import { Redirect } from 'react-router-dom'; | |
import Form from "react-validation/build/form"; | |
import Input from "react-validation/build/input"; | |
import CheckButton from "react-validation/build/button"; | |
import { login } from "../actions/auth"; | |
const required = (value) => { | |
if (!value) { | |
return ( | |
<div className="alert alert-danger" role="alert"> | |
This field is required! | |
</div> | |
); | |
} | |
}; | |
const Login = (props) => { | |
const form = useRef(); | |
const checkBtn = useRef(); | |
const [username, setUsername] = useState(""); | |
const [password, setPassword] = useState(""); | |
const [loading, setLoading] = useState(false); | |
const { isLoggedIn } = useSelector(state => state.auth); | |
const { message } = useSelector(state => state.message); | |
const dispatch = useDispatch(); | |
const onChangeUsername = (e) => { | |
const username = e.target.value; | |
setUsername(username); | |
}; | |
const onChangePassword = (e) => { | |
const password = e.target.value; | |
setPassword(password); | |
}; | |
const handleLogin = (e) => { | |
e.preventDefault(); | |
setLoading(true); | |
form.current.validateAll(); | |
if (checkBtn.current.context._errors.length === 0) { | |
dispatch(login(username, password)) | |
.then(() => { | |
props.history.push("/profile"); | |
window.location.reload(); | |
}) | |
.catch(() => { | |
setLoading(false); | |
}); | |
} else { | |
setLoading(false); | |
} | |
}; | |
if (isLoggedIn) { | |
return <Redirect to="/profile" />; | |
} | |
return ( | |
<div className="col-md-12"> | |
<div className="card card-container"> | |
<img | |
src="//ssl.gstatic.com/accounts/ui/avatar_2x.png" | |
alt="profile-img" | |
className="profile-img-card" | |
/> | |
<Form onSubmit={handleLogin} ref={form}> | |
<div className="form-group"> | |
<label htmlFor="username">Username</label> | |
<Input | |
type="text" | |
className="form-control" | |
name="username" | |
value={username} | |
onChange={onChangeUsername} | |
validations={[required]} | |
placeholder="Enter username" | |
/> | |
</div> | |
<div className="form-group"> | |
<label htmlFor="password">Password</label> | |
<Input | |
type="password" | |
className="form-control" | |
name="password" | |
value={password} | |
onChange={onChangePassword} | |
validations={[required]} | |
placeholder="Enter password" | |
/> | |
</div> | |
<div className="form-group"> | |
<button className="btn btn-primary btn-block" disabled={loading}> | |
{loading && ( | |
<span className="spinner-border spinner-border-sm"></span> | |
)} | |
<span>Login</span> | |
</button> | |
</div> | |
{message && ( | |
<div className="form-group"> | |
<div className="alert alert-danger" role="alert"> | |
{message} | |
</div> | |
</div> | |
)} | |
<CheckButton style={{ display: "none" }} ref={checkBtn} /> | |
</Form> | |
</div> | |
</div> | |
); | |
}; | |
export default Login; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment