Skip to content
Related Articles

Related Articles

Improve Article

How to Update User’s Profile After Signing Through GitHub using Firebase in Android?

  • Last Updated : 30 Jun, 2021

In this article, we will learn to update user’s profiles who signed in to the app through GitHub. The profile will be updated through the user’s GitHub information like GitHub Id, name, followers, following, etc. Let’s start!

Step 1: First set up your app so that users can be signed in through GitHub. So you need to authenticate the user using GitHub with the help of firebase. For this, follow all the steps in the article: Authenticate user using Github in android.

Step 2: Update Profile

For this, declare a variable say ‘user’ of type map<String, Any> in else part of signInWithGithubProvider() method as shown below, use additionalUserInfo for extracting user’s Github profile information.

private fun signInWithGithubProvider(){



. . .

if{    

. . .   }

else {

. . .

 val user: Map<String, Any> =  it.additionalUserInfo!!.profile as Map<String, Any>       // it refers to AuthResult!

. . . 

}           

}               

Using additionalUserInfo, all the required information will be sent from MainActivity to HomePageActivity (using Intent) which contains the user’s profile information. All the code remains the same as in this article find the difference and add those lines of code required for updating the user’s profile. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.OnFailureListener
import com.google.android.gms.tasks.OnSuccessListener
import com.google.android.gms.tasks.Task
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.OAuthProvider
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
  
class MainActivity : AppCompatActivity() {
  
    private lateinit var firebaseUser: FirebaseUser
    private lateinit var loginBtn: Button
    private lateinit var githubEdit: EditText
  
    // firebaseAuth variable to be initialized later
    private lateinit var auth: FirebaseAuth
  
    // an instance of an OAuthProvider using its
      // Builder with the provider ID github.com
    private var provider = OAuthProvider.newBuilder("github.com")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // Initialize Firebase Auth
        auth = Firebase.auth
  
        loginBtn = findViewById(R.id.github_login_btn)
        githubEdit = findViewById(R.id.githubId)
  
        // Target specific email with login hint.
        provider.addCustomParameter("login", githubEdit.text.toString())
  
        // Request read access to a user's email addresses.
        // This must be preconfigured in the app's API permissions.
        val scopes: ArrayList<String?> = object : ArrayList<String?>() {
            init {
                add("user:email")
            }
        }
        provider.scopes = scopes
  
        // call signInWithGithubProvider() method
          // after clicking login Button
        loginBtn.setOnClickListener {
            if (TextUtils.isEmpty(githubEdit.text.toString())) {
                Toast.makeText(this, "Enter email associated to github", Toast.LENGTH_LONG).show()
            } else {
                signInWithGithubProvider()
            }
        }
    }
  
    // To check if there is a pending 
      // result, call pendingAuthResult
    private fun signInWithGithubProvider() {
  
        // There's something already here! Finish the sign-in for your user.
        val pendingResultTask: Task<AuthResult>? = auth.pendingAuthResult
        if (pendingResultTask != null) {
            pendingResultTask
                    .addOnSuccessListener {
                        // User is signed in.
                        Toast.makeText(this, "User exist", Toast.LENGTH_LONG).show()
                    }
                    .addOnFailureListener {
                        // Handle failure.
                        Toast.makeText(this, "Error1 : $it", Toast.LENGTH_LONG).show()
                    }
        } else {
  
            auth.startActivityForSignInWithProvider(this, provider.build())
                    .addOnSuccessListener(
                            OnSuccessListener<AuthResult?> {
                                // User is signed in.
                                // retrieve the current user
                                firebaseUser = auth.currentUser!!
                                val user: Map<String, Any> =
                                        // contains additional user information as 
                                          // a result of a successful sign-in
                                        it.additionalUserInfo!!.profile as Map<String, Any>  //
                                // navigate to HomePageActivity after successful login
                                val intent = Intent(this, HomePageActivity::class.java)
                                // send github user name from MainActivity to HomePageActivity
                                intent.putExtra("githubUserName", firebaseUser.displayName)
                                intent.putExtra("twitterHandle", user["twitter_username"].toString())
                                intent.putExtra("githubBio", user["bio"].toString())
                                intent.putExtra("linkedInLink", user["blog"].toString())
                                intent.putExtra("id", user["login"].toString())
                                intent.putExtra("follower", user["followers"].toString())
                                intent.putExtra("following", user["following"].toString())
                                intent.putExtra("publicRepos", user["public_repos"].toString())
                                intent.putExtra("location", user["location"].toString())
                                intent.putExtra("profilePic", user["avatar_url"].toString())
                                this.startActivity(intent)
                                Toast.makeText(this, "Login Successfully", Toast.LENGTH_LONG).show()
  
                            })
                    .addOnFailureListener(
                            OnFailureListener {
                                // Handle failure.
                                Toast.makeText(this, "Error2 : $it", Toast.LENGTH_LONG).show()
                            })
        }
  
    }
}

Step 3: Create a new empty activity

