Authentication & Authorization
Authentication & Authorization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Authentication
Authorization
Example:
Route Parameters
When a component is rendered by the Route, some additional props are passed.
They are:
->match
->history
->location
History
The history object has some methods to control the navigation in the browser, and
it also maintains the
history of the routes we navigated.
->push()
->replace()
->go()
->goBack()
->goForward(), etc.
3.1.1 history.push()
With the history.push() method, the user can go forward and backwards in the
browser, and the URL will change.
Syntax:
history.push("PATH");
history.replace()
The history.replace() method replaces the current URL with new one.
The user can't go backwards to the previous URL.
Syntax:
history.replace("PATH");
___________________________________________________________________________________
_______________________________
E-Commerce Application
Make an Authentication Request to Login API
Handle Login API Response
On Login Success
On Login Failure
Store the JWT Token
File: src/App.js
import './App.css'
###################################################################################
####################
File: src/components/LoginForm/index.js
import './index.css'
onSubmitSuccess = () => {
const {history} = this.props
history.replace('/')
}
renderPasswordField = () => {
const {password} = this.state
return (
<>
<label className="input-label" htmlFor="password">
PASSWORD
</label>
<input
type="password"
id="password"
className="password-input-filed"
value={password}
onChange={this.onChangePassword}
/>
</>
)
}
renderUsernameField = () => {
const {username} = this.state
return (
<>
<label className="input-label" htmlFor="username">
USERNAME
</label>
<input
type="text"
id="username"
className="username-input-filed"
value={username}
onChange={this.onChangeUsername}
/>
</>
)
}
render() {
return (
<div className="login-form-container">
<img
src="https://assets.ccbp.in/frontend/react-js/nxt-trendz-logo-img.png"
className="login-website-logo-mobile-image"
alt="website logo"
/>
<img
src="https://assets.ccbp.in/frontend/react-js/nxt-trendz-login-img.png"
className="login-image"
alt="website login"
/>
<form className="form-container" onSubmit={this.submitForm}>
<img
src="https://assets.ccbp.in/frontend/react-js/nxt-trendz-logo-img.png"
className="login-website-logo-desktop-image"
alt="website logo"
/>
<div className="input-container">{this.renderUsernameField()}</div>
<div className="input-container">{this.renderPasswordField()}</div>
<button type="submit" className="login-button">
Login
</button>
</form>
</div>
)
}
}
Note
->If the Response status code is 2XX, then response.ok will be true else it is
false.
->Whenever the route changes, the switch in the App.js will trigger again, and the
corresponding component will render.
###################################################################################
########################
File: src/components/Home/index.js
###################################################################################
################
File: src/components/Cart/index.js
###################################################################################
#################
File: src/components/Products/index.js
import './index.css'
###################################################################################
##########################
File: src/components/NotFound/index.js
import "./index.css";
___________________________________________________________________________________
_____________________________
Username: rahul
password: rahul@2021
___________________________________________________________________________________
________________________________