I'm working on a project where I have a Laravel application serving as the backend and a Next.js application as the frontend. These two components are synchronized using Sanctum.
I plan to switch from Sanctum to Passport. thereby my goal is to remove all dependencies on sessions from the project.
Currently, I've implemented OTP (One-Time Password) login functionality, which relies on the session. When an OTP code is generated, it's stored in the session just before being sent to the user via SMS or email. The purpose of this approach is to ensure that the same user who requested the OTP code is the one attempting to log in.
However, I want to transition to a stateless architecture. How can I achieve OTP authentication without relying on sessions?
Here are my considerations:
IP and User Agent Verification:
One suggestion I came across is to store information like the user's IP address and user agent during the initial request. When validating the OTP code, these details can be checked.
To my understanding, the Backend (Laravel) doesn't have direct access to the client's IP address. However, the frontend(Next.js) can access this information.
I considered forwarding the client's IP address via headers to the Laravel backend, but I discovered that this data can be faked.
My Questions:
- What is the best approach to achieve stateless OTP authentication in this scenario?
- How can I securely validate OTP codes without relying on sessions?
- Is there a reliable way to obtain the client's IP address in Laravel when using Passport?
Any insights or guidance would be greatly appreciated! 🙌