|
1 | 1 | package com.github.lookupgroup27.lookup.ui.authentication
|
2 | 2 |
|
| 3 | +import android.content.Intent |
| 4 | +import android.util.Log |
| 5 | +import android.widget.Toast |
| 6 | +import androidx.activity.compose.ManagedActivityResultLauncher |
| 7 | +import androidx.activity.compose.rememberLauncherForActivityResult |
| 8 | +import androidx.activity.result.ActivityResult |
| 9 | +import androidx.activity.result.contract.ActivityResultContracts |
| 10 | +import androidx.compose.foundation.BorderStroke |
| 11 | +import androidx.compose.foundation.Image |
| 12 | +import androidx.compose.foundation.layout.* |
| 13 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 14 | +import androidx.compose.material3.Button |
| 15 | +import androidx.compose.material3.ButtonDefaults |
| 16 | +import androidx.compose.material3.MaterialTheme |
| 17 | +import androidx.compose.material3.Scaffold |
| 18 | +import androidx.compose.material3.Text |
3 | 19 | import androidx.compose.runtime.Composable
|
| 20 | +import androidx.compose.runtime.rememberCoroutineScope |
| 21 | +import androidx.compose.ui.Alignment |
| 22 | +import androidx.compose.ui.Modifier |
| 23 | +import androidx.compose.ui.graphics.Color |
| 24 | +import androidx.compose.ui.platform.LocalContext |
| 25 | +import androidx.compose.ui.platform.testTag |
| 26 | +import androidx.compose.ui.res.painterResource |
| 27 | +import androidx.compose.ui.res.stringResource |
| 28 | +import androidx.compose.ui.text.font.FontWeight |
| 29 | +import androidx.compose.ui.text.style.TextAlign |
| 30 | +import androidx.compose.ui.unit.dp |
| 31 | +import androidx.compose.ui.unit.sp |
| 32 | +import com.github.lookupgroup27.lookup.R |
| 33 | +import com.github.lookupgroup27.lookup.ui.navigation.NavigationActions |
| 34 | +import com.google.android.gms.auth.api.signin.GoogleSignIn |
| 35 | +import com.google.android.gms.auth.api.signin.GoogleSignInOptions |
| 36 | +import com.google.android.gms.common.api.ApiException |
| 37 | +import com.google.firebase.Firebase |
| 38 | +import com.google.firebase.auth.AuthResult |
| 39 | +import com.google.firebase.auth.GoogleAuthProvider |
| 40 | +import com.google.firebase.auth.auth |
| 41 | +import kotlinx.coroutines.launch |
| 42 | +import kotlinx.coroutines.tasks.await |
4 | 43 |
|
5 |
| -@Composable fun SignIn() {} |
| 44 | +@Composable |
| 45 | +fun SignInScreen(navigationActions: NavigationActions) { |
| 46 | + val context = LocalContext.current |
| 47 | + |
| 48 | + val launcher = |
| 49 | + rememberFirebaseAuthLauncher( |
| 50 | + onAuthComplete = { result -> |
| 51 | + Log.d("SignInScreen", "User signed in: ${result.user?.displayName}") |
| 52 | + Toast.makeText(context, "Login successful!", Toast.LENGTH_LONG).show() |
| 53 | + // TODO: navigate to the next screen |
| 54 | + }, |
| 55 | + onAuthError = { |
| 56 | + Log.e("SignInScreen", "Failed to sign in: ${it.statusCode}") |
| 57 | + Toast.makeText(context, "Login Failed!", Toast.LENGTH_LONG).show() |
| 58 | + }) |
| 59 | + val token = stringResource(R.string.default_web_client_id) |
| 60 | + |
| 61 | + Scaffold( |
| 62 | + modifier = Modifier.fillMaxSize(), |
| 63 | + containerColor = Color.White, |
| 64 | + content = { padding -> |
| 65 | + Column( |
| 66 | + modifier = Modifier.fillMaxSize().padding(padding), |
| 67 | + horizontalAlignment = Alignment.CenterHorizontally, |
| 68 | + verticalArrangement = Arrangement.Center, |
| 69 | + ) { |
| 70 | + // App Logo Image |
| 71 | + Image( |
| 72 | + painter = painterResource(id = R.drawable.app_logo), |
| 73 | + contentDescription = "App Logo", |
| 74 | + modifier = Modifier.size(250.dp)) |
| 75 | + |
| 76 | + Spacer(modifier = Modifier.height(16.dp)) |
| 77 | + |
| 78 | + // Welcome Text |
| 79 | + Text( |
| 80 | + modifier = Modifier.testTag("loginTitle"), |
| 81 | + text = "Welcome", |
| 82 | + style = |
| 83 | + MaterialTheme.typography.headlineLarge.copy(fontSize = 57.sp, lineHeight = 64.sp), |
| 84 | + fontWeight = FontWeight.Bold, |
| 85 | + // center the text |
| 86 | + |
| 87 | + textAlign = TextAlign.Center, |
| 88 | + color = Color.White) |
| 89 | + |
| 90 | + Spacer(modifier = Modifier.height(48.dp)) |
| 91 | + |
| 92 | + // Authenticate With Google Button |
| 93 | + GoogleSignInButton( |
| 94 | + onSignInClick = { |
| 95 | + val gso = |
| 96 | + GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) |
| 97 | + .requestIdToken(token) |
| 98 | + .requestEmail() |
| 99 | + .build() |
| 100 | + val googleSignInClient = GoogleSignIn.getClient(context, gso) |
| 101 | + launcher.launch(googleSignInClient.signInIntent) |
| 102 | + }) |
| 103 | + } |
| 104 | + }) |
| 105 | +} |
| 106 | + |
| 107 | +@Composable |
| 108 | +fun GoogleSignInButton(onSignInClick: () -> Unit) { |
| 109 | + Button( |
| 110 | + onClick = onSignInClick, |
| 111 | + colors = ButtonDefaults.buttonColors(containerColor = Color.White), // Button color |
| 112 | + shape = RoundedCornerShape(50), // Circular edges for the button |
| 113 | + border = BorderStroke(1.dp, Color.LightGray), |
| 114 | + modifier = |
| 115 | + Modifier.padding(8.dp) |
| 116 | + .height(48.dp) // Adjust height as needed |
| 117 | + .testTag("loginButton")) { |
| 118 | + Row( |
| 119 | + verticalAlignment = Alignment.CenterVertically, |
| 120 | + horizontalArrangement = Arrangement.Center, |
| 121 | + modifier = Modifier.fillMaxWidth()) { |
| 122 | + // Load the Google logo from resources |
| 123 | + Image( |
| 124 | + painter = painterResource(id = R.drawable.google_logo), |
| 125 | + contentDescription = "Google Logo", |
| 126 | + modifier = |
| 127 | + Modifier.size(30.dp) // Size of the Google logo |
| 128 | + .padding(end = 8.dp)) |
| 129 | + |
| 130 | + // Text for the button |
| 131 | + Text( |
| 132 | + text = "Sign in with Google", |
| 133 | + color = Color.Gray, // Text color |
| 134 | + fontSize = 16.sp, // Font size |
| 135 | + fontWeight = FontWeight.Medium) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +@Composable |
| 141 | +fun rememberFirebaseAuthLauncher( |
| 142 | + onAuthComplete: (AuthResult) -> Unit, |
| 143 | + onAuthError: (ApiException) -> Unit |
| 144 | +): ManagedActivityResultLauncher<Intent, ActivityResult> { |
| 145 | + val scope = rememberCoroutineScope() |
| 146 | + return rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) { |
| 147 | + result -> |
| 148 | + val task = GoogleSignIn.getSignedInAccountFromIntent(result.data) |
| 149 | + try { |
| 150 | + val account = task.getResult(ApiException::class.java)!! |
| 151 | + val credential = GoogleAuthProvider.getCredential(account.idToken!!, null) |
| 152 | + scope.launch { |
| 153 | + val authResult = Firebase.auth.signInWithCredential(credential).await() |
| 154 | + onAuthComplete(authResult) |
| 155 | + } |
| 156 | + } catch (e: ApiException) { |
| 157 | + onAuthError(e) |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments