forked from tukcomCD2024/ISP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat : Implement Google Login with Firebase
1. Implement Splash Activity. - Set delay(1000) and then move Activity to Login Activity 2. Implement Google Login - Receive IDToken from Google Server - Backend server communication not implemented - Auto Login not implemented 3. Add KTX libraries
- Loading branch information
Showing
17 changed files
with
304 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"project_info": { | ||
"project_number": "740845966307", | ||
"project_id": "howabouttrip", | ||
"storage_bucket": "howabouttrip.appspot.com" | ||
}, | ||
"client": [ | ||
{ | ||
"client_info": { | ||
"mobilesdk_app_id": "1:740845966307:android:71aff1bfe733b688ad46d6", | ||
"android_client_info": { | ||
"package_name": "com.project.how" | ||
} | ||
}, | ||
"oauth_client": [ | ||
{ | ||
"client_id": "740845966307-oulmuvm5mv595vlu9hf1a3j5n4ouvskg.apps.googleusercontent.com", | ||
"client_type": 1, | ||
"android_info": { | ||
"package_name": "com.project.how", | ||
"certificate_hash": "8f29dbe96504294182411cc8b5af35fee8e57de8" | ||
} | ||
}, | ||
{ | ||
"client_id": "740845966307-vhqequ0l999eglm9p87su14da4bjreid.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
], | ||
"api_key": [ | ||
{ | ||
"current_key": "AIzaSyDg0_7_UQD4gNj9XmKnQZpBGREl0r9CZ_Q" | ||
} | ||
], | ||
"services": { | ||
"appinvite_service": { | ||
"other_platform_oauth_client": [ | ||
{ | ||
"client_id": "740845966307-vhqequ0l999eglm9p87su14da4bjreid.apps.googleusercontent.com", | ||
"client_type": 3 | ||
} | ||
] | ||
} | ||
} | ||
} | ||
], | ||
"configuration_version": "1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
android/HowAboutTrip/app/src/main/java/com/project/how/model/LoginRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.project.how.model | ||
|
||
import android.util.Log | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import com.google.firebase.auth.FirebaseAuth | ||
import com.google.firebase.auth.FirebaseUser | ||
import com.google.firebase.auth.GoogleAuthProvider | ||
|
||
class LoginRepository { | ||
private val firebaseAuth = FirebaseAuth.getInstance() | ||
private val _userLiveData = MutableLiveData<FirebaseUser>() | ||
val userLiveData: LiveData<FirebaseUser> | ||
get() = _userLiveData | ||
|
||
fun getUser(idToken: String) { | ||
val credential = GoogleAuthProvider.getCredential(idToken, null) | ||
firebaseAuth.signInWithCredential(credential).addOnCompleteListener{ | ||
if(it.isSuccessful){ | ||
_userLiveData.postValue(firebaseAuth.currentUser) | ||
} else{ | ||
Log.e("GoogleAuthRepository", "getUser is failed\n${it.exception}") | ||
} | ||
} | ||
} | ||
} |
48 changes: 47 additions & 1 deletion
48
android/HowAboutTrip/app/src/main/java/com/project/how/view/activity/LoginActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,58 @@ | ||
package com.project.how.view.activity | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import android.util.Log | ||
import android.widget.Toast | ||
import androidx.activity.result.ActivityResult | ||
import androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
import androidx.activity.viewModels | ||
import androidx.databinding.DataBindingUtil | ||
import com.google.android.gms.auth.api.identity.BeginSignInRequest | ||
import com.google.android.gms.auth.api.signin.GoogleSignIn | ||
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.project.how.BuildConfig | ||
import com.project.how.R | ||
import com.project.how.databinding.ActivityLoginBinding | ||
import com.project.how.view_model.LoginViewModel | ||
|
||
class LoginActivity : AppCompatActivity() { | ||
private lateinit var binding : ActivityLoginBinding | ||
private val viewModel: LoginViewModel by viewModels() | ||
private lateinit var activityResultLauncher : ActivityResultLauncher<Intent> | ||
private lateinit var googleSignInRequest: GoogleSignInClient | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_login) | ||
binding = DataBindingUtil.setContentView(this, R.layout.activity_login) | ||
binding.login = this | ||
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) | ||
.requestIdToken(BuildConfig.GOOGLE_SERVER_ID) | ||
.requestEmail() | ||
.build() | ||
googleSignInRequest = GoogleSignIn.getClient(this, gso) | ||
activityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ | ||
if(it.resultCode == Activity.RESULT_OK) { | ||
val task = GoogleSignIn.getSignedInAccountFromIntent(it.data) | ||
try { | ||
val account = task.result | ||
viewModel.getUser(account.idToken!!) | ||
Log.d("activityResultLauncher", "Login Success\nidToken : ${account.idToken}\nid : ${account.id}\nemail : ${account.email}") | ||
startActivity(Intent(this, MainActivity::class.java)) | ||
} catch (e: Exception){ | ||
Log.e("activityResultLauncher", "Login Failed\nError : ${e.stackTrace}\n${e.message}") | ||
} | ||
} | ||
} | ||
|
||
binding.googleLogin.setOnClickListener { | ||
activityResultLauncher.launch(googleSignInRequest.signInIntent) | ||
binding.lottie.pauseAnimation() | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
android/HowAboutTrip/app/src/main/java/com/project/how/view/activity/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.project.how.view.activity | ||
|
||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import androidx.databinding.DataBindingUtil | ||
import com.project.how.R | ||
import com.project.how.databinding.ActivityMainBinding | ||
|
||
class MainActivity : AppCompatActivity() { | ||
private lateinit var binding: ActivityMainBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
binding= DataBindingUtil.setContentView(this, R.layout.activity_main) | ||
|
||
} | ||
} |
15 changes: 14 additions & 1 deletion
15
android/HowAboutTrip/app/src/main/java/com/project/how/view/activity/SplashActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
package com.project.how.view.activity | ||
|
||
import android.content.Intent | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.os.Bundle | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.lifecycle.lifecycleScope | ||
import com.project.how.R | ||
import com.project.how.databinding.ActivitySplashBinding | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
class SplashActivity : AppCompatActivity() { | ||
private lateinit var binding : ActivitySplashBinding | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_splash) | ||
binding = DataBindingUtil.setContentView(this, R.layout.activity_splash) | ||
binding.splash = this | ||
|
||
lifecycleScope.launch { | ||
delay(1000) | ||
startActivity(Intent(this@SplashActivity, LoginActivity::class.java)) | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
android/HowAboutTrip/app/src/main/java/com/project/how/view_model/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.project.how.view_model | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.ViewModel | ||
import com.google.firebase.auth.FirebaseUser | ||
import com.project.how.model.LoginRepository | ||
|
||
class LoginViewModel : ViewModel() { | ||
private var loginRepository : LoginRepository = LoginRepository() | ||
private val _userLiveData = loginRepository.userLiveData | ||
val userLiveData: LiveData<FirebaseUser> | ||
get() = _userLiveData | ||
|
||
fun getUser(idToken: String){ | ||
loginRepository.getUser(idToken) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
android/HowAboutTrip/app/src/main/res/layout/activity_main.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<layout xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<data> | ||
<import type="android.view.View"/> | ||
<variable | ||
name="main" | ||
type="com.project.how.view.activity.MainActivity" /> | ||
</data> | ||
|
||
<androidx.coordinatorlayout.widget.CoordinatorLayout | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<com.google.android.material.bottomappbar.BottomAppBar | ||
android:id="@+id/bottomAppBar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:layout_gravity="bottom" | ||
app:fabCradleMargin="5dp" | ||
app:fabCradleRoundedCornerRadius="10dp"> | ||
<com.google.android.material.bottomnavigation.BottomNavigationView | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
</com.google.android.material.bottomappbar.BottomAppBar> | ||
|
||
<com.google.android.material.floatingactionbutton.FloatingActionButton | ||
android:id="@+id/addButton" | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:backgroundTint="@color/white" | ||
android:clickable="true" | ||
app:layout_anchor="@+id/bottomAppBar" | ||
app:layout_anchorGravity="top|center" /> | ||
|
||
|
||
</androidx.coordinatorlayout.widget.CoordinatorLayout> | ||
|
||
</layout> |
Oops, something went wrong.