{"id":2047,"date":"2025-05-26T23:29:24","date_gmt":"2025-05-26T15:29:24","guid":{"rendered":"https:\/\/www.tonghin.com.my\/blog\/?p=2047"},"modified":"2025-05-26T23:29:25","modified_gmt":"2025-05-26T15:29:25","slug":"fixing-token-expiry-access-in-next-js-layout","status":"publish","type":"post","link":"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/","title":{"rendered":"Fixing Token Expiry Access in Next.js Layout"},"content":{"rendered":"\n<p>Monday, May 19, 2025 \u2013 Last week\u2019s issue was related to authentication. Previously, I had already implemented an authentication layout to prevent outsiders or logged-out users from accessing UI pages. Although the backend was already secure, blocking unauthorized data access, we still didn\u2019t want unauthenticated users to even see the UI. That\u2019s why the layout-based authentication was put in place.<br>However, I noticed a new problem: when a user\u2019s token expired, they could still access the <\/p>\n\n\n\n<p>UI pages. The backend continued to block the data, but the frontend still rendered the components, which wasn\u2019t ideal. So, I decided to add another layer of protection by checking whether the token had expired and redirecting the user to the login page if it had.<br>On my first attempt, I tried adding the logic directly in the layout file, but I immediately ran into an error \u201cwindow or localStorage is not defined\u201d. This happens because Next.js layout files run on the server by default, and these browser-specific objects don\u2019t exist in that context.<\/p>\n\n\n\n<p>After looking into it further, I created a custom hook to handle the token expiration logic and tried calling it from the layout. That also didn\u2019t work, since React hooks can only be used inside client components, and layouts are server components by default.<br>To get around this, I created a new client-only component specifically for token validation. I then placed this component inside the layout, which made the logic work correctly. Thanks to Mr. Peter\u2019s tip on how to simulate an expired token, I was able to test the flow and confirm it behaved as expected.<\/p>\n\n\n\n<p>This also required me to store another value in localStorage, \u201csessionExpiry\u201d in addition to the existing \u201cisLoggingOut\u201d. With both of these in place, I could now reliably determine whether a user should still have access. The session is considered expired if either there\u2019s no token or if the current time has passed the session expiry timestamp. Now, the UI is finally in sync with the authentication state, users are redirected when they\u2019re no longer supposed to be there.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Monday, May 19, 2025 \u2013 Last week\u2019s issue was related to authentication. Previously, I had already implemented an authentication layout to prevent outsiders or logged-out users from accessing UI pages. Although the backend was already secure, blocking unauthorized data access, we still didn\u2019t want unauthenticated users to even see the UI. That\u2019s why the layout-based &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Fixing Token Expiry Access in Next.js Layout&#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>Fixing Token Expiry Access in Next.js Layout | 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\/26\/fixing-token-expiry-access-in-next-js-layout\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Fixing Token Expiry Access in Next.js Layout | Tong Hin&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Monday, May 19, 2025 \u2013 Last week\u2019s issue was related to authentication. Previously, I had already implemented an authentication layout to prevent outsiders or logged-out users from accessing UI pages. Although the backend was already secure, blocking unauthorized data access, we still didn\u2019t want unauthenticated users to even see the UI. That\u2019s why the layout-based &hellip; Continue reading &quot;Fixing Token Expiry Access in Next.js Layout&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/\" \/>\n<meta property=\"og:site_name\" content=\"Tong Hin&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-26T15:29:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-05-26T15:29:25+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\/26\/fixing-token-expiry-access-in-next-js-layout\/#webpage\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/\",\"name\":\"Fixing Token Expiry Access in Next.js Layout | Tong Hin&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#website\"},\"datePublished\":\"2025-05-26T15:29:24+00:00\",\"dateModified\":\"2025-05-26T15:29:25+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/#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\/26\/fixing-token-expiry-access-in-next-js-layout\/\",\"url\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/\",\"name\":\"Fixing Token Expiry Access in Next.js Layout\"}}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/#webpage\"},\"author\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/#\/schema\/person\/81e2366362d8d14b15519395755cee6f\"},\"headline\":\"Fixing Token Expiry Access in Next.js Layout\",\"datePublished\":\"2025-05-26T15:29:24+00:00\",\"dateModified\":\"2025-05-26T15:29:25+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.tonghin.com.my\/blog\/2025\/05\/26\/fixing-token-expiry-access-in-next-js-layout\/#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\/2047"}],"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=2047"}],"version-history":[{"count":2,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2047\/revisions"}],"predecessor-version":[{"id":2053,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/posts\/2047\/revisions\/2053"}],"wp:attachment":[{"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/media?parent=2047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/categories?post=2047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tonghin.com.my\/blog\/wp-json\/wp\/v2\/tags?post=2047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}