start login

authentification
samuel 1 year ago
parent 51f7c046ce
commit a3fbe984b6

@ -7,21 +7,23 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.iqball.app.api.EitherBodyConverter import com.iqball.app.api.EitherBodyConverter
import com.iqball.app.api.EitherCallAdapterFactory import com.iqball.app.api.EitherCallAdapterFactory
import com.iqball.app.api.service.IQBallService import com.iqball.app.api.service.IQBallService
import com.iqball.app.page.RegisterPage import com.iqball.app.page.HomePage
import com.iqball.app.page.LoginPage
import com.iqball.app.session.DataSession
import com.iqball.app.session.Session
import com.iqball.app.ui.theme.IQBallTheme import com.iqball.app.ui.theme.IQBallTheme
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.ResponseBody
import retrofit2.Converter
import retrofit2.Retrofit import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.create import retrofit2.create
import java.lang.reflect.Type
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -46,8 +48,12 @@ class MainActivity : ComponentActivity() {
setContent { setContent {
IQBallTheme { IQBallTheme {
// A surface container using the 'background' color from the theme // A surface container using the 'background' color from the theme
Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colorScheme.background) { Surface(
App(service) modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
val sessionState = remember { mutableStateOf<Session>(DataSession()) }
App(service, sessionState)
} }
} }
} }
@ -55,6 +61,16 @@ class MainActivity : ComponentActivity() {
} }
@Composable @Composable
fun App(service: IQBallService) { fun App(service: IQBallService, sessionState: MutableState<Session>) {
RegisterPage(service)
val loginPage: @Composable () -> Unit = {
LoginPage(service = service, onLoginSuccess = { auth ->
sessionState.value = DataSession(auth)
})
}
val homePage : @Composable () -> Unit = { HomePage(service, sessionState.value) }
val currentPage = remember(sessionState.value.auth) { if (sessionState.value.auth == null) loginPage else homePage }
currentPage()
} }

@ -0,0 +1,10 @@
package com.iqball.app.page
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) {
}

@ -2,8 +2,70 @@ package com.iqball.app.page
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.foundation.layout.*
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Surface
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
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 @Composable
fun LoginPage() { fun LoginPage(service: AuthService, onLoginSuccess : (Authentication) -> Unit) {
Text(text = "Login Page") var username by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
Surface(
color = Color.White,
modifier = Modifier.fillMaxSize()
) {
Column(
modifier = Modifier
.padding(16.dp)
.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
text = "Login",
fontSize = 28.sp,
color = Color.Black
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
value = username,
onValueChange = { username = it },
label = { Text("Username") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(16.dp))
OutlinedTextField(
value = password,
onValueChange = { password = it },
label = { Text("Password") },
modifier = Modifier.fillMaxWidth()
)
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
runBlocking {
when(val response = service.login(AuthService.LoginRequest(username, password))){
is Either.Left -> println(response.value)
is Either.Right -> onLoginSuccess(Authentication(response.value.token, LocalDateTime.parse(response.value.expirationDate)))
}
}
}) {
Text(text = "Se connecter")
}
}
}
} }

@ -3,21 +3,14 @@ package com.iqball.app.page
import android.util.Log import android.util.Log
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
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
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.AuthService.RegisterRequest
import com.iqball.app.api.service.IQBallService 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 @Composable
fun RegisterPage(service: IQBallService) { fun RegisterPage(service: IQBallService) {
@ -25,7 +18,7 @@ fun RegisterPage(service: IQBallService) {
var text by remember { mutableStateOf("No message !") } var text by remember { mutableStateOf("No message !") }
runBlocking { LaunchedEffect(Unit) {
val result = service.login(AuthService.LoginRequest("maxime@mail.com", "123456")) val result = service.login(AuthService.LoginRequest("maxime@mail.com", "123456"))
when (result) { when (result) {

@ -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 package com.iqball.app.session
interface Session { interface Session {
val auth: Authentication val auth: Authentication?
} }

@ -1,5 +1,5 @@
[versions] [versions]
agp = "8.3.0" agp = "8.2.2"
arrowCore = "1.2.1" arrowCore = "1.2.1"
converterGson = "2.9.0" converterGson = "2.9.0"
kotlin = "1.9.0" kotlin = "1.9.0"

Loading…
Cancel
Save