{"id":2037,"date":"2025-05-16T20:41:47","date_gmt":"2025-05-16T12:41:47","guid":{"rendered":"https:\/\/www.tonghin.com.my\/blog\/?p=2037"},"modified":"2025-05-16T20:41:48","modified_gmt":"2025-05-16T12:41:48","slug":"securing-routes-and-role-based-access-in-next-js","status":"publish","type":"post","link":"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/","title":{"rendered":"Securing Routes and Role-Based Access in Next.js"},"content":{"rendered":"\n<p>Monday, April 28 2025 \u2013 Last week, I continued working on restricting unauthorized users from directly accessing UI pages, such as when they try to search for a page URL manually. To guide me, I referred to a blog titled &#8220;Authorization in Next.js&#8221; by Robin Wieruch, published on March 25, 2025. The blog explored several methods for implementing authorization, and I decided to focus on securing routing.<\/p>\n\n\n\n<p>One key takeaway from the blog was the importance of a defense mechanism in place for routing. Without it, unauthorized users might still manage to navigate to the page (which is a security risk). However, if the necessary authorization checks are implemented within the API, Service, and Data Access Layers, unauthorized users won&#8217;t be able to read or modify any data. Still, adding this layer of protection not only enhances security but also improves the user experience, which is why it&#8217;s highly recommended.<\/p>\n\n\n\n<p>To implement this in my project, I placed all related files into an &#8220;authorized&#8221; folder and created a layout file that included the necessary security check. The layout file essentially checks whether the user has access; if they don\u2019t, they\u2019re automatically redirected to the login page.<\/p>\n\n\n\n<p>Once that was in place, I focused on implementing role-based authorization, with admin users. I customized the navigation bar to ensure that features specific to certain roles only appear for users with the correct role. To do this, I added a &#8220;role&#8221; property to the page configuration so that only users with the &#8220;Admin&#8221; role could see certain features in the navbar.<\/p>\n\n\n\n<p>Finally, I worked on page-level authorization for each individual page. For each one, I added a check to determine the user&#8217;s role. If the user is an admin, they can access the page\u2019s data; otherwise, they will be shown an &#8220;Unauthorized&#8221; page. This added layer of security ensures that only authorized users can access specific content.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monday, April 28 2025 \u2013 Last week, I continued working on restricting unauthorized users from directly accessing UI pages, such as when they try to search for a page URL manually. To guide me, I referred to a blog titled &#8220;Authorization in Next.js&#8221; by Robin Wieruch, published on March 25, 2025. The blog explored several &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Securing Routes and Role-Based Access in Next.js&#8221;<\/span><\/a><\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v15.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Securing Routes and Role-Based Access in Next.js | Tong Hin&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Securing Routes and Role-Based Access in Next.js | Tong Hin&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Monday, April 28 2025 \u2013 Last week, I continued working on restricting unauthorized users from directly accessing UI pages, such as when they try to search for a page URL manually. To guide me, I referred to a blog titled &#8220;Authorization in Next.js&#8221; by Robin Wieruch, published on March 25, 2025. The blog explored several &hellip; Continue reading &quot;Securing Routes and Role-Based Access in Next.js&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/\" \/>\n<meta property=\"og:site_name\" content=\"Tong Hin&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-16T12:41:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-16T12:41:48+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"2 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#organization\",\"name\":\"TONG HIN MACHINERY\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"sameAs\":[\"https:\/\/www.youtube.com\/channel\/UC60EoxJMNbu8mV0LuEGfrBg\"],\"logo\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#logo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/wp-content\/uploads\/2020\/10\/web_logo_180_180.jpg\",\"width\":180,\"height\":180,\"caption\":\"TONG HIN MACHINERY\"},\"image\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#logo\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#website\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"name\":\"Tong Hin&#039;s Blog\",\"description\":\"Sharing About Technology\",\"publisher\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.tonghin.com.my\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/#webpage\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/\",\"name\":\"Securing Routes and Role-Based Access in Next.js | Tong Hin&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#website\"},\"datePublished\":\"2025-05-16T12:41:47+00:00\",\"dateModified\":\"2025-05-16T12:41:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/\",\"name\":\"Securing Routes and Role-Based Access in Next.js\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/#webpage\"},\"author\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#\/schema\/person\/81e2366362d8d14b15519395755cee6f\"},\"headline\":\"Securing Routes and Role-Based Access in Next.js\",\"datePublished\":\"2025-05-16T12:41:47+00:00\",\"dateModified\":\"2025-05-16T12:41:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/16\/securing-routes-and-role-based-access-in-next-js\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#organization\"},\"articleSection\":\"Experiential\",\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#\/schema\/person\/81e2366362d8d14b15519395755cee6f\",\"name\":\"nurlailiyuzi\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/5c0d4cc6f7fc881bd56d95bdbb0c4c14?s=96&d=mm&r=g\",\"caption\":\"nurlailiyuzi\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2037"}],"collection":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/comments?post=2037"}],"version-history":[{"count":1,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2037\/revisions"}],"predecessor-version":[{"id":2038,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2037\/revisions\/2038"}],"wp:attachment":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/media?parent=2037"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/categories?post=2037"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/tags?post=2037"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}