Email, OTP and Google sign in without the usual holes
An authentication flow that resists account enumeration, keeps refresh tokens honest, and does not log users out because two requests raced.
Authentication is the feature everyone has built and most codebases get subtly wrong. The mistakes are rarely exotic. They are a response that reveals whether an email exists, a refresh flow that logs people out at random, or a logout that clears storage and leaves the user object in memory.
This is the flow I built for Nirbhor, and the reasoning behind each part.
One entry point, three outcomes
The user gives an email. The server responds with what happens next:
- The account exists with a password: ask for the password.
- The account does not exist: send an OTP and start registration.
- The user chose Google: verify the Google token instead.
The subtlety is that the response must not reveal which case it is in a way that lets someone enumerate accounts. On forgot password specifically, an unknown email still gets a success response and a decoy token. Timing stays comparable. Otherwise the endpoint is a free list of which of your users exist, and that list has value to someone.
OTP handling
- The OTP is hashed before it is stored. It is a credential for its lifetime, and a plaintext OTP in a database is a plaintext credential.
- It is short lived and single use.
- Verification returns a scoped, short lived setup token rather than logging the user in.
That last one matters. The OTP proves control of an inbox. It does not prove intent to complete registration or a password reset. A scoped token carries exactly what was proven and nothing else, so a token issued for a password reset cannot be replayed to create an account.
Google sign in
The client sends the Google ID token. The server verifies it with Google's library, checks the audience matches its own client id, and only then trusts the identity in it.
The audience check is the step people skip. Without it, a valid Google token issued for any other application is accepted by yours.
Accounts are matched on verified email, and a user who registered with a password and later uses Google lands in the same account rather than a duplicate.
Access and refresh tokens
Short lived access token, long lived refresh token, and the refresh token is only ever sent to the refresh endpoint.
The race everybody hits: the app opens, four requests fire, the access token has expired, all four get a 401 and all four try to refresh. With refresh token rotation, three of them are now presenting a token that was just invalidated, and the user is logged out on app open, intermittently.
Future<String>? _refreshing;
Future<String> validToken() {
final token = _store.access;
if (token != null && !token.expired) return Future.value(token.value);
// everyone awaits the same call
return _refreshing ??= _refresh().whenComplete(() => _refreshing = null);
}One in flight refresh, everyone else awaits it, failed requests replayed once the new token lands. Solved on the client, because the server cannot tell the difference between four honest requests and a replay.
Storage
Secure storage on the device. Keychain on iOS, EncryptedSharedPreferences on Android. Never shared preferences, which is a plain file.
Logout
Three things, and the third gets forgotten: clear secure storage, revoke the refresh token server side, and reset in memory state. A cached user object or a still open stream that survives logout means the next person to sign in sees the previous user's data for a second. Small bug, very bad impression.
Logout also deregisters the device push token, otherwise the previous user's notifications keep arriving on a phone that is now someone else's.
The review questions
- What does the API reveal about whether an account exists?
- Is the OTP hashed, single use and short lived?
- Does the Google verification check the audience?
- What happens when two requests refresh at the same time?
- Does logout clear memory as well as storage?
- Is a token ever written to a log?
The last one finds more real problems than the rest combined.