Merge pull request 'authentification and navigation' (#31) from authentification into master
Reviewed-on: #31visualizer
commit
509909e13d
@ -0,0 +1,12 @@
|
||||
package com.iqball.app.page
|
||||
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import com.iqball.app.api.service.IQBallService
|
||||
import com.iqball.app.session.Session
|
||||
|
||||
@Composable
|
||||
fun HomePage(service: IQBallService, session: Session) {
|
||||
|
||||
Text(text = "HELLO WELCOME")
|
||||
}
|
@ -1,54 +1,130 @@
|
||||
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
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.height
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.OutlinedTextFieldDefaults
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
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.AuthService.RegisterRequest
|
||||
import com.iqball.app.api.service.IQBallService
|
||||
import com.iqball.app.session.Authentication
|
||||
import com.iqball.app.session.MutableSession
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
|
||||
@Composable
|
||||
fun RegisterPage(service: IQBallService) {
|
||||
fun RegisterPage(
|
||||
service: AuthService,
|
||||
onRegisterSuccess: (Authentication) -> Unit,
|
||||
onNavigateToLogin: () -> Unit
|
||||
) {
|
||||
var username by remember { mutableStateOf("") }
|
||||
var email by remember { mutableStateOf("") }
|
||||
var password by remember { mutableStateOf("") }
|
||||
var errors by remember { mutableStateOf("") }
|
||||
|
||||
Surface(
|
||||
color = Color.White,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
|
||||
var text by remember { mutableStateOf("No message !") }
|
||||
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.padding(16.dp)
|
||||
.fillMaxSize(),
|
||||
verticalArrangement = Arrangement.Center,
|
||||
horizontalAlignment = Alignment.CenterHorizontally
|
||||
) {
|
||||
Text(
|
||||
text = "S'enregistrer",
|
||||
fontSize = 28.sp,
|
||||
color = Color.Black
|
||||
)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Text(
|
||||
text = errors,
|
||||
color = Color.Red,
|
||||
fontSize = 14.sp,
|
||||
modifier = Modifier.padding(vertical = 8.dp)
|
||||
)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
OutlinedTextField(
|
||||
value = username,
|
||||
onValueChange = { username = it },
|
||||
label = { Text("Nom d'utilisateur") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = OutlinedTextFieldDefaults.colors(
|
||||
focusedTextColor = Color.Black,
|
||||
unfocusedTextColor = Color.Black
|
||||
)
|
||||
)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
OutlinedTextField(
|
||||
value = password,
|
||||
onValueChange = { password = it },
|
||||
label = { Text("Mot de passe") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = OutlinedTextFieldDefaults.colors(
|
||||
focusedTextColor = Color.Black,
|
||||
unfocusedTextColor = Color.Black
|
||||
),
|
||||
visualTransformation = PasswordVisualTransformation()
|
||||
)
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
OutlinedTextField(
|
||||
value = email,
|
||||
onValueChange = { email = it },
|
||||
label = { Text("Email") },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
colors = OutlinedTextFieldDefaults.colors(
|
||||
focusedTextColor = Color.Black,
|
||||
unfocusedTextColor = Color.Black
|
||||
)
|
||||
)
|
||||
Button(onClick = {
|
||||
runBlocking {
|
||||
val result = service.login(AuthService.LoginRequest("maxime@mail.com", "123456"))
|
||||
|
||||
when (result) {
|
||||
when (val response =
|
||||
service.register(AuthService.RegisterRequest(username, email, password))) {
|
||||
is Either.Left -> {
|
||||
println("Error : " + result.value)
|
||||
text = result.toString()
|
||||
errors = response.value.toList()
|
||||
.flatMap { entry -> entry.second.map { "${entry.first} : ${it}" } }
|
||||
.joinToString("\n")
|
||||
}
|
||||
is Either.Right -> {
|
||||
val token = result.value.token
|
||||
val userDataResponse = service.getUserData(token)
|
||||
|
||||
when (userDataResponse) {
|
||||
is Either.Left -> println("Error User Data : " + userDataResponse.value)
|
||||
is Either.Right -> println("Success User Data : " + userDataResponse.value)
|
||||
is Either.Right -> {
|
||||
onRegisterSuccess(
|
||||
Authentication(
|
||||
response.value.token,
|
||||
response.value.expirationDate
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
text = userDataResponse.toString()
|
||||
}
|
||||
}
|
||||
|
||||
println(result)
|
||||
Log.i("%", result.toString())
|
||||
}) {
|
||||
Text(text = "Créer votre compte")
|
||||
}
|
||||
Spacer(modifier = Modifier.height(16.dp))
|
||||
Button(onClick = { onNavigateToLogin() }) {
|
||||
Text(text = "Vous avez déjà un compte ?")
|
||||
}
|
||||
}
|
||||
|
||||
Text(text = text)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -0,0 +1,3 @@
|
||||
package com.iqball.app.session
|
||||
|
||||
data class DataSession(override val auth: Authentication? = null) : Session
|
@ -1,5 +0,0 @@
|
||||
package com.iqball.app.session
|
||||
|
||||
interface MutableSession : Session {
|
||||
override var auth: Authentication
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package com.iqball.app.session
|
||||
|
||||
interface Session {
|
||||
val auth: Authentication
|
||||
val auth: Authentication?
|
||||
}
|
Loading…
Reference in new issue