Skip to content

Commit 651feee

Browse files
authored
firebase auth and profile code added
1 parent d751527 commit 651feee

File tree

1 file changed

+145
-6
lines changed

1 file changed

+145
-6
lines changed

_drafts/2019-03-25-authentication.md

Lines changed: 145 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ date: 2019-03-25
55
categories: ["Firebase"]
66
image: firebase/authentication
77
description: "Firebase"
8-
keywords: "firebase, autentykacja, autoryzacja, authentication, account, user, email, password, phone, google, facebook, twitter, github, rejestracja, logowanie, sign in, log in, sign up, log out, android, programowanie, programming"
8+
version: Firebase-Auth 16.1, Firebase-UI-Auth 4.1
9+
keywords: "firebase, autentykacja, autoryzacja, authentication, firebaseui, firebaseauth, auth, account, user, email, password, phone, google, facebook, twitter, github, rejestracja, logowanie, sign in, log in, sign up, log out, android, programowanie, programming"
910
---
1011

1112
## Wprowadzenie
@@ -71,14 +72,14 @@ class UserActivity : AppCompatActivity() {
7172
super.onCreate(savedInstanceState)
7273
setContentView(R.layout.activity_user)
7374

74-
signOut.setOnClickListener {
75+
signOutButton.setOnClickListener {
7576
AuthUI.getInstance().signOut(this)
7677
.addOnCompleteListener {
7778
//do something like navigate to home screen
7879
}
7980
}
8081

81-
delete.setOnClickListener {
82+
deleteAccountButton.setOnClickListener {
8283
AuthUI.getInstance().delete(this)
8384
.addOnCompleteListener {
8485
//do something like navigate to home screen
@@ -94,7 +95,58 @@ class UserActivity : AppCompatActivity() {
9495
Kiedy zachodzi potrzeba przejęcia większej kontroli nad procesami uwierzytelniania należy w tym celu wykorzystać `Firebase SDK`, który umożliwia zdefiniowanie zachowania i obsługę zdarzeń na każdym kroku danego procesu. Poniższy listing prezentuje wykorzystanie Firebase SDK w procesie rejestracji za pomocą email i hasła, logowania i wylogowania użytkownika.
9596

9697
{% highlight kotlin %}
97-
//TODO code
98+
class FirebaseAuthActivity : AppCompatActivity() {
99+
100+
private lateinit var auth: FirebaseAuth
101+
102+
override fun onCreate(savedInstanceState: Bundle?) {
103+
super.onCreate(savedInstanceState)
104+
setContentView(R.layout.activity_firebase_auth)
105+
auth = FirebaseAuth.getInstance()
106+
107+
signUpButton.setOnClickListener { updateProfile() }
108+
signInButton.setOnClickListener { changeEmail() }
109+
}
110+
111+
override fun onStart() {
112+
super.onStart()
113+
if(auth.currentUser != null) {
114+
//user is signed in, update UI or navigate
115+
}
116+
}
117+
118+
private fun signUp() {
119+
//validate fields before
120+
val email = emailEditText.getText().toString()
121+
val password = passwordEditText.getText().toString()
122+
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task ->
123+
if (task.isSuccessful) {
124+
//sign up success, update UI or navigate
125+
val user = auth.currentUser
126+
startActivity(Intent(this, ProfileActivity::class.java))
127+
}
128+
else {
129+
//sign up fails, show error message
130+
}
131+
}
132+
}
133+
134+
private fun signIn() {
135+
//validate fields before
136+
val email = emailEditText.getText().toString()
137+
val password = passwordEditText.getText().toString()
138+
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener { task ->
139+
if (task.isSuccessful) {
140+
//sign ip success, update UI or navigate
141+
val user = auth.currentUser
142+
startActivity(Intent(this, ProfileActivity::class.java))
143+
}
144+
else {
145+
//sign in fails, show error message
146+
}
147+
}
148+
}
149+
}
98150
{% endhighlight %}
99151

100152
Co więcej istnieje możliwość ręcznej konfiguracji zewnętrznych API autoryzacji dla m.in. Google, Twitter, Facebook, Github i integracji z kontem użytkownika Firebase. Ponadto Firebase Authentication oferuje także mechanizm uwierzytelniania kont anonimowych i ich konwersji do kont stałych oraż możliwość logowania się za pomocą linka w wiadomości email.
@@ -103,5 +155,92 @@ Co więcej istnieje możliwość ręcznej konfiguracji zewnętrznych API autoryz
103155
Firebase Authentication poza podstawowymi właściwościami użytkownika Firebase umożliwia także uzyskanie dostępu do informacji profilowych dostarczonych przez zewnętrznych usługodawców autoryzacji. Ponadto możliwe jest dodanie lub zmiana bieżących danych (informacje profilowe, email, hasło, itp), a także usuwanie konta oraz wysyłanie maili zmiany hasła, weryfikacji czy ponownej autoryzacji konta.
104156

105157
{% highlight kotlin %}
106-
//TODO code
107-
{% endhighlight %}
158+
class ProfileActivity : AppCompatActivity() {
159+
160+
override fun onCreate(savedInstanceState: Bundle?) {
161+
super.onCreate(savedInstanceState)
162+
setContentView(R.layout.activity_profile)
163+
164+
showUserInfo()
165+
showProfiles()
166+
167+
updateProfileButton.setOnClickListener { updateProfile() }
168+
changeEmailButton.setOnClickListener { changeEmail() }
169+
changePasswordButton.setOnClickListener { changePassword() }
170+
deleteAccountButton.setOnClickListener { deleteAccount() }
171+
}
172+
173+
private fun showUserInfo() {
174+
val user = FirebaseAuth.getInstance().currentUser
175+
user?.let {
176+
var info = String()
177+
info += "uid: ${user.uid}\n"
178+
info += "name: ${user.displayName}\n"
179+
info += "email: ${user.email}\n"
180+
info += "is verified: ${user.isEmailVerified}\n"
181+
info += "photoUrl: ${user.photoUrl.toString()}\n"
182+
//get more info
183+
userInfo.setText(info)
184+
}
185+
}
186+
187+
private fun showProfiles() {
188+
val user = FirebaseAuth.getInstance().currentUser
189+
user?.let {
190+
for(profile in it.providerData) {
191+
var info = String()
192+
info += "PROVIDER: ${profile.providerId}\n"
193+
info += "uid: ${profile.uid}\n"
194+
info += "name: ${profile.displayName}\n"
195+
//get more info
196+
userProfiles.setText(info)
197+
}
198+
}
199+
}
200+
201+
private fun updateProfile() {
202+
val user = FirebaseAuth.getInstance().currentUser
203+
val profileUpdates = UserProfileChangeRequest.Builder()
204+
.setDisplayName(editText.getText().toString())
205+
.setPhotoUri(Uri.parse("http://androidcode.pl/assets/img/logo.png"))
206+
.build()
207+
208+
user?.updateProfile(profileUpdates)?.addOnCompleteListener { task ->
209+
if (task.isSuccessful) {
210+
//show some message and change UI
211+
}
212+
}
213+
}
214+
215+
private fun changeEmail() {
216+
val user = FirebaseAuth.getInstance().currentUser
217+
val email = editText.getText().toString() //validate
218+
user?.updateEmail(email)?.addOnCompleteListener { task ->
219+
if (task.isSuccessful) {
220+
//show some message and change UI
221+
}
222+
}
223+
}
224+
225+
private fun changePassword() {
226+
val user = FirebaseAuth.getInstance().currentUser
227+
val newPassword = editText.getText().toString() //validate
228+
user?.updatePassword(newPassword)?.addOnCompleteListener { task ->
229+
if (task.isSuccessful) {
230+
//show some message and change UI
231+
}
232+
}
233+
}
234+
235+
private fun deleteAccount() {
236+
val user = FirebaseAuth.getInstance().currentUser
237+
user?.delete()?.addOnCompleteListener { task ->
238+
if (task.isSuccessful) {
239+
//show some message and change UI
240+
}
241+
}
242+
}
243+
244+
//more methods
245+
}
246+
{% endhighlight %}

0 commit comments

Comments
 (0)