Authentication & Authorization 2
Authentication & Authorization 2
JSON Web Token is a standard used to create Access Tokens. These access tokens are
also called JWT Tokens.
The client uses these access tokens on every subsequent request to communicate with
the Server.
Note
While making HTTP Request, we have to send an access token in the HTTP Headers with
the key Authorization.
Example:
When we store the JWT Token in the state,On page refresh, the JWT token won't be
available
It is difficult to pass state information to every component
2. Storage Mechanisms
Local Storage
Cookies
Session Storage
IndexedDB, etc.
3. Cookies
A cookie is a piece of data that is stored on the user's computer by the web
browser.
Installation Command:
3.3.1 Cookies.set()
Cookies.get()
Syntax:
Cookies.get('CookieName');
eg:Cookies.get('ACCESS_TOKEN');
cookies.remove()
Syntax:
Cookies.remove('CookieName');
eg:Cookies.remove('ACCESS_TOKEN');
Note
The Redirect component uses the history push and replace methods behinds the scene.
withRouter
The history prop will be available for only components which are directly given for
Route.
To provide history prop to other components, we can wrap it with the withRouter
function while exporting it.
Example:
-----------------------------------------------------------------------------------
----------------------------
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/Cart/index.js
###################################################################################
#######################
File: src/components/Header/index.js
import './index.css'
###################################################################################
################
File: src/components/Home/index.js
import './index.css'
return (
<>
<Header />
<div className="home-container">
<div className="home-content">
<h1 className="home-heading">Clothes That Get YOU Noticed</h1>
<img
src="https://assets.ccbp.in/frontend/react-js/nxt-trendz-home-img.png"
alt="dresses to be noticed"
className="home-mobile-img"
/>
<p className="home-description">
Fashion is part of the daily air and it does not quite help that it
changes all the time. Clothes have always been a marker of the era
and we are in a revolution. Your fashion makes you been seen and
heard that way you are. So, celebrate the seasons new and exciting
fashion in your own way.
</p>
<button type="button" className="shop-now-button">
Shop Now
</button>
</div>
<img
src="https://assets.ccbp.in/frontend/react-js/nxt-trendz-home-img.png"
alt="dresses to be noticed"
className="home-desktop-img"
/>
</div>
</>
)
}
###################################################################################
##############
File: src/components/LoginForm/index.js
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() {
const {showSubmitError, errorMsg} = this.state
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>
{showSubmitError && <p className="error-message">*{errorMsg}</p>}
</form>
</div>
)
}
}
File: src/components/NotFound/index.js
import './index.css'
###################################################################################
################
File: src/components/Products/index.js
import './index.css'
###################################################################################
##############