feat: registration api support
This commit is contained in:
parent
32dce50a0a
commit
328c51bc82
|
@ -28,4 +28,17 @@ data class SessionResponse(
|
|||
val expires_at: String, // ISO 8601
|
||||
val created_at: String, // ISO 8601
|
||||
val revoked: Boolean,
|
||||
)
|
||||
|
||||
data class RegisterRequest(
|
||||
val username: String,
|
||||
val password: String,
|
||||
val email: String,
|
||||
)
|
||||
|
||||
data class RegisterResponse(
|
||||
val user_id: String,
|
||||
val username: String,
|
||||
val email: String,
|
||||
val created_at: String, // ISO 8601
|
||||
)
|
|
@ -19,4 +19,7 @@ interface AuthService {
|
|||
|
||||
@GET("session")
|
||||
suspend fun getSession(@Header("Authorization") token: String): SessionResponse
|
||||
|
||||
@POST("users")
|
||||
suspend fun register(@Body registerRequest: RegisterRequest): RegisterResponse
|
||||
}
|
|
@ -3,6 +3,8 @@ package com.p_vacho.neat_calendar.auth
|
|||
import android.util.Log
|
||||
import com.p_vacho.neat_calendar.api.RetrofitClient
|
||||
import com.p_vacho.neat_calendar.api.models.LoginRequest
|
||||
import com.p_vacho.neat_calendar.api.models.RegisterRequest
|
||||
import com.p_vacho.neat_calendar.api.models.RegisterResponse
|
||||
import com.p_vacho.neat_calendar.api.models.SessionResponse
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
@ -25,6 +27,20 @@ class AuthRepository(private val tokenManager: TokenManager) {
|
|||
}
|
||||
}
|
||||
|
||||
// Register user (doesn't perform a login)
|
||||
suspend fun register(username: String, password: String, email: String): RegisterResponse? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
try {
|
||||
val response = RetrofitClient.authService.register(RegisterRequest(username, password, email))
|
||||
Log.i("API", "Register request succesful")
|
||||
response
|
||||
} catch (e: Exception) {
|
||||
Log.w("API", "Register request failed", e)
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Validate the access token by fetching session info
|
||||
suspend fun validateAccessToken(): SessionResponse? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
|
|
Loading…
Reference in a new issue