From 07252b902589a6f028cf855df8c7ee383abf8cfa Mon Sep 17 00:00:00 2001 From: KaulH Date: Wed, 27 Mar 2024 17:32:42 +0100 Subject: [PATCH] register and login wip --- .../main/java/com/iqball/app/MainActivity.kt | 4 +-- .../com/iqball/app/api/service/AuthService.kt | 2 +- .../java/com/iqball/app/page/LoginPage.kt | 27 +++++++++++++++---- .../java/com/iqball/app/page/RegisterPage.kt | 22 ++++++++------- .../com/iqball/app/session/Authentication.kt | 4 +-- 5 files changed, 39 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/iqball/app/MainActivity.kt b/app/src/main/java/com/iqball/app/MainActivity.kt index bc2fc63..9accff0 100644 --- a/app/src/main/java/com/iqball/app/MainActivity.kt +++ b/app/src/main/java/com/iqball/app/MainActivity.kt @@ -65,7 +65,7 @@ class MainActivity : ComponentActivity() { fun App(service: IQBallService, sessionState: MutableState) { val registerPage: @Composable () -> Unit = { - RegisterPage(service = service, onLoginSuccess = { auth -> + RegisterPage(service = service, onRegisterSuccess = { auth -> sessionState.value = DataSession(auth) }) } @@ -77,7 +77,7 @@ fun App(service: IQBallService, sessionState: MutableState) { } val homePage : @Composable () -> Unit = { HomePage(service, sessionState.value) } - registerPage() + //registerPage() val currentPage = remember(sessionState.value.auth) { if (sessionState.value.auth == null) loginPage else homePage } currentPage() diff --git a/app/src/main/java/com/iqball/app/api/service/AuthService.kt b/app/src/main/java/com/iqball/app/api/service/AuthService.kt index 5b88a09..bb444fe 100644 --- a/app/src/main/java/com/iqball/app/api/service/AuthService.kt +++ b/app/src/main/java/com/iqball/app/api/service/AuthService.kt @@ -11,7 +11,7 @@ import retrofit2.http.POST interface AuthService { @Serializable - data class AuthResponse(val token: String, val expirationDate: String) + data class AuthResponse(val token: String, val expirationDate: Long) @Serializable data class RegisterRequest(val username: String, val email: String, val password: String) diff --git a/app/src/main/java/com/iqball/app/page/LoginPage.kt b/app/src/main/java/com/iqball/app/page/LoginPage.kt index 053e14d..d2d50d6 100644 --- a/app/src/main/java/com/iqball/app/page/LoginPage.kt +++ b/app/src/main/java/com/iqball/app/page/LoginPage.kt @@ -10,17 +10,17 @@ import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import arrow.core.Either import com.iqball.app.api.service.AuthService import com.iqball.app.session.Authentication import kotlinx.coroutines.runBlocking -import kotlinx.datetime.LocalDateTime @Composable -fun LoginPage(service: AuthService, onLoginSuccess : (Authentication) -> Unit) { +fun LoginPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit) { var email by remember { mutableStateOf("") } var password by remember { mutableStateOf("") } var errors by remember { mutableStateOf("") } @@ -42,6 +42,15 @@ fun LoginPage(service: AuthService, onLoginSuccess : (Authentication) -> Unit) { color = Color.Black ) Spacer(modifier = Modifier.height(16.dp)) + errors?.let { message -> + Text( + text = message, + color = Color.Red, + fontSize = 14.sp, + modifier = Modifier.padding(vertical = 8.dp) + ) + } + Spacer(modifier = Modifier.height(16.dp)) OutlinedTextField( value = email, onValueChange = { email = it }, @@ -53,18 +62,26 @@ fun LoginPage(service: AuthService, onLoginSuccess : (Authentication) -> Unit) { value = password, onValueChange = { password = it }, label = { Text("Password") }, + visualTransformation = PasswordVisualTransformation(), modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(16.dp)) Button(onClick = { runBlocking { - when(val response = service.login(AuthService.LoginRequest(email, password))){ + when (val response = service.login(AuthService.LoginRequest(email, password))) { is Either.Left -> { - errors = response.value.toList().flatMap { entry -> entry.second.map { "${entry.first} : ${it}" } }.joinToString("\n") + errors = response.value.toList() + .flatMap { entry -> entry.second.map { "${entry.first} : ${it}" } } + .joinToString("\n") } - is Either.Right -> onLoginSuccess(Authentication(response.value.token, LocalDateTime.parse(response.value.expirationDate))) + is Either.Right -> onLoginSuccess( + Authentication( + response.value.token, + response.value.expirationDate.toLong() + ) + ) } } }) { diff --git a/app/src/main/java/com/iqball/app/page/RegisterPage.kt b/app/src/main/java/com/iqball/app/page/RegisterPage.kt index 869a777..2e02b03 100644 --- a/app/src/main/java/com/iqball/app/page/RegisterPage.kt +++ b/app/src/main/java/com/iqball/app/page/RegisterPage.kt @@ -1,6 +1,5 @@ package com.iqball.app.page -import android.util.Log import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Spacer @@ -13,7 +12,6 @@ import androidx.compose.material3.OutlinedTextField import androidx.compose.material3.Surface import androidx.compose.material3.Text import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember @@ -21,18 +19,16 @@ import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.input.PasswordVisualTransformation import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import arrow.core.Either import com.iqball.app.api.service.AuthService -import com.iqball.app.api.service.IQBallService import com.iqball.app.session.Authentication import kotlinx.coroutines.runBlocking -import kotlinx.datetime.LocalDateTime -import androidx.compose.ui.text.input.VisualTransformation @Composable -fun RegisterPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit) { +fun RegisterPage(service: AuthService, onRegisterSuccess: (Authentication) -> Unit) { var username by remember { mutableStateOf("") } var email by remember { mutableStateOf("") } var password by remember { mutableStateOf("") } @@ -76,7 +72,8 @@ fun RegisterPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit) value = password, onValueChange = { password = it }, label = { Text("Mot de passe") }, - modifier = Modifier.fillMaxWidth() + modifier = Modifier.fillMaxWidth(), + visualTransformation = PasswordVisualTransformation() ) Spacer(modifier = Modifier.height(16.dp)) OutlinedTextField( @@ -90,11 +87,18 @@ fun RegisterPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit) when (val response = service.register(AuthService.RegisterRequest(username, email, password))) { is Either.Left -> { - errors = response.value.toList().flatMap { entry -> entry.second.map { "${entry.first} : ${it}" } }.joinToString("\n") + errors = response.value.toList() + .flatMap { entry -> entry.second.map { "${entry.first} : ${it}" } } + .joinToString("\n") } is Either.Right -> { - onLoginSuccess(Authentication(response.value.token,LocalDateTime.parse(response.value.expirationDate))) + onRegisterSuccess( + Authentication( + response.value.token, + response.value.expirationDate + ) + ) } } } diff --git a/app/src/main/java/com/iqball/app/session/Authentication.kt b/app/src/main/java/com/iqball/app/session/Authentication.kt index 1f9aed4..b5dbeaf 100644 --- a/app/src/main/java/com/iqball/app/session/Authentication.kt +++ b/app/src/main/java/com/iqball/app/session/Authentication.kt @@ -1,5 +1,3 @@ package com.iqball.app.session -import kotlinx.datetime.LocalDateTime - -data class Authentication(val token: String, val expirationDate: LocalDateTime) +data class Authentication(val token: String, val expirationDate: Long)