Skip to content

Commit 8410cb2

Browse files
committed
authentication code added
1 parent 890e0ea commit 8410cb2

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class FirebaseAuthActivity : AppCompatActivity() {
2+
3+
private lateinit var auth: FirebaseAuth
4+
5+
override fun onCreate(savedInstanceState: Bundle?) {
6+
super.onCreate(savedInstanceState)
7+
setContentView(R.layout.activity_firebase_auth)
8+
auth = FirebaseAuth.getInstance()
9+
10+
signUpButton.setOnClickListener { updateProfile() }
11+
signInButton.setOnClickListener { changeEmail() }
12+
}
13+
14+
override fun onStart() {
15+
super.onStart()
16+
if(auth.currentUser != null) {
17+
//user is signed in, update UI or navigate
18+
}
19+
}
20+
21+
private fun signUp() {
22+
//validate fields before
23+
val email = emailEditText.getText().toString()
24+
val password = passwordEditText.getText().toString()
25+
auth.createUserWithEmailAndPassword(email, password).addOnCompleteListener { task ->
26+
if (task.isSuccessful) {
27+
//sign up success, update UI or navigate
28+
val user = auth.currentUser
29+
startActivity(Intent(this, ProfileActivity::class.java))
30+
}
31+
else {
32+
//sign up fails, show error message
33+
}
34+
}
35+
}
36+
37+
private fun signIn() {
38+
//validate fields before
39+
val email = emailEditText.getText().toString()
40+
val password = passwordEditText.getText().toString()
41+
auth.signInWithEmailAndPassword(email, password).addOnCompleteListener { task ->
42+
if (task.isSuccessful) {
43+
//sign ip success, update UI or navigate
44+
val user = auth.currentUser
45+
startActivity(Intent(this, ProfileActivity::class.java))
46+
}
47+
else {
48+
//sign in fails, show error message
49+
}
50+
}
51+
}
52+
}

authentication/FirebaseUIActivity.kt

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
class FirebaseUIActivity : AppCompatActivity() {
2+
3+
companion object {
4+
const val SIGN_IN = 100
5+
}
6+
7+
override fun onCreate(savedInstanceState: Bundle?) {
8+
super.onCreate(savedInstanceState)
9+
setContentView(R.layout.activity_firebase_ui)
10+
11+
//check if user is already signed in
12+
//if not then show sign in screen
13+
14+
//add available providers
15+
val providers = arrayListOf(
16+
AuthUI.IdpConfig.EmailBuilder().build(),
17+
AuthUI.IdpConfig.PhoneBuilder().build(),
18+
AuthUI.IdpConfig.GoogleBuilder().build())
19+
//add more like Twitter, Facebook etc
20+
21+
//start sign in screen and optional customize by logo, theme and urls
22+
val intent = AuthUI.getInstance()
23+
.createSignInIntentBuilder()
24+
.setAvailableProviders(providers)
25+
.setLogo(R.drawable.logo)
26+
.setTheme(R.style.AppTheme)
27+
.setTosAndPrivacyPolicyUrls("http://androidcode.pl/terms", "http://androidcode.pl/privacy")
28+
.build()
29+
startActivityForResult(intent, SIGN_IN)
30+
}
31+
32+
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
33+
super.onActivityResult(requestCode, resultCode, data)
34+
if (requestCode == SIGN_IN) {
35+
val response = IdpResponse.fromResultIntent(data)
36+
if (resultCode == Activity.RESULT_OK) {
37+
//success, get user and do some work
38+
val user = FirebaseAuth.getInstance().currentUser
39+
startActivity(Intent(this, UserActivity::class.java))
40+
}
41+
else {
42+
//fail, show some error message
43+
}
44+
}
45+
}
46+
}

