- installer les dependencies
- remplacer
LOCAL_IP
(danslogin
) - démarrer le serveur
- installer les dependencies
- créer un component
Dashboard
import React from 'react'
const Dashboard = ({ user }) => {
return <p>Welcome, {user} 🎉</p>
}
export default Dashboard
- modifier
src/App.js
import React from 'react'
import LoginForm from './components/LoginForm.js'
import Dashboard from './components/Dashboard.js'
function App() {
return (
<div className="container my-4">
<h1 className="display-3 text-center mb-4">Authentification</h1>
<LoginForm />
<Dashboard />
<button type="button" className="btn btn-danger">
Log out
</button>
</div>
)
}
export default App
- Tous les components ne devraient pas être rendus sur la page en même temps, nous devons aussi passer
username
dans le componentDashboard
import React, { useState } from 'react'
import LoginForm from './components/LoginForm.js'
import Dashboard from './components/Dashboard.js'
function App() {
const [loggedIn, setLoggedIn] = useState(false)
const [username, setUsername] = useState('')
return (
<div className="container my-4">
<h1 className="display-3 text-center mb-4">Authentification</h1>
{!loggedIn && (
<LoginForm setUsername={setUsername} setLoggedIn={setLoggedIn} />
)}
{loggedIn && (
<>
<Dashboard user={username} />
<button className="btn btn-danger" onClick={handleLogoutClick}>
Log out
</button>
</>
)}
</div>
)
}
- Dans
src/components/LoginForm.js
import React from 'react'
const LoginForm = () => {
const handleFormSubmit = (event) => {
event.preventDefault()
fetch('http://192.168.1.70:7777/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username,
password,
}),
})
.then((response) => {
console.log(response)
return response.json()
})
.catch((error) => console.error(error))
}
return (
<form onSubmit={handleFormSubmit} className="mt-4">
{/* ... */}
</form>
)
}
Nous devons ajouter middleware:
// login/src/app.js
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*')
res.header('Access-Control-Allow-Headers', 'Content-Type')
next()
})
- Nous allons remplacer
res.send(string)
parres.send(object)
(res.json(object)
fonctionne aussi)
// src/app.js
// avant (x2) res.send('Username or password invalid')
res.send({ valid: false })
// avant res.send(`Welcome to your dashboard ${username}`)
res.send({ valid: true, username })
- Nous allons completer
handleFormSubmit
danssrc/components/LoginForm.js
//
const handleFormSubmit = (e) => {
e.preventDefault()
const username = e.target.elements.username.value
const password = e.target.elements.password.value
e.target.reset()
fetch('http://192.168.1.70:7777/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username,
password,
}),
})
.then((response) => {
console.log(response)
return response.json()
})
.then((data) => {
if (data.valid) {
setLoggedIn(true)
setUsername(data.username)
} else {
alert('Your credentials are not valid, try again ;)')
}
})
.catch((error) => console.error(error))
}
- Finallemend dans
src/App.js
nous allons ajouter handler pour le bouton
import React, { useState } from 'react'
import LoginForm from './components/LoginForm.js'
import Dashboard from './components/Dashboard'
function App() {
const [loggedIn, setLoggedIn] = useState(false)
const [username, setUsername] = useState('')
const handleLogoutClick = () => {
setUsername('')
setLoggedIn(false)
}
return (
<div className="container my-4">
<h1 className="display-3 text-center mb-4">Authentification</h1>
{/* ... */}
{loggedIn && (
<>
<Dashboard user={username} />
<button className="btn btn-danger" onClick={handleLogoutClick}>
Log out
</button>
</>
)}
</div>
)
}
export default App