|
| 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