We are developing an application with Laravel/PHP and we want to use an pay-per-user pricing model. For that we have to ensure that an account can only used by only one concurrent user. We use JWT for authentication and it is stateless so I can't use sessions.
To ensure one concurrent login I can enclose the Browser-agent or IP, but both aren't unique and it is possible that they occur multiple times in e.g. an office. Also I can send the MAC address, but that is not the easiest way.
Are there other solutions to ensure one concurrent login per user with JWT?
I think that the simple answer to this is NO, you cannot do that by JWT and keep the server stateless. However, if you use the setup with an Access Token and a Refresh Token, you can probably achieve something like this:
This will result in a login flow where the latest user to login can use your service. This is handy if it actually is the same user that change device or restart browser session. Compare to for example Spotify's "chasing the stream" way of handling concurrent listening.
Only answer i can think of:
On login: "User" in your DB has a value = activeJwt
User logs in and JWT token is created, copy the JWT string to value activeJWT in your DB and send it to user. If you login on another device same deal, and the activeJWT value is changed
On all requests that require login match users JWT-string and activeJWT, if they dont match it means another device logged in after making the old token useless.
I now testing without JWT and with OAuth2 authentication with password grant tokens. Within 1 client the user can only use the app with one login, if he logins on another session/device, the other login (token) will not be valid anymore. When i want to allow one user to login multiple times (e.g. web app and mobile app) i can use multiple clients.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With