A local-logic-only Android application demonstrating passwordless authentication using Login, OTP, and Session screens. Built with Kotlin and Jetpack Compose.
The application follows the MVVM (Model-View-ViewModel) architecture:
- Data Layer:
OtpManagerhandles OTP generation, storage (in-memoryMap), and validation. It acts as the "Backend" simulation. - ViewModel Layer:
AuthViewModelmanages the UI state (AuthState) and business logic (timers, coordinating withOtpManager). - UI Layer: Jetpack Compose screens (
LoginScreen,OtpScreen,SessionScreen) observe the state and render accordingly. Navigation is handled by swapping composables based on the current state inMainActivity.
OtpData: Storesotp(String),expiryTime(Long), andattempts(Int).otpStore: AmutableMapOf<String, OtpData>inOtpManagercreates a transient in-memory database keyed by email.
Timber is used for logging as per requirements. It is initialized in PasswordlessAuthApplication.
Logged events include:
- OTP generated
- OTP validation success/failure/expiry
- Logout events
- OTP Expiry: OTPs are valid for 60 seconds.
OtpManagerchecksSystem.currentTimeMillis()againstexpiryTime. - Attempts: Max 3 attempts are allowed. Exceeding this blocks validation until a new OTP is requested.
- Session Timer:
SessionScreendisplays the live duration (mm:ss), updated every second via a Coroutine inAuthViewModel. - Rotation: UI state inputs (email, otp input) use
rememberSaveableto survive configuration changes. The session timer survives in theViewModel.
- Open the project in Android Studio.
- Sync Gradle with the project files.
- Run on an emulator or physical device.
- Check Logcat (filter by "Timber" or "PasswordlessAuth") to see the generated OTPs for testing.
- Login: Enter an email and click "Send OTP".
- Get OTP: Check Android Studio Logcat for "OTP generated for : <123456>".
- Enter OTP:
- Success: Enter the correct code. You go to the Session screen.
- Fail: Enter incorrect code. Error message shown.
- Wait: Wait >60s. Error message "OTP Expired".
- Retry: Enter wrong code 3 times. Error message "Too many attempts".
- Session: Observe the timer counting up. Click Logout to reset.