register and login wip

authentification
KaulH 1 year ago
parent 9990c69374
commit 07252b9025

@ -65,7 +65,7 @@ class MainActivity : ComponentActivity() {
fun App(service: IQBallService, sessionState: MutableState<Session>) { fun App(service: IQBallService, sessionState: MutableState<Session>) {
val registerPage: @Composable () -> Unit = { val registerPage: @Composable () -> Unit = {
RegisterPage(service = service, onLoginSuccess = { auth -> RegisterPage(service = service, onRegisterSuccess = { auth ->
sessionState.value = DataSession(auth) sessionState.value = DataSession(auth)
}) })
} }
@ -77,7 +77,7 @@ fun App(service: IQBallService, sessionState: MutableState<Session>) {
} }
val homePage : @Composable () -> Unit = { HomePage(service, sessionState.value) } val homePage : @Composable () -> Unit = { HomePage(service, sessionState.value) }
registerPage() //registerPage()
val currentPage = remember(sessionState.value.auth) { if (sessionState.value.auth == null) loginPage else homePage } val currentPage = remember(sessionState.value.auth) { if (sessionState.value.auth == null) loginPage else homePage }
currentPage() currentPage()

@ -11,7 +11,7 @@ import retrofit2.http.POST
interface AuthService { interface AuthService {
@Serializable @Serializable
data class AuthResponse(val token: String, val expirationDate: String) data class AuthResponse(val token: String, val expirationDate: Long)
@Serializable @Serializable
data class RegisterRequest(val username: String, val email: String, val password: String) data class RegisterRequest(val username: String, val email: String, val password: String)

@ -10,13 +10,13 @@ import androidx.compose.runtime.*
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import arrow.core.Either import arrow.core.Either
import com.iqball.app.api.service.AuthService import com.iqball.app.api.service.AuthService
import com.iqball.app.session.Authentication import com.iqball.app.session.Authentication
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.datetime.LocalDateTime
@Composable @Composable
@ -42,6 +42,15 @@ fun LoginPage(service: AuthService, onLoginSuccess : (Authentication) -> Unit) {
color = Color.Black color = Color.Black
) )
Spacer(modifier = Modifier.height(16.dp)) 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( OutlinedTextField(
value = email, value = email,
onValueChange = { email = it }, onValueChange = { email = it },
@ -53,6 +62,7 @@ fun LoginPage(service: AuthService, onLoginSuccess : (Authentication) -> Unit) {
value = password, value = password,
onValueChange = { password = it }, onValueChange = { password = it },
label = { Text("Password") }, label = { Text("Password") },
visualTransformation = PasswordVisualTransformation(),
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth()
) )
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
@ -61,10 +71,17 @@ fun LoginPage(service: AuthService, onLoginSuccess : (Authentication) -> Unit) {
runBlocking { runBlocking {
when (val response = service.login(AuthService.LoginRequest(email, password))) { when (val response = service.login(AuthService.LoginRequest(email, password))) {
is Either.Left -> { 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()
)
)
} }
} }
}) { }) {

@ -1,6 +1,5 @@
package com.iqball.app.page package com.iqball.app.page
import android.util.Log
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.Spacer
@ -13,7 +12,6 @@ import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
@ -21,18 +19,16 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import arrow.core.Either import arrow.core.Either
import com.iqball.app.api.service.AuthService import com.iqball.app.api.service.AuthService
import com.iqball.app.api.service.IQBallService
import com.iqball.app.session.Authentication import com.iqball.app.session.Authentication
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.datetime.LocalDateTime
import androidx.compose.ui.text.input.VisualTransformation
@Composable @Composable
fun RegisterPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit) { fun RegisterPage(service: AuthService, onRegisterSuccess: (Authentication) -> Unit) {
var username by remember { mutableStateOf("") } var username by remember { mutableStateOf("") }
var email by remember { mutableStateOf("") } var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") } var password by remember { mutableStateOf("") }
@ -76,7 +72,8 @@ fun RegisterPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit)
value = password, value = password,
onValueChange = { password = it }, onValueChange = { password = it },
label = { Text("Mot de passe") }, label = { Text("Mot de passe") },
modifier = Modifier.fillMaxWidth() modifier = Modifier.fillMaxWidth(),
visualTransformation = PasswordVisualTransformation()
) )
Spacer(modifier = Modifier.height(16.dp)) Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField( OutlinedTextField(
@ -90,11 +87,18 @@ fun RegisterPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit)
when (val response = when (val response =
service.register(AuthService.RegisterRequest(username, email, password))) { service.register(AuthService.RegisterRequest(username, email, password))) {
is Either.Left -> { 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 -> { is Either.Right -> {
onLoginSuccess(Authentication(response.value.token,LocalDateTime.parse(response.value.expirationDate))) onRegisterSuccess(
Authentication(
response.value.token,
response.value.expirationDate
)
)
} }
} }
} }

@ -1,5 +1,3 @@
package com.iqball.app.session package com.iqball.app.session
import kotlinx.datetime.LocalDateTime data class Authentication(val token: String, val expirationDate: Long)
data class Authentication(val token: String, val expirationDate: LocalDateTime)

Loading…
Cancel
Save