Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
native auth (#73)
Browse files Browse the repository at this point in the history
* Implement firebase integration for android and ios and implement deeplink for android

* native auth

* finish implement google and apple auth

* Implement google auth for android side

* Fixed auth on iphone implement google auth for android

* Implement auth flow before profile open

* Implement auth request

* Implement count visit if user login (just network)

* Polish code

* Implement apple auth for android side

* Implemement firebase crashlitics

* Implement placeholder for visited cocktails

* Fix linter issues
  • Loading branch information
VovaStelmashchuk authored Jun 28, 2023
1 parent 475f9df commit 7488265
Show file tree
Hide file tree
Showing 36 changed files with 1,500 additions and 273 deletions.
85 changes: 48 additions & 37 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,52 +1,63 @@
@file:Suppress("UnusedPrivateMember")

plugins {
kotlin("multiplatform")
id("com.android.application")
id("org.jetbrains.compose")
kotlin("multiplatform")
id("com.android.application")
id("org.jetbrains.compose")
id("com.google.gms.google-services")
id("com.google.firebase.crashlytics")
}

kotlin {
android()
sourceSets {
val androidMain by getting {
dependencies {
implementation(project(":shared"))
}
android()
sourceSets {
val androidMain by getting {
dependencies {
implementation(project(":shared"))
implementation("com.google.android.gms:play-services-auth:20.6.0")
implementation(platform("com.google.firebase:firebase-bom:32.1.1"))
implementation("com.google.firebase:firebase-auth-ktx")
implementation("com.google.firebase:firebase-crashlytics-ktx")
implementation("com.google.firebase:firebase-analytics-ktx")
}
}
}
}
}

android {
compileSdk = (findProperty("android.compileSdk") as String).toInt()
namespace = "org.mixdrinks"
compileSdk = (findProperty("android.compileSdk") as String).toInt()
namespace = "org.mixdrinks"

sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")

defaultConfig {
applicationId = "org.mixdrinks.app"
minSdk = (findProperty("android.minSdk") as String).toInt()
targetSdk = (findProperty("android.targetSdk") as String).toInt()
versionCode = System.getenv("MIXDRINKS_MOBILE_APP_VERSION_CODE")?.toIntOrNull() ?: 1
versionName = System.getenv("MIXDRINKS_MOBILE_APP_VERSION_NAME") ?: "0.0.1"
}
buildTypes {
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
this.resValue("string", "app_name", "MixDrinks")
defaultConfig {
applicationId = "org.mixdrinks.app"
minSdk = (findProperty("android.minSdk") as String).toInt()
targetSdk = (findProperty("android.targetSdk") as String).toInt()
versionCode = System.getenv("MIXDRINKS_MOBILE_APP_VERSION_CODE")?.toIntOrNull() ?: 1
versionName = System.getenv("MIXDRINKS_MOBILE_APP_VERSION_NAME") ?: "0.0.1"
}
getByName("debug") {
applicationIdSuffix = ".debug"
isDebuggable = true
this.resValue("string", "app_name", "Debug_MixDrinks")
buildTypes {
getByName("release") {
isMinifyEnabled = true
isShrinkResources = true
this.resValue("string", "app_name", "MixDrinks")
}
getByName("debug") {
isDebuggable = true
this.resValue("string", "app_name", "MixDrinks")
}
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlin {
jvmToolchain(11)
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlin {
jvmToolchain(11)
}
}

dependencies {
implementation(platform("com.google.firebase:firebase-bom:32.1.1"))
implementation("com.google.firebase:firebase-analytics-ktx")
}
62 changes: 62 additions & 0 deletions androidApp/google-services.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
{
"project_info": {
"project_number": "18528209355",
"project_id": "mixdrinks-bb828",
"storage_bucket": "mixdrinks-bb828.appspot.com"
},
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:18528209355:android:5c2687eb3bada359e5ce4e",
"android_client_info": {
"package_name": "org.mixdrinks.app"
}
},
"oauth_client": [
{
"client_id": "18528209355-ei9b3v6gkcr9rjk29lspvdpfs5e7uvq0.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "org.mixdrinks.app",
"certificate_hash": "391bf5ea50bf4d137b995aec1505aecaf21cb999"
}
},
{
"client_id": "18528209355-nfp25i6n0pn8n98099b04q93plcsgre1.apps.googleusercontent.com",
"client_type": 1,
"android_info": {
"package_name": "org.mixdrinks.app",
"certificate_hash": "39fc6fcdbfcc7a5cdeb112ca87d8089112b82fed"
}
},
{
"client_id": "18528209355-f88aa33m72rshqp56b2so77ujnm7s32d.apps.googleusercontent.com",
"client_type": 3
}
],
"api_key": [
{
"current_key": "AIzaSyB_ICk516QsqWbQdogKkt_9B6V_-lmOgTg"
}
],
"services": {
"appinvite_service": {
"other_platform_oauth_client": [
{
"client_id": "18528209355-8o18kp2sabqr0928mkp0icaatiu265v3.apps.googleusercontent.com",
"client_type": 3
},
{
"client_id": "18528209355-27v672k8ptniriie05l143938toknieo.apps.googleusercontent.com",
"client_type": 2,
"ios_info": {
"bundle_id": "org.mixdrinks.app"
}
}
]
}
}
}
],
"configuration_version": "1"
}
4 changes: 4 additions & 0 deletions androidApp/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@
</activity>
</application>

<meta-data
android:name="firebase_crashlytics_collection_enabled"
android:value="false" />

</manifest>
115 changes: 113 additions & 2 deletions androidApp/src/androidMain/kotlin/org/mixdrinks/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,129 @@
package org.mixdrinks

import MainView
import NewToken
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInAccount
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.android.gms.tasks.Task
import com.google.firebase.FirebaseApp
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.auth.OAuthProvider
import com.google.firebase.crashlytics.ktx.crashlytics
import com.google.firebase.ktx.Firebase
import setAppleAuthStart
import setGoogleAuthStart
import setLogout


class MainActivity : AppCompatActivity() {

lateinit var googleSignInClient: GoogleSignInClient
private lateinit var register: ActivityResultLauncher<Intent>
private lateinit var firebaseAuth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val deepLink = intent?.data?.toString()

setContent {
MainView(deepLink)
}

FirebaseApp.initializeApp(this)

val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()

googleSignInClient = GoogleSignIn.getClient(this, gso)
firebaseAuth = FirebaseAuth.getInstance()

setGoogleAuthStart {
signInGoogle()
}

val appleProvider = OAuthProvider.newBuilder("apple.com")

setAppleAuthStart {
val pending = firebaseAuth.pendingAuthResult
if (pending != null) {
pending.addOnSuccessListener { authResult ->
authResult.user.sendNewToken()
}.addOnFailureListener { e ->
Firebase.crashlytics.recordException(e)
}
} else {
firebaseAuth.startActivityForSignInWithProvider(this, appleProvider.build())
.addOnSuccessListener { authResult ->
authResult.user.sendNewToken()
}
.addOnFailureListener { e ->
Firebase.crashlytics.recordException(e)
}
}
}

setLogout {
FirebaseAuth.getInstance().signOut()
}
}

override fun onStart() {
super.onStart()
register = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
val task: Task<GoogleSignInAccount> = GoogleSignIn.getSignedInAccountFromIntent(result.data)
handleResult(task)
}
}
}

private fun signInGoogle() {
val signInIntent: Intent = googleSignInClient.signInIntent
register.launch(signInIntent)
}

private fun handleResult(completedTask: Task<GoogleSignInAccount>) {
try {
val account: GoogleSignInAccount? = completedTask.getResult(ApiException::class.java)
if (account != null) {
update(account)
}
} catch (e: ApiException) {
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show()
}
}

private fun update(account: GoogleSignInAccount) {
val credential = GoogleAuthProvider.getCredential(account.idToken, null)
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
FirebaseAuth.getInstance().currentUser?.sendNewToken()
}
}

private fun FirebaseUser?.sendNewToken() {
this ?: return
getIdToken(true)
.addOnCompleteListener {
it.result?.token?.let { token ->
NewToken(token)
}
}
.addOnFailureListener {
Firebase.crashlytics.recordException(it)
}
}
}
6 changes: 5 additions & 1 deletion iosApp/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ target 'iosApp' do
use_frameworks!
platform :ios, '14.1'
pod 'shared', :path => '../shared'
end
end

pod 'GoogleSignIn'
pod 'FirebaseCore'
pod 'FirebaseAuth'
Loading

0 comments on commit 7488265

Please sign in to comment.