authentication/ProfileActivity.kt

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
class ProfileActivity : AppCompatActivity() {
2+
3+
override fun onCreate(savedInstanceState: Bundle?) {
4+
super.onCreate(savedInstanceState)
5+
setContentView(R.layout.activity_profile)
6+
7+
showUserInfo()
8+
showProfiles()
9+
10+
updateProfileButton.setOnClickListener { updateProfile() }
11+
changeEmailButton.setOnClickListener { changeEmail() }
12+
changePasswordButton.setOnClickListener { changePassword() }
13+
deleteAccountButton.setOnClickListener { deleteAccount() }
14+
signOutButton.setOnClickListener { signOut() }
15+
}
16+
17+
private fun showUserInfo() {
18+
val user = FirebaseAuth.getInstance().currentUser
19+
user?.let {
20+
var info = String()
21+
info += "uid: ${user.uid}\n"
22+
info += "name: ${user.displayName}\n"
23+
info += "email: ${user.email}\n"
24+
info += "is verified: ${user.isEmailVerified}\n"
25+
info += "photoUrl: ${user.photoUrl.toString()}\n"
26+
//get more info
27+
userInfo.setText(info)
28+
}
29+
}
30+
31+
private fun showProfiles() {
32+
val user = FirebaseAuth.getInstance().currentUser
33+
user?.let {
34+
for(profile in it.providerData) {
35+
var info = String()
36+
info += "PROVIDER: ${profile.providerId}\n"
37+
info += "uid: ${profile.uid}\n"
38+
info += "name: ${profile.displayName}\n"
39+
//get more info
40+
userProfiles.setText(info)
41+
}
42+
}
43+
}
44+
45+
private fun updateProfile() {
46+
val user = FirebaseAuth.getInstance().currentUser
47+
val profileUpdates = UserProfileChangeRequest.Builder()
48+
.setDisplayName(editText.getText().toString())
49+
.setPhotoUri(Uri.parse("http://androidcode.pl/assets/img/logo.png"))
50+
.build()
51+
52+
user?.updateProfile(profileUpdates)?.addOnCompleteListener { task ->
53+
if (task.isSuccessful) {
54+
//show some message and change UI
55+
}
56+
}
57+
}
58+
59+
private fun changeEmail() {
60+
val user = FirebaseAuth.getInstance().currentUser
61+
val email = editText.getText().toString() //validate
62+
user?.updateEmail(email)?.addOnCompleteListener { task ->
63+
if (task.isSuccessful) {
64+
//show some message and change UI
65+
}
66+
}
67+
}
68+
69+
private fun changePassword() {
70+
val user = FirebaseAuth.getInstance().currentUser
71+
val newPassword = editText.getText().toString() //validate
72+
user?.updatePassword(newPassword)?.addOnCompleteListener { task ->
73+
if (task.isSuccessful) {
74+
//show some message and change UI
75+
}
76+
}
77+
}
78+
79+
private fun deleteAccount() {
80+
val user = FirebaseAuth.getInstance().currentUser
81+
user?.delete()?.addOnCompleteListener { task ->
82+
if (task.isSuccessful) {
83+
//show some message and change UI
84+
}
85+
}
86+
}
87+
88+
private fun signOut() {
89+
FirebaseAuth.getInstance().signOut()
90+
finish()
91+
}
92+
93+
//more methods
94+
}

authentication/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# authentication
2+
This is repository of http://androidcode.pl blog. It shows uses Authentication in Android. It is a part of Firebase - Authentication post in the blog.

authentication/UserActivity.kt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class UserActivity : AppCompatActivity() {
2+
3+
override fun onCreate(savedInstanceState: Bundle?) {
4+
super.onCreate(savedInstanceState)
5+
setContentView(R.layout.activity_user)
6+
7+
signOutButton.setOnClickListener {
8+
AuthUI.getInstance().signOut(this)
9+
.addOnCompleteListener {
10+
//do something like navigate to home screen
11+
}
12+
}
13+
14+
deleteAccountButton.setOnClickListener {
15+
AuthUI.getInstance().delete(this)
16+
.addOnCompleteListener {
17+
//do something like navigate to home screen
18+
}
19+
}
20+
}
21+
22+
//more methods
23+
}

0 commit comments

Comments
 (0)