Repository navigation
Add Google sign-up and sign-in #126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
64ed51c
f7bd25d
571bcac
cdfaf5e
2e6d25b
70345f9
539833c
5a9d900
617b178
996fcf7
a645f2d
1f59b71
6ceef4e
f69c41e
0689e24
fe3103a
bf01222
bb1e5b5
b291bdf
122ab35
dc900f2
ed21b71
49f5e02
1597916
5dd429c
0679464
9dcfd30
1c59b90
72ae474
9150768
9d5dbc4
e075ff8
08ebb0b
d580f98
3ebccad
62db4eb
247fd88
39a605e
dda86eb
dc3f8a9
d520726
2e36a6a
70bfaba
4fa2f26
1e2ea23
b461368
76f0f2f
4059984
5e961d3
ef4433f
ddd821d
5e8c78b
0c4cea2
65ddd3b
eff9a62
24d68df
b58f6bc
fc6358c
0b67045
b715e63
1516091
a5642c6
2782ff1
04d334a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,4 +36,5 @@ app/google-services.json | |
| secrets.properties | ||
|
|
||
| # Mac | ||
| .DS_store | ||
| .DS_store | ||
| .kotlin/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,6 @@ plugins { | |
| id("com.google.gms.google-services") | ||
| } | ||
|
|
||
|
|
||
| android { | ||
| namespace = "com.cornellappdev.score" | ||
| compileSdk = 37 | ||
|
|
@@ -38,32 +37,20 @@ android { | |
| } | ||
| buildTypes { | ||
| debug { | ||
| buildConfigField( | ||
| "String", | ||
| "BASE_URL", | ||
| "\"${secrets.getProperty("API_URL_DEV")}\"" | ||
| ) | ||
| buildConfigField( | ||
| "String", | ||
| "SOCKET_URL", | ||
| "\"${secrets.getProperty("SOCKET_URL_DEV")}\"" | ||
| ) | ||
| buildConfigField("String", "BASE_URL", "\"${secrets.getProperty("API_URL_PROD")}\"") | ||
| buildConfigField("String", "SOCKET_URL", "\"${secrets.getProperty("SOCKET_URL_PROD")}\"") | ||
| } | ||
| release { | ||
| buildConfigField( | ||
| "String", | ||
| "BASE_URL", | ||
| "\"${secrets.getProperty("API_URL_PROD")}\"" | ||
| ) | ||
| buildConfigField("String", "BASE_URL", "\"${secrets.getProperty("API_URL_DEV")}\"") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirm the release endpoint mapping before merge. The release build type now reads If release builds must use the production backend, the release
|
||
| buildConfigField( | ||
| "String", | ||
| "SOCKET_URL", | ||
| "\"${secrets.getProperty("SOCKET_URL_PROD")}\"" | ||
| "\"${secrets.getProperty("SOCKET_URL_DEV")}\"", | ||
| ) | ||
| isMinifyEnabled = false | ||
| proguardFiles( | ||
| getDefaultProguardFile("proguard-android-optimize.txt"), | ||
| "proguard-rules.pro" | ||
| "proguard-rules.pro", | ||
| ) | ||
| } | ||
| } | ||
|
|
@@ -117,16 +104,25 @@ dependencies { | |
| lintChecks(libs.compose.lint.checks) | ||
| implementation(platform("com.google.firebase:firebase-bom:34.3.0")) | ||
| implementation("com.google.firebase:firebase-analytics") | ||
| implementation("com.google.firebase:firebase-auth") | ||
| implementation("androidx.credentials:credentials:1.5.0") | ||
| implementation("androidx.credentials:credentials-play-services-auth:1.5.0") | ||
| implementation("com.google.android.libraries.identity.googleid:googleid:1.1.1") | ||
| implementation("org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.10.1") | ||
| implementation("androidx.lifecycle:lifecycle-runtime-compose:2.8.7") | ||
| } | ||
|
|
||
| apollo { | ||
| service("service") { | ||
| packageName.set("com.example.score") | ||
| secrets.getProperty("API_URL_DEV")?.takeIf { it.isNotBlank() }?.let { apiUrl -> | ||
| introspection { | ||
| endpointUrl.set(apiUrl) | ||
| schemaFile.set(file("src/main/graphql/schema.graphqls")) | ||
| secrets | ||
| .getProperty("API_URL_PROD") | ||
| ?.takeIf { it.isNotBlank() } | ||
| ?.let { apiUrl -> | ||
| introspection { | ||
| endpointUrl.set(apiUrl) | ||
| schemaFile.set(file("src/main/graphql/schema.graphqls")) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| mutation LoginUser($idToken: String!) { | ||
| loginUser(idToken: $idToken) { | ||
| accessToken | ||
| refreshToken | ||
| } | ||
| } | ||
|
|
||
| mutation SignupUser($idToken: String!) { | ||
| signupUser(idToken: $idToken) { | ||
| accessToken | ||
| refreshToken | ||
| } | ||
| } | ||
|
|
||
| mutation RefreshAccessToken { | ||
| refreshAccessToken { | ||
| newAccessToken | ||
| } | ||
| } | ||
|
|
||
| mutation LogoutUser { | ||
| logoutUser { | ||
| success | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.cornellappdev.score | ||
|
|
||
| import androidx.compose.foundation.background | ||
| import androidx.compose.foundation.layout.Box | ||
| import androidx.compose.foundation.layout.fillMaxSize | ||
| import androidx.compose.material3.CircularProgressIndicator | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.LaunchedEffect | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.mutableStateOf | ||
| import androidx.compose.runtime.saveable.rememberSaveable | ||
| import androidx.compose.runtime.setValue | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.graphics.Color | ||
| import androidx.lifecycle.compose.collectAsStateWithLifecycle | ||
| import com.cornellappdev.score.auth.AuthState | ||
| import com.cornellappdev.score.auth.AuthStatusOverlay | ||
| import com.cornellappdev.score.auth.AuthViewModel | ||
| import com.cornellappdev.score.model.OnboardingPreferencesRepository | ||
| import com.cornellappdev.score.nav.root.RootNavigation | ||
| import com.cornellappdev.score.screen.OnboardingScreen | ||
| import com.cornellappdev.score.theme.CrimsonPrimary | ||
|
|
||
| @Composable | ||
| fun ScoreApp( | ||
| authViewModel: AuthViewModel, | ||
| onboardingPreferences: OnboardingPreferencesRepository, | ||
| onSignIn: () -> Unit, | ||
| modifier: Modifier = Modifier, | ||
| ) { | ||
| val authState by authViewModel.authState.collectAsStateWithLifecycle() | ||
| val signInState by authViewModel.signInState.collectAsStateWithLifecycle() | ||
| var skippedWelcome by rememberSaveable { | ||
| mutableStateOf(onboardingPreferences.isOnboardingCompleted()) | ||
| } | ||
| val signedIn = authState is AuthState.SignedIn | ||
|
|
||
| LaunchedEffect(signedIn) { | ||
| if (signedIn) { | ||
| onboardingPreferences.markOnboardingCompleted() | ||
| skippedWelcome = true | ||
| } | ||
| } | ||
|
|
||
| Box(modifier.fillMaxSize().background(Color.White)) { | ||
| when { | ||
| authState == AuthState.Loading -> | ||
| Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { | ||
| CircularProgressIndicator(color = CrimsonPrimary) | ||
| } | ||
| signedIn || skippedWelcome -> RootNavigation() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Keep sign-in reachable after “Sign Up Later”. When a guest selects “Sign Up Later”, the callback persists onboarding completion. This condition then hides the welcome sign-in buttons on every later launch. Existing installations with completed onboarding also take this path. The root graph has schedule, scores and game-detail destinations, but no authentication destination. (raw.githubusercontent.com) Keep guest browsing, but add a reachable sign-in action from
|
||
| else -> | ||
| OnboardingScreen( | ||
| onSignUp = onSignIn, | ||
| onLogIn = onSignIn, | ||
| onLater = { | ||
| onboardingPreferences.markOnboardingCompleted() | ||
| skippedWelcome = true | ||
| }, | ||
| ) | ||
| } | ||
| AuthStatusOverlay(signInState, authViewModel::dismissError) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package com.cornellappdev.score.auth | ||
|
|
||
| import com.apollographql.apollo.ApolloClient | ||
| import com.apollographql.apollo.api.ApolloResponse | ||
| import com.apollographql.apollo.api.http.HttpHeader | ||
| import com.apollographql.apollo.exception.ApolloHttpException | ||
| import com.example.score.LoginUserMutation | ||
| import com.example.score.LogoutUserMutation | ||
| import com.example.score.RefreshAccessTokenMutation | ||
| import com.example.score.SignupUserMutation | ||
| import javax.inject.Inject | ||
| import javax.inject.Named | ||
|
|
||
| interface AuthApi { | ||
| suspend fun login(idToken: String): AuthTokens | ||
|
|
||
| suspend fun signup(idToken: String): AuthTokens | ||
|
|
||
| suspend fun refresh(refreshToken: String): String | ||
|
|
||
| suspend fun revoke(token: String) | ||
| } | ||
|
|
||
| class UserNotFoundException : Exception() | ||
|
|
||
| class InvalidSessionException : Exception() | ||
|
|
||
| class ApolloAuthApi @Inject constructor(@Named("auth") private val client: ApolloClient) : AuthApi { | ||
| override suspend fun login(idToken: String): AuthTokens { | ||
| val response = client.mutation(LoginUserMutation(idToken)).execute() | ||
| if (response.errors?.any { it.message.removeSuffix(".") == "User not found" } == true) { | ||
| throw UserNotFoundException() | ||
| } | ||
| response.checkErrors() | ||
| val result = response.data?.loginUser ?: error("Missing login response") | ||
| return tokens(result.accessToken, result.refreshToken) | ||
| } | ||
|
|
||
| override suspend fun signup(idToken: String): AuthTokens { | ||
| val response = client.mutation(SignupUserMutation(idToken)).execute() | ||
| response.checkErrors() | ||
| val result = response.data?.signupUser ?: error("Missing signup response") | ||
| return tokens(result.accessToken, result.refreshToken) | ||
| } | ||
|
|
||
| override suspend fun refresh(refreshToken: String): String { | ||
| val response = | ||
| client | ||
| .mutation(RefreshAccessTokenMutation()) | ||
| .httpHeaders(listOf(HttpHeader("Authorization", "Bearer $refreshToken"))) | ||
| .execute() | ||
| val status = (response.exception as? ApolloHttpException)?.statusCode | ||
| if (status == 422 || response.isSessionRejected()) throw InvalidSessionException() | ||
| response.checkErrors() | ||
| return response.data?.refreshAccessToken?.newAccessToken?.takeIf { it.isNotBlank() } | ||
| ?: error("Missing refresh response") | ||
| } | ||
|
|
||
| override suspend fun revoke(token: String) { | ||
| client | ||
| .mutation(LogoutUserMutation()) | ||
| .httpHeaders(listOf(HttpHeader("Authorization", "Bearer $token"))) | ||
| .execute() | ||
| .checkErrors() | ||
| } | ||
|
|
||
| private fun tokens(access: String?, refresh: String?): AuthTokens { | ||
| require(!access.isNullOrBlank() && !refresh.isNullOrBlank()) { "Missing session tokens" } | ||
| return AuthTokens(access, refresh) | ||
| } | ||
|
|
||
| private fun ApolloResponse<*>.isSessionRejected(): Boolean { | ||
| if ((exception as? ApolloHttpException)?.statusCode == 401) return true | ||
| return errors.orEmpty().any { | ||
| it.extensions?.get("code") == "UNAUTHENTICATED" || | ||
| it.message in | ||
| setOf( | ||
| "Token has expired", | ||
| "Token has been revoked", | ||
| "Signature verification failed", | ||
| "Not enough segments", | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private fun ApolloResponse<*>.checkErrors() { | ||
| exception?.let { throw it } | ||
| check(errors.isNullOrEmpty()) { "Authentication request failed" } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.cornellappdev.score.auth | ||
|
|
||
| import com.apollographql.apollo.ApolloClient | ||
| import com.cornellappdev.score.BuildConfig | ||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import javax.inject.Named | ||
| import javax.inject.Singleton | ||
|
|
||
| @Module | ||
| @InstallIn(SingletonComponent::class) | ||
| abstract class AuthModule { | ||
| @Binds @Singleton abstract fun bindStorage(storage: EncryptedSessionStorage): SessionStorage | ||
|
|
||
| @Binds @Singleton abstract fun bindApi(api: ApolloAuthApi): AuthApi | ||
|
|
||
| companion object { | ||
| @Provides | ||
| @Singleton | ||
| @Named("auth") | ||
| fun provideAuthClient(): ApolloClient = | ||
| ApolloClient.Builder().serverUrl(BuildConfig.BASE_URL).build() | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this is all probably correct, it's unclear who this is meant for. It's probably more useful to focus on the setup steps for future devs.