This repository has been archived by the owner on Sep 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
43 changed files
with
2,082 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
*.iml | ||
.idea/* | ||
.gradle | ||
/gradle | ||
/local.properties | ||
.DS_Store | ||
/build | ||
/captures | ||
.externalNativeBuild |
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,35 @@ | ||
# How to become a contributor and submit your own code | ||
|
||
## Contributor License Agreements | ||
|
||
We'd love to accept your sample apps and patches! Before we can take them, we | ||
have to jump a couple of legal hurdles. | ||
|
||
Please fill out either the individual or corporate Contributor License Agreement | ||
(CLA). | ||
|
||
* If you are an individual writing original source code and you're sure you | ||
own the intellectual property, then you'll need to sign an [individual CLA] | ||
(https://developers.google.com/open-source/cla/individual). | ||
* If you work for a company that wants to allow you to contribute your work, | ||
then you'll need to sign a [corporate CLA] | ||
(https://developers.google.com/open-source/cla/corporate). | ||
|
||
Follow either of the two links above to access the appropriate CLA and | ||
instructions for how to sign and return it. Once we receive it, we'll be able to | ||
accept your pull requests. | ||
|
||
## Contributing A Patch | ||
|
||
1. Submit an issue describing your proposed change to the repo in question. | ||
1. The repo owner will respond to your issue promptly. | ||
1. If your proposed change is accepted, and you haven't already done so, sign a | ||
Contributor License Agreement (see details above). | ||
1. Fork the desired repo, develop and test your code changes. | ||
1. Ensure that your code adheres to the existing style in the sample to which | ||
you are contributing. Refer to the | ||
[Google Cloud Platform Samples Style Guide] | ||
(https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the | ||
recommended coding standards for this organization. | ||
1. Ensure that your code has an appropriate set of unit tests which all pass. | ||
1. Submit a pull request. |
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 +1,25 @@ | ||
# android-kotlin-login | ||
Implementing Login on Android with FirebaseUI | ||
========================= | ||
|
||
This repository contains the final code for the Login Codelab. | ||
|
||
License | ||
------- | ||
|
||
Copyright 2019 Google, Inc. | ||
|
||
Licensed to the Apache Software Foundation (ASF) under one or more contributor | ||
license agreements. See the NOTICE file distributed with this work for | ||
additional information regarding copyright ownership. The ASF licenses this | ||
file to you under the Apache License, Version 2.0 (the "License"); you may not | ||
use this file except in compliance with the License. You may obtain a copy of | ||
the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
License for the specific language governing permissions and limitations under | ||
the License. | ||
|
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 @@ | ||
/build |
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,60 @@ | ||
/* | ||
* Copyright 2019, The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
apply plugin: 'com.android.application' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion 29 | ||
defaultConfig { | ||
applicationId "com.example.android.firebaseui_login_sample" | ||
minSdkVersion 19 | ||
targetSdkVersion 29 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
dataBinding { | ||
enabled = true | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'androidx.appcompat:appcompat:1.0.2' | ||
implementation 'androidx.activity:activity-ktx:1.1.0-alpha01' | ||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3' | ||
implementation 'androidx.core:core-ktx:1.0.2' | ||
implementation 'androidx.fragment:fragment-ktx:1.2.0-alpha01' | ||
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0' | ||
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha02" | ||
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02' | ||
implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0' | ||
implementation "androidx.navigation:navigation-ui-ktx:2.0.0" | ||
implementation 'androidx.preference:preference-ktx:1.1.0-rc01' | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
implementation 'com.firebaseui:firebase-ui-auth:5.0.0' | ||
|
||
testImplementation 'junit:junit:4.12' | ||
} | ||
|
||
apply plugin: 'com.google.gms.google-services' |
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,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
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,37 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
~ Copyright 2019, The Android Open Source Project | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ https://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
--> | ||
|
||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.example.android.firebaseui_login_sample"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
<activity android:name="com.example.android.firebaseui_login_sample.MainActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN"/> | ||
|
||
<category android:name="android.intent.category.LAUNCHER"/> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
56 changes: 56 additions & 0 deletions
56
app/src/main/java/com/example/android/firebaseui_login_sample/FirebaseUserLiveData.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,56 @@ | ||
/* | ||
* Copyright 2019, The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.android.firebaseui_login_sample | ||
|
||
import com.google.firebase.auth.FirebaseUser | ||
import com.google.firebase.auth.FirebaseAuth | ||
import androidx.lifecycle.LiveData | ||
|
||
/** | ||
* This class observes the current FirebaseUser. If there is no logged in user, FirebaseUser will | ||
* be null. | ||
* | ||
* Note that onActive() and onInactive() will get triggered when the configuration changes (for | ||
* example when the device is rotated). This may be undesirable or expensive depending on the | ||
* nature of your LiveData object, but is okay for this purpose since we are only adding and | ||
* removing the authStateListener. | ||
*/ | ||
class FirebaseUserLiveData : LiveData<FirebaseUser?>() { | ||
private val firebaseAuth = FirebaseAuth.getInstance() | ||
|
||
// TODO set the value of this FireUserLiveData object by hooking it up to equal the value of the | ||
// current FirebaseUser. You can utilize the FirebaseAuth.AuthStateListener callback to get | ||
// updates on the current Firebase user logged into the app. | ||
private val authStateListener = FirebaseAuth.AuthStateListener { firebaseAuth -> | ||
// Use the FirebaseAuth instance instantiated at the beginning of the class to get an entry | ||
// point into the Firebase Authentication SDK the app is using. | ||
// With an instance of the FirebaseAuth class, you can now query for the current user. | ||
value = firebaseAuth.currentUser | ||
} | ||
|
||
// When this object has an active observer, start observing the FirebaseAuth state to see if | ||
// there is currently a logged in user. | ||
override fun onActive() { | ||
firebaseAuth.addAuthStateListener(authStateListener) | ||
} | ||
|
||
// When this object no longer has an active observer, stop observing the FirebaseAuth state to | ||
// prevent memory leaks. | ||
override fun onInactive() { | ||
firebaseAuth.removeAuthStateListener(authStateListener) | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
app/src/main/java/com/example/android/firebaseui_login_sample/LoginFragment.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,106 @@ | ||
/* | ||
* Copyright 2019, The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.android.firebaseui_login_sample | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.fragment.app.Fragment | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.lifecycle.Observer | ||
import androidx.navigation.fragment.findNavController | ||
import androidx.activity.addCallback | ||
import androidx.databinding.DataBindingUtil | ||
import androidx.fragment.app.viewModels | ||
import androidx.navigation.NavController | ||
import com.example.android.firebaseui_login_sample.databinding.* | ||
import com.firebase.ui.auth.AuthUI | ||
import com.firebase.ui.auth.IdpResponse | ||
import com.google.android.material.snackbar.Snackbar | ||
import com.google.firebase.auth.FirebaseAuth | ||
|
||
class LoginFragment : Fragment() { | ||
|
||
companion object { | ||
const val TAG = "LoginFragment" | ||
const val SIGN_IN_RESULT_CODE = 1001 | ||
} | ||
|
||
// Get a reference to the ViewModel scoped to this Fragment. | ||
private val viewModel by viewModels<LoginViewModel>() | ||
|
||
private lateinit var navController: NavController | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? | ||
): View? { | ||
|
||
// Inflate the layout for this fragment. | ||
val binding = DataBindingUtil.inflate<FragmentLoginBinding>( | ||
inflater, R.layout.fragment_login, container, false | ||
) | ||
|
||
binding.authButton.setOnClickListener { launchSignInFlow() } | ||
|
||
return binding.root | ||
} | ||
|
||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||
super.onViewCreated(view, savedInstanceState) | ||
|
||
navController = findNavController() | ||
} | ||
|
||
private fun launchSignInFlow() { | ||
// Give users the option to sign in / register with their email or Google account. If users | ||
// choose to register with their email, they will need to create a password as well. | ||
val providers = arrayListOf( | ||
AuthUI.IdpConfig.EmailBuilder().build(), AuthUI.IdpConfig.GoogleBuilder().build() | ||
) | ||
|
||
// Create and launch sign-in intent. We listen to the response of this activity with the | ||
// SIGN_IN_RESULT_CODE code. | ||
startActivityForResult( | ||
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders( | ||
providers | ||
).build(), SIGN_IN_RESULT_CODE | ||
) | ||
} | ||
|
||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||
super.onActivityResult(requestCode, resultCode, data) | ||
if (requestCode == SIGN_IN_RESULT_CODE) { | ||
val response = IdpResponse.fromResultIntent(data) | ||
if (resultCode == Activity.RESULT_OK) { | ||
// Successfully signed in user. | ||
Log.i( | ||
TAG, | ||
"Successfully signed in user " + | ||
"${FirebaseAuth.getInstance().currentUser?.displayName}!" | ||
) | ||
} else { | ||
// Sign in failed. If response is null the user canceled the sign-in flow using | ||
// the back button. Otherwise check response.getError().getErrorCode() and handle | ||
// the error. | ||
Log.i(TAG, "Sign in unsuccessful ${response?.error?.errorCode}") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.