Skip to content
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

Google sign in + snapshot releases #14

Merged
merged 1 commit into from
Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Auto deploy snapshot artifacts on draft release

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-20.04

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- name: Check out code
uses: actions/[email protected]
with:
fetch-depth: '0'

- name: Release tag
run: |
git describe --tags --always --first-parent
git tag
- name: Setup java
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: 11

- name: Copy CI gradle.properties
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties

- name: Generate cache key
run: ./scripts/checksum.sh ./ checksum.txt

- uses: actions/cache@v2
with:
path: |
~/.gradle/caches/modules-*
~/.gradle/caches/jars-*
~/.gradle/caches/build-cache-*
build-cache
key: gradle-${{ hashFiles('checksum.txt') }}

- name: Publish application
run: |
echo VERSION_NAME=1.0.0-SNAPSHOT > ./local.properties
./gradlew publishToSonatype
env:
OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }}
OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }}
SONATYPE_STAGING_PROFILE_ID: ${{ secrets.SONATYPE_STAGING_PROFILE_ID }}
SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }}
SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}
SIGNING_KEY: ${{ secrets.SIGNING_KEY }}

- name: Upload reports
uses: actions/upload-artifact@v2
if: failure()
with:
name: Reports
path: '**/build/reports/*'
retention-days: 2
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ build
.cxx
local.properties
google-services.json
build-cache
secrets.properties
build-cache
*.sarif
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,29 @@ dependencies {
}
```

<details>
<summary>Snapshots of the development version are available in Sonatype's snapshots repository.</summary>
<p>

[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/se.warting.firebase-compose/auth?server=https%3A%2F%2Foss.sonatype.org)](https://oss.sonatype.org/content/repositories/snapshots/se/warting/firebase-compose/auth/)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/se.warting.firebase-compose/core?server=https%3A%2F%2Foss.sonatype.org)](https://oss.sonatype.org/content/repositories/snapshots/se/warting/firebase-compose/core/)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/se.warting.firebase-compose/dynamic-links?server=https%3A%2F%2Foss.sonatype.org)](https://oss.sonatype.org/content/repositories/snapshots/se/warting/firebase-compose/dynamic-links/)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/se.warting.firebase-compose/messaging?server=https%3A%2F%2Foss.sonatype.org)](https://oss.sonatype.org/content/repositories/snapshots/se/warting/firebase-compose/messaging/)
[![Sonatype Nexus (Snapshots)](https://img.shields.io/nexus/s/se.warting.firebase-compose/messaging-firestore?server=https%3A%2F%2Foss.sonatype.org)](https://oss.sonatype.org/content/repositories/snapshots/se/warting/firebase-compose/messaging-firestore/)

```groovy
allprojects {
repositories {
// ...
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
}
```
</p>
</details>

## How to use

All you need to do is to call `FirebaseComposeAuth`:
Expand Down
18 changes: 17 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ plugins {

val composeVersion = "1.0.4"


val DEFAULT_WEB_CLIENT_ID: String =
com.android.build.gradle.internal.cxx.configure.gradleLocalProperties(rootDir)
.getProperty("DEFAULT_WEB_CLIENT_ID", "CHANGE_ME")


android {
compileSdk = 31

Expand All @@ -20,6 +26,7 @@ android {
vectorDrawables {
useSupportLibrary = true
}
resValue("string", "web_client_id", DEFAULT_WEB_CLIENT_ID)
}

buildTypes {
Expand All @@ -29,6 +36,7 @@ android {
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
resValue("string", "web_client_id", DEFAULT_WEB_CLIENT_ID)
}
}
compileOptions {
Expand All @@ -50,7 +58,15 @@ android {
}

lint {
lintConfig = file("$rootDir/config/lint/lint.xml")
baseline(file("lint-baseline.xml"))
isCheckReleaseBuilds = true
isCheckAllWarnings = true
isWarningsAsErrors = true
isAbortOnError = true
disable.add("LogConditional")
isCheckDependencies = true
isCheckGeneratedSources = false
sarifOutput = file("../lint-results-app.sarif")
}
}

Expand Down
4 changes: 4 additions & 0 deletions app/lint-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 7.0.3" type="baseline" client="gradle" name="AGP (7.0.3)" variant="all" version="7.0.3">

</issues>
57 changes: 44 additions & 13 deletions app/src/main/java/se/warting/firebasecompose/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.google.firebase.auth.FirebaseAuthInvalidUserException
import kotlinx.coroutines.launch
import se.warting.firebasecompose.auth.AuthEvents
import se.warting.firebasecompose.auth.FirebaseComposeAuth
import se.warting.firebasecompose.auth.LocalFirebaseAuth
import se.warting.firebasecompose.auth.LocalFirebaseAuthState
import se.warting.firebasecompose.auth.ProvideFirebaseComposeAuthLocals
import se.warting.firebasecompose.auth.rememberFirebaseAuthState
Expand All @@ -32,7 +35,22 @@ class MainActivity : ComponentActivity() {
setContent {
MaterialTheme {
Surface {
HighLevel()
HighLevel {
when (it) {
is AuthEvents.FirebaseSignedIn -> Log.d(
"AuthEvents",
"FirebaseSignedIn provider: " + it.provider
)
AuthEvents.FirebaseSignedOut -> Log.d(
"AuthEvents",
"FirebaseSignedOut"
)
AuthEvents.GoogleAuthenticated -> Log.d(
"AuthEvents",
"GoogleAuthenticated"
)
}
}
}
}
}
Expand All @@ -41,8 +59,8 @@ class MainActivity : ComponentActivity() {

@Suppress("unused")
@Composable
fun LowLevel() {
val state = rememberFirebaseAuthState()
fun LowLevel(eventListener: (AuthEvents) -> Unit = {}) {
val state = rememberFirebaseAuthState(eventListener)

CompositionLocalProvider(
LocalFirebaseAuthState provides state,
Expand All @@ -60,8 +78,8 @@ fun LowLevel() {
}

@Composable
fun MidLevel() {
ProvideFirebaseComposeAuthLocals {
fun MidLevel(eventListener: (AuthEvents) -> Unit = {}) {
ProvideFirebaseComposeAuthLocals(eventListener) {
// LocalFirebaseAuthState.current is now available
val state = LocalFirebaseAuthState.current
when {
Expand All @@ -72,36 +90,42 @@ fun MidLevel() {
}

@Composable
fun HighLevel() {
fun HighLevel(eventListener: (AuthEvents) -> Unit = {}) {
// LocalFirebaseAuthState.current is now available
FirebaseComposeAuth(
eventListener = eventListener,
loggedInContent = { LoggedIn() },
loggedOutContent = { LoggedOut() }
)
}

@Composable
fun LoggedIn() {
val f = LocalFirebaseAuthState.current
val firebaseAuthState = LocalFirebaseAuthState.current
val firebaseAuth = LocalFirebaseAuth.current

val scope = rememberCoroutineScope()
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Text(text = "Hello anonymous!")
Text(text = f.getUserId() ?: "")
if (firebaseAuth.currentUser?.isAnonymous == true) {
Text(text = "Hello anonymous!")
} else {
Text(text = "Hello " + firebaseAuth.currentUser?.displayName.toString() + "!")
}
Text(text = "UserId: " + firebaseAuthState.getUserId())
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = { f.logout() }) {
Button(onClick = { firebaseAuthState.logout() }) {
Text(text = "Logout!")
}
Spacer(modifier = Modifier.height(16.dp))

Button(onClick = {
scope.launch {
try {
f.getItToken(true)
firebaseAuthState.getItToken(true)
} catch (e: FirebaseAuthInvalidUserException) {
Log.e(
"FirebaseCompose",
Expand All @@ -118,15 +142,22 @@ fun LoggedIn() {

@Composable
fun LoggedOut() {
val f = LocalFirebaseAuthState.current
val firebaseAuthState = LocalFirebaseAuthState.current
val clientId: String = stringResource(id = R.string.web_client_id)
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Text(text = "Hello logged out user!")
Button(onClick = { f.signInAnonymously() }) {
Button(onClick = { firebaseAuthState.signInAnonymously() }) {
Text(text = "Sign in Anonymously!")
}
Spacer(modifier = Modifier.height(16.dp))
Button(onClick = {
firebaseAuthState.signInWithGoogle(clientId)
}) {
Text(text = "Sign in with google!")
}
}
}
49 changes: 46 additions & 3 deletions auth/api/auth.api
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
public abstract class se/warting/firebasecompose/auth/AuthEvents {
public static final field $stable I
}

public final class se/warting/firebasecompose/auth/AuthEvents$FirebaseSignedIn : se/warting/firebasecompose/auth/AuthEvents {
public static final field $stable I
public fun <init> (Ljava/lang/String;)V
public final fun component1 ()Ljava/lang/String;
public final fun copy (Ljava/lang/String;)Lse/warting/firebasecompose/auth/AuthEvents$FirebaseSignedIn;
public static synthetic fun copy$default (Lse/warting/firebasecompose/auth/AuthEvents$FirebaseSignedIn;Ljava/lang/String;ILjava/lang/Object;)Lse/warting/firebasecompose/auth/AuthEvents$FirebaseSignedIn;
public fun equals (Ljava/lang/Object;)Z
public final fun getProvider ()Ljava/lang/String;
public fun hashCode ()I
public fun toString ()Ljava/lang/String;
}

public final class se/warting/firebasecompose/auth/AuthEvents$FirebaseSignedOut : se/warting/firebasecompose/auth/AuthEvents {
public static final field $stable I
public static final field INSTANCE Lse/warting/firebasecompose/auth/AuthEvents$FirebaseSignedOut;
}

public final class se/warting/firebasecompose/auth/AuthEvents$GoogleAuthenticated : se/warting/firebasecompose/auth/AuthEvents {
public static final field $stable I
public static final field INSTANCE Lse/warting/firebasecompose/auth/AuthEvents$GoogleAuthenticated;
}

public final class se/warting/firebasecompose/auth/BuildConfig {
public static final field BUILD_TYPE Ljava/lang/String;
public static final field DEBUG Z
Expand All @@ -13,19 +39,36 @@ public abstract interface class se/warting/firebasecompose/auth/FirebaseAuthStat
public abstract fun logout ()V
public abstract fun removeAuthStateListener (Lcom/google/firebase/auth/FirebaseAuth$AuthStateListener;)V
public abstract fun signInAnonymously ()V
public abstract fun signInWithGoogle (Ljava/lang/String;)V
public abstract fun updateLoggedInState ()V
}

public final class se/warting/firebasecompose/auth/FirebaseAuthStateKt {
public static final fun rememberFirebaseAuthState (Landroidx/compose/runtime/Composer;I)Lse/warting/firebasecompose/auth/FirebaseAuthState;
public static final fun rememberFirebaseAuthState (Lkotlin/jvm/functions/Function1;Landroidx/compose/runtime/Composer;II)Lse/warting/firebasecompose/auth/FirebaseAuthState;
}

public final class se/warting/firebasecompose/auth/FirebaseComposeAuthKt {
public static final fun FirebaseComposeAuth (Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;I)V
public static final fun FirebaseComposeAuth (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
}

public final class se/warting/firebasecompose/auth/FirebaseSignInResultContracts {
public static final field $stable I
public fun <init> ()V
}

public final class se/warting/firebasecompose/auth/FirebaseSignInResultContracts$SignInWithGoogle : androidx/activity/result/contract/ActivityResultContract {
public static final field $stable I
public fun <init> ()V
public synthetic fun createIntent (Landroid/content/Context;Ljava/lang/Object;)Landroid/content/Intent;
public fun createIntent (Landroid/content/Context;Ljava/lang/String;)Landroid/content/Intent;
public synthetic fun getSynchronousResult (Landroid/content/Context;Ljava/lang/Object;)Landroidx/activity/result/contract/ActivityResultContract$SynchronousResult;
public fun getSynchronousResult (Landroid/content/Context;Ljava/lang/String;)Landroidx/activity/result/contract/ActivityResultContract$SynchronousResult;
public fun parseResult (ILandroid/content/Intent;)Lcom/google/firebase/auth/AuthCredential;
public synthetic fun parseResult (ILandroid/content/Intent;)Ljava/lang/Object;
}

public final class se/warting/firebasecompose/auth/ProvideAndroidCompositionLocalsKt {
public static final fun ProvideFirebaseComposeAuthLocals (Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;I)V
public static final fun ProvideFirebaseComposeAuthLocals (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun getLocalFirebaseAuth ()Landroidx/compose/runtime/ProvidableCompositionLocal;
public static final fun getLocalFirebaseAuthState ()Landroidx/compose/runtime/ProvidableCompositionLocal;
}
Expand Down
Loading