Refer to this article How to Create New Activity in Android Studio and create a new activity. Name the activity as HomePageActivity. Navigate to the app > res > layout > activity_home_page.xml and add the below code to that file. Below is the code for the activity_home_page.xml file. 

XML




<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomePageActivity">
      
    <ImageView
        android:id="@+id/profilePic"
        android:layout_width="120dp"
        android:layout_height="100dp"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:contentDescription="profile image"
        android:scaleType="centerCrop"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:srcCompat="@tools:sample/avatars[3]" />
  
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginTop="20dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/profilePic">
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
  
            <TextView
                android:id="@+id/idHeader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="GitHub Id:"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/githubId"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="github id"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
  
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
              
            <TextView
                android:id="@+id/headerId"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="User Name :"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/id"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="user name"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
        </LinearLayout>
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
              
            <TextView
                android:id="@+id/followerHeader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Follower:"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/follower"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="no. of follower"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
              
            <TextView
                android:id="@+id/followingHeader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Following:"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/following"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="no. of following"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
  
  
            <TextView
                android:id="@+id/repoHeader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Public Repos :"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/repos"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="no. of repos"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
  
  
            <TextView
                android:id="@+id/twitter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Twitter:"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/twitterUserName"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="twitter handle"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
  
  
            <TextView
                android:id="@+id/bioHeader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="Bio:"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/bio"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="twitter handle"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
  
  
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:orientation="horizontal"
            android:weightSum="2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
  
  
            <TextView
                android:id="@+id/linkedIn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.5"
                android:text="LinkedIn:"
                android:textColor="#06590A"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
  
            <TextView
                android:id="@+id/linkedInLink"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1.5"
                android:hint="linkedIn link"
                android:textAlignment="center"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
  
    </LinearLayout>
  
    <Button
        android:id="@+id/logOut"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:gravity="center"
        android:text="Logout"
        android:textColor="#fff"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
  
</androidx.constraintlayout.widget.ConstraintLayout>

Go to the HomePageActivity.kt file and refer to the following code. Below is the code for the HomePageActivity.kt file. Comments are added inside the code to understand the code in more detail.

Kotlin




package com.anju.kumari.usergithubauth
  
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
  
class HomePageActivity : AppCompatActivity() {
  
    // declare variables to contain 
    // user's sent information
    // initialize them too
    private var userName = ""
    private var twitterHandle = " "
    private var bio = " "
    private var linkedIn = " "
    private var githubId = " "
    private var follower = " "
    private var following = " "
    private var repos = " "
    private var picUrl = " "
  
    private lateinit var githubUserName: TextView
    private lateinit var logoutBtn: Button
    private lateinit var handle: TextView
    private lateinit var githubBio: TextView
    private lateinit var linkedInLink: TextView
    private lateinit var userId: TextView
    private lateinit var totalFollower: TextView
    private lateinit var totalFollowing: TextView
    private lateinit var totalRepos: TextView
    private lateinit var userPic: ImageView
      
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home_page)
  
        githubUserName = findViewById(R.id.id)
        logoutBtn = findViewById(R.id.logOut)
        handle = findViewById(R.id.twitterUserName)
        githubBio = findViewById(R.id.bio)
        linkedInLink = findViewById(R.id.linkedInLink)
        userId = findViewById(R.id.githubId)
        totalFollower = findViewById(R.id.follower)
        totalFollowing = findViewById(R.id.following)
        totalRepos = findViewById(R.id.repos)
        userPic = findViewById(R.id.profilePic)
  
        // catching all data sent from MainActivity using Intent
        userName = intent.getStringExtra("githubUserName")!!
        twitterHandle = intent.getStringExtra("twitterHandle")!!
        bio = intent.getStringExtra("githubBio")!!
        linkedIn = intent.getStringExtra("linkedInLink")!!
        githubId = intent.getStringExtra("id")!!
        follower = intent.getStringExtra("follower")!!
        following = intent.getStringExtra("following")!!
        repos = intent.getStringExtra("publicRepos")!!
        picUrl = intent.getStringExtra("profilePic")!!
  
        // set all information to the widgets 
        // to display them on the screen
        githubUserName.text = userName
        handle.text = twitterHandle
        githubBio.text = bio
        linkedInLink.text = linkedIn
        userId.text = githubId
        totalFollower.text = follower
        totalFollowing.text = following
        totalRepos.text = repos
  
        // use glide to download the user's 
        // pic using url of the image
        Glide.with(this)
            .load(picUrl)
            .into(userPic)
  
        // logout button to signOut from the app
        logoutBtn.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            this.startActivity(intent)
        }
    }
}

Note: make sure to add a dependency for using Glide for loading the user’s pic.

dependencies{



. . .

implementation ‘com.github.bumptech.glide:glide:4.11.0’

annotationProcessor ‘com.github.bumptech.glide:compiler:4.11.0’

. . . 

}

Here is the output.

Output:

 Source Code on GitHub: https://github.com/Anju1415/Github-Auth

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :