-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthScreen.kt
More file actions
67 lines (63 loc) · 2.48 KB
/
Copy pathAuthScreen.kt
File metadata and controls
67 lines (63 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package studio.moondev.samples.demo.auth
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.Button
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import io.moondev.auth.AuthProvider
import kotlinx.coroutines.launch
import org.koin.compose.koinInject
/**
* Demonstrates sign-in / sign-out via the L1 [AuthProvider] contract
* (implemented by FirebaseAuthProvider from moon-firebase-auth-kmp).
*
* Usage shape:
* 1. Inject [AuthProvider] via Koin.
* 2. Observe `currentUser: StateFlow<AuthUser?>`.
* 3. Call `signInWithEmailPassword(email, password)` on a background scope.
*/
@Composable
fun AuthScreen() {
val auth = koinInject<AuthProvider>()
val scope = rememberCoroutineScope()
val user by auth.currentUser.collectAsState()
var email by remember { mutableStateOf("") }
var password by remember { mutableStateOf("") }
var status by remember { mutableStateOf("") }
Column(
modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.spacedBy(8.dp)
) {
Text(text = "Signed in as: ${user?.uid ?: "(none)"}")
OutlinedTextField(value = email, onValueChange = { email = it }, label = { Text("Email") })
OutlinedTextField(value = password, onValueChange = { password = it }, label = { Text("Password") })
Button(onClick = {
scope.launch {
status = runCatching {
// TODO(cc): verify signature with library v1.0.0 API reference.
auth.signInWithEmailPassword(email, password)
"Signed in"
}.getOrElse { "Error: ${it.message}" }
}
}) { Text("Sign in") }
Button(onClick = {
scope.launch {
status = runCatching {
auth.signOut()
"Signed out"
}.getOrElse { "Error: ${it.message}" }
}
}) { Text("Sign out") }
Text(text = status)
}
}