-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
FacebookLoginActivity.kt
155 lines (131 loc) · 5.28 KB
/
FacebookLoginActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package com.google.firebase.quickstart.auth.kotlin
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import com.facebook.AccessToken
import com.facebook.CallbackManager
import com.facebook.FacebookCallback
import com.facebook.FacebookException
import com.facebook.login.LoginManager
import com.facebook.login.LoginResult
import com.google.firebase.auth.FacebookAuthProvider
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.quickstart.auth.R
import kotlinx.android.synthetic.main.activity_facebook.buttonFacebookLogin
import kotlinx.android.synthetic.main.activity_facebook.buttonFacebookSignout
import kotlinx.android.synthetic.main.activity_facebook.detail
import kotlinx.android.synthetic.main.activity_facebook.status
/**
* Demonstrate Firebase Authentication using a Facebook access token.
*/
class FacebookLoginActivity : BaseActivity(), View.OnClickListener {
// [START declare_auth]
private lateinit var auth: FirebaseAuth
// [END declare_auth]
private lateinit var callbackManager: CallbackManager
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_facebook)
buttonFacebookSignout.setOnClickListener(this)
// [START initialize_auth]
// Initialize Firebase Auth
auth = FirebaseAuth.getInstance()
// [END initialize_auth]
// [START initialize_fblogin]
// Initialize Facebook Login button
callbackManager = CallbackManager.Factory.create()
buttonFacebookLogin.setReadPermissions("email", "public_profile")
buttonFacebookLogin.registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onSuccess(loginResult: LoginResult) {
Log.d(TAG, "facebook:onSuccess:$loginResult")
handleFacebookAccessToken(loginResult.accessToken)
}
override fun onCancel() {
Log.d(TAG, "facebook:onCancel")
// [START_EXCLUDE]
updateUI(null)
// [END_EXCLUDE]
}
override fun onError(error: FacebookException) {
Log.d(TAG, "facebook:onError", error)
// [START_EXCLUDE]
updateUI(null)
// [END_EXCLUDE]
}
})
// [END initialize_fblogin]
}
// [START on_start_check_user]
public override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = auth.currentUser
updateUI(currentUser)
}
// [END on_start_check_user]
// [START on_activity_result]
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Pass the activity result back to the Facebook SDK
callbackManager.onActivityResult(requestCode, resultCode, data)
}
// [END on_activity_result]
// [START auth_with_facebook]
private fun handleFacebookAccessToken(token: AccessToken) {
Log.d(TAG, "handleFacebookAccessToken:$token")
// [START_EXCLUDE silent]
showProgressDialog()
// [END_EXCLUDE]
val credential = FacebookAuthProvider.getCredential(token.token)
auth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success")
val user = auth.currentUser
updateUI(user)
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.exception)
Toast.makeText(baseContext, "Authentication failed.",
Toast.LENGTH_SHORT).show()
updateUI(null)
}
// [START_EXCLUDE]
hideProgressDialog()
// [END_EXCLUDE]
}
}
// [END auth_with_facebook]
fun signOut() {
auth.signOut()
LoginManager.getInstance().logOut()
updateUI(null)
}
private fun updateUI(user: FirebaseUser?) {
hideProgressDialog()
if (user != null) {
status.text = getString(R.string.facebook_status_fmt, user.displayName)
detail.text = getString(R.string.firebase_status_fmt, user.uid)
buttonFacebookLogin.visibility = View.GONE
buttonFacebookSignout.visibility = View.VISIBLE
} else {
status.setText(R.string.signed_out)
detail.text = null
buttonFacebookLogin.visibility = View.VISIBLE
buttonFacebookSignout.visibility = View.GONE
}
}
override fun onClick(v: View) {
val i = v.id
if (i == R.id.buttonFacebookSignout) {
signOut()
}
}
companion object {
private const val TAG = "FacebookLogin"
}
}