{"id":7400,"date":"2023-09-23T18:46:33","date_gmt":"2023-09-23T13:16:33","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7400"},"modified":"2023-09-23T18:46:35","modified_gmt":"2023-09-23T13:16:35","slug":"session-implementation-in-react-js","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/session-implementation-in-react-js\/","title":{"rendered":"Session Implementation In React Js"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Introduction:<\/strong><br>Session Implementation is essential for managing user authentication and preserving user-specific data in React applications. It allows the application to keep track of a user&#8217;s login status, ensuring it can provide personalized experiences and secure access to authorized users.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Local storage for session management:<\/strong><br>Use the localstorage.setItem and localstorage.getItem functions to store and retrieve the isLoggedIn state.<br>const[isLoggedIn,setIsLoggedIn]=useState(localStorage.getItem(&#8216;isLoggedIn&#8217;) ===&#8217;true&#8217;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Initialize isLoggedIn State:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Initialize the isLoggedIn state using the value retrieved from local storage. Update the state when the User logs in or logs out.<br><br><strong>Redirect on login:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implement handleLoginSuccess function, to set the isLoggedIn state to true when a user successfully logs in. Redirect the User to the dashboard upon successful login<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">const handleLognSuccess = () =&gt;{<br>     setIsLoggedIn(true); <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">      localstorage.setItem(&#8216;isLoggedIn&#8217;,&#8217;true&#8217;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">      history.push(&#8216;\/dashboard&#8217;);<br>};<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note: make sure to call the handleLoginSuccess function in the Login API Response upon successful login.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Logout  functionality:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create handleLogout function to set the isLoggedIn state to false when the user logs out. clear the local storage to remove session data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Redirect the user to the login page after logging out.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">const handleLogout = () =&gt;{<br>setIsLoggedIn(false);<br>\/\/clear session data from local storage<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">localstorage.removeItem(&#8216;isLoggedIn&#8217;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/Redirect to login page<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">history.push(&#8216;\/&#8217;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">};<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conditional Rendering:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">conditionally render Navbar and Sidenav based on the isLoggedIn state.show navigation menus and user-specific content when the user is logged in.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{isLoggedIn &amp;&amp; (<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;&gt;<br>&lt;Navbar handleDraweropen={handleDraweropen} handleLogout={handleLogout} \/&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&lt;Sidenav mobileopen={mobileopen} handleDraweropen={handleDraweropen} handleDrawerClose={handleDrawerClose} isLoggedIn={isLoggedIn} handleLogout={handleLogout} \/&gt;<br>&lt;\/&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">)}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><br><strong>Protecting Routes:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Implement a PrivateRoute component to protect specific routes for authenticated users. Use React Router to ensure that only authenticated users can access certain pages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">import { Route, Redirect } from &#8216;react-router-dom&#8217;;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">function PrivateRoute({ component: Component, isLoggedIn, \u2026rest }) {<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">return (<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">{&#8230;rest}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">render={(props) =&gt; isLoggedIn ? &lt;Component {&#8230;props} \/&gt; : &lt;Redirect to=&#8221;\/&#8221; \/&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/&gt;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">}<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\/\/ Usage;<br>&lt;PrivateRoute exact path=&#8221;\/dashboard&#8221; component={Dashboard} isLoggedIn={isLoggedIn} \/&gt;<br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conclusion:<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Session management in React provides the foundation for secure authentication and personalized user experiences.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction:Session Implementation is essential for managing user authentication and preserving user-specific data in React applications. It allows the application to [&hellip;]<\/p>\n","protected":false},"author":501,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-7400","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Vm","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/501"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7400"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7400\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}