Skip to content

Commit 83a0830

Browse files
committed
add firebaseUI auth
1 parent 95c1639 commit 83a0830

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+2089
-19
lines changed

16-sports-app/.idea/deploymentTargetDropDown.xml

-17
This file was deleted.

17-fun-facts/.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.iml
2+
.idea/*
3+
.gradle
4+
/gradle
5+
/local.properties
6+
.DS_Store
7+
/build
8+
/captures
9+
.externalNativeBuild

17-fun-facts/CONTRIBUTING.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# How to become a contributor and submit your own code
2+
3+
## Contributor License Agreements
4+
5+
We'd love to accept your sample apps and patches! Before we can take them, we
6+
have to jump a couple of legal hurdles.
7+
8+
Please fill out either the individual or corporate Contributor License Agreement
9+
(CLA).
10+
11+
* If you are an individual writing original source code and you're sure you
12+
own the intellectual property, then you'll need to sign an [individual CLA]
13+
(https://developers.google.com/open-source/cla/individual).
14+
* If you work for a company that wants to allow you to contribute your work,
15+
then you'll need to sign a [corporate CLA]
16+
(https://developers.google.com/open-source/cla/corporate).
17+
18+
Follow either of the two links above to access the appropriate CLA and
19+
instructions for how to sign and return it. Once we receive it, we'll be able to
20+
accept your pull requests.
21+
22+
## Contributing A Patch
23+
24+
1. Submit an issue describing your proposed change to the repo in question.
25+
1. The repo owner will respond to your issue promptly.
26+
1. If your proposed change is accepted, and you haven't already done so, sign a
27+
Contributor License Agreement (see details above).
28+
1. Fork the desired repo, develop and test your code changes.
29+
1. Ensure that your code adheres to the existing style in the sample to which
30+
you are contributing. Refer to the
31+
[Google Cloud Platform Samples Style Guide]
32+
(https://github.com/GoogleCloudPlatform/Template/wiki/style.html) for the
33+
recommended coding standards for this organization.
34+
1. Ensure that your code has an appropriate set of unit tests which all pass.
35+
1. Submit a pull request.

17-fun-facts/LICENSE

+613
Large diffs are not rendered by default.

17-fun-facts/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Fun Facts
2+
3+
An app that displays fun facts about Android, using FirebaseUI for authentication.
4+
5+
<!-- <p align="center">
6+
<img src="screenshot.png" style="width:528px;max-width: 100%;">
7+
</p> -->
8+
9+
## Features
10+
11+
- adding Firebase to the project.
12+
- implementing the login feature.
13+
- adding a custom greeting for users who are logged in.
14+
- implementing the logout feature.
15+
16+
Based on [Advanced Android in Kotlin 06.1: Android Login with FirebaseUI](https://developer.android.com/codelabs/advanced-android-kotlin-training-login#0) by Google Codelabs (2022).

17-fun-facts/app/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

17-fun-facts/app/build.gradle

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
apply plugin: 'com.android.application'
18+
apply plugin: 'kotlin-android'
19+
20+
android {
21+
compileSdkVersion 29
22+
defaultConfig {
23+
applicationId "com.example.android.firebaseui_login_sample"
24+
minSdkVersion 19
25+
targetSdkVersion 29
26+
versionCode 1
27+
versionName "1.0"
28+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
29+
}
30+
buildTypes {
31+
release {
32+
minifyEnabled false
33+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
34+
}
35+
}
36+
dataBinding {
37+
enabled = true
38+
}
39+
}
40+
41+
dependencies {
42+
implementation fileTree(dir: 'libs', include: ['*.jar'])
43+
implementation 'androidx.appcompat:appcompat:1.0.2'
44+
implementation 'androidx.activity:activity-ktx:1.1.0-alpha01'
45+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
46+
implementation 'androidx.core:core-ktx:1.0.2'
47+
implementation 'androidx.fragment:fragment-ktx:1.2.0-alpha01'
48+
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
49+
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.2.0-alpha02"
50+
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0-alpha02'
51+
implementation 'androidx.navigation:navigation-fragment-ktx:2.0.0'
52+
implementation "androidx.navigation:navigation-ui-ktx:2.0.0"
53+
implementation 'androidx.preference:preference-ktx:1.1.0-rc01'
54+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
55+
implementation 'com.firebaseui:firebase-ui-auth:5.0.0'
56+
57+
testImplementation 'junit:junit:4.12'
58+
}
59+
60+
apply plugin: 'com.google.gms.google-services'

17-fun-facts/app/proguard-rules.pro

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2019, The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
19+
package="com.example.android.firebaseui_login_sample">
20+
21+
<application
22+
android:allowBackup="true"
23+
android:icon="@mipmap/ic_launcher"
24+
android:label="@string/app_name"
25+
android:roundIcon="@mipmap/ic_launcher_round"
26+
android:supportsRtl="true"
27+
android:theme="@style/AppTheme">
28+
<activity android:name="com.example.android.firebaseui_login_sample.MainActivity">
29+
<intent-filter>
30+
<action android:name="android.intent.action.MAIN"/>
31+
32+
<category android:name="android.intent.category.LAUNCHER"/>
33+
</intent-filter>
34+
</activity>
35+
</application>
36+
37+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.firebaseui_login_sample
18+
19+
import com.google.firebase.auth.FirebaseUser
20+
import com.google.firebase.auth.FirebaseAuth
21+
import androidx.lifecycle.LiveData
22+
23+
/**
24+
* This class observes the current FirebaseUser. If there is no logged in user, FirebaseUser will
25+
* be null.
26+
*
27+
* Note that onActive() and onInactive() will get triggered when the configuration changes (for
28+
* example when the device is rotated). This may be undesirable or expensive depending on the
29+
* nature of your LiveData object, but is okay for this purpose since we are only adding and
30+
* removing the authStateListener.
31+
*/
32+
class FirebaseUserLiveData : LiveData<FirebaseUser?>() {
33+
private val firebaseAuth = FirebaseAuth.getInstance()
34+
35+
// TODO set the value of this FireUserLiveData object by hooking it up to equal the value of the
36+
// current FirebaseUser. You can utilize the FirebaseAuth.AuthStateListener callback to get
37+
// updates on the current Firebase user logged into the app.
38+
private val authStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
39+
// Use the FirebaseAuth instance instantiated at the beginning of the class to get an entry
40+
// point into the Firebase Authentication SDK the app is using.
41+
// With an instance of the FirebaseAuth class, you can now query for the current user.
42+
value = firebaseAuth.currentUser
43+
}
44+
45+
// When this object has an active observer, start observing the FirebaseAuth state to see if
46+
// there is currently a logged in user.
47+
override fun onActive() {
48+
firebaseAuth.addAuthStateListener(authStateListener)
49+
}
50+
51+
// When this object no longer has an active observer, stop observing the FirebaseAuth state to
52+
// prevent memory leaks.
53+
override fun onInactive() {
54+
firebaseAuth.removeAuthStateListener(authStateListener)
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2019, The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.android.firebaseui_login_sample
18+
19+
import android.app.Activity
20+
import android.content.Intent
21+
import android.os.Bundle
22+
import android.util.Log
23+
import androidx.fragment.app.Fragment
24+
import android.view.LayoutInflater
25+
import android.view.View
26+
import android.view.ViewGroup
27+
import androidx.lifecycle.Observer
28+
import androidx.navigation.fragment.findNavController
29+
import androidx.activity.addCallback
30+
import androidx.databinding.DataBindingUtil
31+
import androidx.fragment.app.viewModels
32+
import androidx.navigation.NavController
33+
import com.example.android.firebaseui_login_sample.databinding.*
34+
import com.firebase.ui.auth.AuthUI
35+
import com.firebase.ui.auth.IdpResponse
36+
import com.google.android.material.snackbar.Snackbar
37+
import com.google.firebase.auth.FirebaseAuth
38+
39+
class LoginFragment : Fragment() {
40+
41+
companion object {
42+
const val TAG = "LoginFragment"
43+
const val SIGN_IN_RESULT_CODE = 1001
44+
}
45+
46+
// Get a reference to the ViewModel scoped to this Fragment.
47+
private val viewModel by viewModels<LoginViewModel>()
48+
49+
private lateinit var navController: NavController
50+
51+
override fun onCreateView(
52+
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
53+
): View? {
54+
55+
// Inflate the layout for this fragment.
56+
val binding = DataBindingUtil.inflate<FragmentLoginBinding>(
57+
inflater, R.layout.fragment_login, container, false
58+
)
59+
60+
binding.authButton.setOnClickListener { launchSignInFlow() }
61+
62+
return binding.root
63+
}
64+
65+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
66+
super.onViewCreated(view, savedInstanceState)
67+
68+
navController = findNavController()
69+
}
70+
71+
private fun launchSignInFlow() {
72+
// Give users the option to sign in / register with their email or Google account. If users
73+
// choose to register with their email, they will need to create a password as well.
74+
val providers = arrayListOf(
75+
AuthUI.IdpConfig.EmailBuilder().build(), AuthUI.IdpConfig.GoogleBuilder().build()
76+
)
77+
78+
// Create and launch sign-in intent. We listen to the response of this activity with the
79+
// SIGN_IN_RESULT_CODE code.
80+
startActivityForResult(
81+
AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(
82+
providers
83+
).build(), SIGN_IN_RESULT_CODE
84+
)
85+
}
86+
87+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
88+
super.onActivityResult(requestCode, resultCode, data)
89+
if (requestCode == SIGN_IN_RESULT_CODE) {
90+
val response = IdpResponse.fromResultIntent(data)
91+
if (resultCode == Activity.RESULT_OK) {
92+
// Successfully signed in user.
93+
Log.i(
94+
TAG,
95+
"Successfully signed in user " +
96+
"${FirebaseAuth.getInstance().currentUser?.displayName}!"
97+
)
98+
} else {
99+
// Sign in failed. If response is null the user canceled the sign-in flow using
100+
// the back button. Otherwise check response.getError().getErrorCode() and handle
101+
// the error.
102+
Log.i(TAG, "Sign in unsuccessful ${response?.error?.errorCode}")
103+
}
104+
}
105+
}
106+
}

0 commit comments

Comments
 (0)