Skip to main content

Overview

The AuthRepository interface handles all authentication-related operations including user login, logout, session validation, and token management. It provides methods for both local and server-side session management.

Methods

login()

Authenticate a user with username and password. Stores the JWT token on successful login. Parameters
username
String
required
The user’s username or email address
password
String
required
The user’s password
Returns
Result<UserSession>
Result
Result containing UserSession with JWT token and user details, or error on failure

logout()

Log out the current user by calling the server logout endpoint and clearing stored tokens. Returns
Result<Unit>
Result
Result indicating success or failure of logout operation

clearLocalSession()

Clear local tokens without calling the server. Used for immediate local logout when server communication is not required or has failed. Returns Void - no return value

isAuthenticated()

Check if a user is currently authenticated by verifying the presence of a valid stored token. Returns
Boolean
Boolean
true if user has a valid token stored, false otherwise

getCurrentSession()

Retrieve the current user session if authenticated. Returns
UserSession?
UserSession
UserSession object if authenticated, null if not authenticated

validateSession()

Validates the current session with the backend by making a lightweight API call to verify the token is still valid. Returns
Boolean
Boolean
true if session is valid, false if expired or invalid

Usage Example

class LoginViewModel(
    private val authRepository: AuthRepository
) : ViewModel() {
    private val _state = MutableStateFlow(LoginUiState())
    val state: StateFlow<LoginUiState> = _state.asStateFlow()

    fun onLogin(username: String, password: String) {
        viewModelScope.launch {
            _state.update { it.copy(isLoading = true, error = null) }
            
            authRepository.login(username.trim(), password)
                .onSuccess { session ->
                    _state.update { it.copy(
                        isLoading = false,
                        isAuthenticated = true
                    ) }
                }
                .onFailure { error ->
                    _state.update { it.copy(
                        isLoading = false,
                        error = error.message ?: "Login failed"
                    ) }
                }
        }
    }
    
    fun validateSession() {
        viewModelScope.launch {
            val isValid = authRepository.validateSession()
            _state.update { it.copy(isAuthenticated = isValid) }
        }
    }
    
    fun logout() {
        viewModelScope.launch {
            authRepository.logout()
                .onSuccess {
                    _state.update { it.copy(isAuthenticated = false) }
                }
        }
    }
}

Authentication Flow

Complete authentication implementation guide

UsersRepository

User management operations

User Models

User and UserSession data models

LoginViewModel

Login screen state management

Build docs developers (and LLMs) love