Authentication & Authorization 3
Authentication & Authorization 3
Route
User defined Component
Redirect
2. Wrapper Component
Redirection Logic can be reused by separating out into a React Component called
Wrapper Component.
Each route will be wrapped with it.
File: src/components/ProtectedRoute/index.js
3. E-Commerce Application
File: src/App.js
import "./App.css";
###################################################################################
#################
File: src/components/AllProductsSection/index.js
componentDidMount() {
this.getProducts();
}
renderProductsList = () => {
const { productsList } = this.state;
return (
<div>
<h1 className="products-list-heading">All Products</h1>
<ul className="products-list">
{productsList.map((product) => (
<ProductCard productData={product} key={product.id} />
))}
</ul>
</div>
);
};
render() {
return <>{this.renderProductsList()}</>;
}
}
File: src/components/Header/index.js
import "./index.css";
File: src/components/Home/index.js
File: src/components/LoginForm/index.js
import "./index.css";
Cookies.set("jwt_token", jwtTkoken, {
expires: 30,
path: "/",
});
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() {
const { showSubmitError, errorMsg } = this.state;
const jwtToken = Cookies.get("jwt_token");
if (jwtToken !== undefined) {
return <Redirect to="/" />;
}
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/ProductCard/index.js
import "./index.css";
return (
<li className="product-item">
<img src={imageUrl} alt="product" className="thumbnail" />
<h1 className="title">{title}</h1>
<p className="brand">by {brand}</p>
<div className="product-details">
<p className="price">Rs {price}/-</p>
<div className="rating-container">
<p className="rating">{rating}</p>
<img
src="https://assets.ccbp.in/frontend/react-js/star-img.png"
alt="star"
className="star"
/>
</div>
</div>
</li>
);
};
export default ProductCard;
###################################################################################
##########################
File: src/components/Products/index.js
import "./index.css";
File: src/components/ProtectedRoute/index.js