{"id":1839,"date":"2026-05-22T14:40:55","date_gmt":"2026-05-22T12:40:55","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/?p=1839"},"modified":"2026-05-22T14:40:55","modified_gmt":"2026-05-22T12:40:55","slug":"react-springboot-authentication-part-3-protected-routes","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/react-springboot-authentication-part-3-protected-routes\/","title":{"rendered":"React SpringBoot Authentication \u2013 Part 3 (Protected Routes)"},"content":{"rendered":"<p>In this tutorial, you will learn how to protect some routes in your React application. We would create an Authentication Context and then we would create Protected Route component and use it to protect some routes n our application.<\/p>\n<ol>\n<li><a href=\"#t1\">Create the Authentication Context<\/a><\/li>\n<li><a href=\"#t2\">Create the Protected Route Component<\/a><\/li>\n<li><a href=\"#t3\">Protect Some Routes Using Protected Route Component<\/a><\/li>\n<li><a href=\"#t4\">Manage Login and Logout<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Create the Authentication Context<\/strong><\/h4>\n<p>To be able to manage authentication state across our application, we would use the useContext hook.<\/p>\n<p>You will need to create a file called AuthContext.js. In this file, you will create and export an Auth context as well as an AuthContext provider.<\/p>\n<p>The content of this file is given below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> React, { createContext, useContext, useState } from <span style=\"background-color: #fff0f0;\">'react'<\/span>\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">const<\/span> AuthContext <span style=\"color: #333333;\">=<\/span> createContext();\r\n  <span style=\"color: #008800; font-weight: bold;\">export<\/span> <span style=\"color: #008800; font-weight: bold;\">const<\/span> useAuth <span style=\"color: #333333;\">=<\/span> () <span style=\"color: #333333;\">=&gt;<\/span> useContext(AuthContext);\r\n  <span style=\"color: #008800; font-weight: bold;\">export<\/span> <span style=\"color: #008800; font-weight: bold;\">const<\/span> AuthProvider <span style=\"color: #333333;\">=<\/span> ({children}) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">const<\/span> [isAuthenticated, setIsAuthenticated] <span style=\"color: #333333;\">=<\/span> useState(() <span style=\"color: #333333;\">=&gt;<\/span> {\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> localStorage.getItem(<span style=\"background-color: #fff0f0;\">'isAuthenticated'<\/span>) <span style=\"color: #333333;\">===<\/span> <span style=\"color: #008800; font-weight: bold;\">true<\/span>\r\n  })\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">const<\/span> login <span style=\"color: #333333;\">=<\/span> () <span style=\"color: #333333;\">=&gt;<\/span> {\r\n    setIsAuthenticated(<span style=\"color: #008800; font-weight: bold;\">true<\/span>)\r\n    localStorage.setItem(<span style=\"background-color: #fff0f0;\">'isAuthenticated'<\/span>, <span style=\"color: #008800; font-weight: bold;\">true<\/span>)\r\n  }\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">const<\/span> logout <span style=\"color: #333333;\">=<\/span> () <span style=\"color: #333333;\">=&gt;<\/span> {\r\n    setIsAuthenticated(<span style=\"color: #008800; font-weight: bold;\">true<\/span>)\r\n    localStorage.setItem(<span style=\"background-color: #fff0f0;\">'isAuthenticated'<\/span>, <span style=\"color: #008800; font-weight: bold;\">false<\/span>)\r\n  }\r\n\r\n  <span style=\"color: #008800; font-weight: bold;\">return<\/span> (\r\n    <span style=\"color: #333333;\">&lt;<\/span>AuthContext.Provider value<span style=\"color: #333333;\">=<\/span>{{isAuthenticated, login, logout}}<span style=\"color: #333333;\">&gt;<\/span>\r\n        {children}\r\n    <span style=\"color: #333333;\">&lt;<\/span>\/AuthContext.Provider&gt;\r\n  );\r\n};\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Create the Protected Route Component<\/strong><\/h4>\n<p>To be able to protect some routes, we would need to have a wrapper around the Route component. The purpose would be to check whether a user is authenticated. If yes, the user is forwarded to the requested route, otherwise, he is redirected to the \/login route.<\/p>\n<p>The ProtectedRoute component is given below:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> ProtectedRoute <span style=\"color: #333333;\">=<\/span> ({children}) <span style=\"color: #333333;\">=&gt;<\/span>  {\r\n  <span style=\"color: #008800; font-weight: bold;\">const<\/span> {isAuthenticated} <span style=\"color: #333333;\">=<\/span> useAuth();\r\n  <span style=\"color: #008800; font-weight: bold;\">if<\/span>(<span style=\"color: #333333;\">!<\/span>isAuthenticated){\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #333333;\">&lt;<\/span>Navigate to<span style=\"color: #333333;\">=<\/span><span style=\"background-color: #fff0f0;\">\"\/login\"<\/span><span style=\"color: #333333;\">&gt;&lt;<\/span>\/Navigate&gt;\r\n  }<span style=\"color: #008800; font-weight: bold;\">else<\/span> {\r\n      <span style=\"color: #008800; font-weight: bold;\">return<\/span> children;\r\n  }\r\n}\r\n<span style=\"color: #008800; font-weight: bold;\">export<\/span> <span style=\"color: #008800; font-weight: bold;\">default<\/span> ProtectedRoute\r\n<\/pre>\n<p>What happens here is that we retrieve the isAuthenticated state from\u00a0 from the context and if it is not set, we would navigate to the Login component, otherwise, we would return the protected routes.<\/p>\n<p>We would then modify App component to protect some components.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Protect Some Routes Using Protected Route Component<\/strong><\/h4>\n<p>We would now modify our base App component to use our AuthProvider and Protected Route.<\/p>\n<p>So in our App component,\u00a0 we would make two changes:<\/p>\n<ol>\n<li>wrap the BrowserRouter inside the &lt;AuthProvider&gt; component<\/li>\n<li>include Protected Route component<\/li>\n<\/ol>\n<p>So to protect the Product component, you will have to wrap it into the &lt;ProtectedRoute&gt; component like so:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;Route<\/span> <span style=\"color: #0000cc;\">path=<\/span><span style=\"background-color: #fff0f0;\">\"\/product\"<\/span> <span style=\"color: #0000cc;\">element=<\/span><span style=\"background-color: #fff0f0;\">{&lt;ProtectedRoute<\/span><span style=\"color: #007700;\">&gt;&lt;Product&gt;&lt;\/Product&gt;<\/span> <span style=\"color: #007700;\">&lt;\/ProtectedRoute&gt;<\/span>} \/&gt;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Manage Login and Logout<\/strong><\/h4>\n<p>The two functions for managing user login and logout are available in the context and we just need to call each as needed.<\/p>\n<p><strong>For Login:<\/strong><\/p>\n<p>In the Login component, at successful user login we just need to call the login function and the same for logout.<\/p>\n<p>Import the useAuth hook as shown below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">import<\/span> { useAuth } from <span style=\"background-color: #fff0f0;\">'.\/AuthContext'<\/span>;\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Next, you need to call the login function just before you navigate to the loginSuccess page.<\/p>\n<p><strong>For Logout:<\/strong><\/p>\n<p>In the AppBar component, import the useAuth hook as before.<\/p>\n<p>Then you can write the logout event handler:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">const<\/span> logoutUser <span style=\"color: #333333;\">=<\/span> () <span style=\"color: #333333;\">=&gt;<\/span> {\r\n  logout()\r\n  navigate(<span style=\"background-color: #fff0f0;\">\"\/login\"<\/span>)\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Finally, in the Logout button, invoke the logoutUser() event handler<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to protect some routes in your React application. We would create an Authentication Context and then we would &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[414],"tags":[],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1839"}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=1839"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1839\/revisions"}],"predecessor-version":[{"id":1840,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/1839\/revisions\/1840"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=1839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=1839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=1839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}