You are relying on client-side validation, which is not recommended in Next.js. Instead, you should handle redirection to the login page on the server-side component. Additionally, it is better to use cookies for session management rather than local storage.
You can find a complete guide here: Next.js Authentication Documentation.
It would be helpful if you edit your question to clarify how you are storing the token. Are you using a separate server API, or are you relying on Next.js server actions?