Authenticate a User

Add some users to your app and authenticate via the SDK.

Before we can authenticate a user from our app, we need to create one. You can create a user in either of these 2 ways:

  • Use the dev panel (the "Users" tab when you select an application) in the Mitter.io Dashboard

  • Make an API call from your backend server

In this example, we’ll be creating a user from the Mitter.io Dashboard using the dev panel.

Create users from the dashboard

Head over to the Mitter.io Dashboard and do the following:

  • Select your application from the list

  • In the left sidebar, click on the Users icon, and then click on New User

  • Give a name for your user and create one

Since we’ll be creating a group chat channel in the upcoming steps, go ahead and create 2 or 3 more users with different names.

Now, select one of the users you’ve created and click on Add Token. This will create an access token for the user. Copy the access token and keep it handy.

Follow the same process for creating a channel. Just choose the channel type as Group Chat and add all the users that you created in the previous step. Once your channel is created, you’ll see a channel ID. Copy it and keep it handy as well.

Add a user to your app

Go back to your Android project and navigate to your custom Application class.

First, you need to define a UserAuth object with the user details you acquired in the previous step.

MyApp.kt
val userAuth = UserAuth(
    userId = "8ed92f3c-0696-4513-a842-085e3cee589e",
    userAuthToken = "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJtaXR0ZXItaW8iLCJ1c2VyVG9rZW5JZCI6ImJtRlI5bWNQaDhkQnJHaWIiLCJ1c2VydG9rZW4iOiJiM2dvY2ZhZ3ZyNWlrNHJkbXJlc29wNnNlcyJ9.dlE1QOYmUJpqoh1kORm3hEI3KbBM0v8kKZQnQQwXR6TZuFiCaDQrJMlp-2dgNP1CTYCPMFYoqGctRWQ5JyNiOQ"
)

You can get the user ID from the list of users in the Users tab. Make sure that the ID and auth token both belong to the same user.

Now, just pass this UserAuth object to your previously configured Mitter object inside the onCreate() method in your MyApp class, like this:

MyApp.kt
mitter = Mitter(
    context = this,
    mitterConfig = mitterConfig,
    userAuth = userAuth
)

Your Android app is now fully configured to make calls to the Mitter.io Platform. Let’s test that out by fetching some basic information for the currently logged-in user.

Fetch currently logged-in user details

Head over to your MainActivity and get a reference to the Mitter object inside the onCreate() method.

MainActivity.kt
class MainActivity : AppCompatActivity() {

    private lateinit var mitter: Mitter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mitter = (application as MyApp).mitter
    }
}

Now, before making any calls, you need to get access to the basic group objects:

  • Users

  • Channels

  • Messaging

Inside your onCreate(), define 3 objects like this:

MainActivity.kt
val users = mitter.Users()
val channels = mitter.Channels()
val messaging = mitter.Messaging()

You’re now all set to make calls. Let’s test out our setup by making a call to fetch the currently logged-in user details.

You need to call the getCurrentUser() method on the Users object that you defined just now.

MainActivity.kt
users.getCurrentUser(
    onValueAvailableCallback = object : Mitter.OnValueAvailableCallback<User> {
        override fun onError(apiError: ApiError) {
            Log.d("MSA", "Error while fetching user: $apiError")
        }

        override fun onValueAvailable(value: User) {
            Log.d("MSA", "User is: ${value.screenName.screenName}")
        }
    }
)

If you’ve set everything up correctly, you should see something similar to this in your app’s logs:

D/MSA: User is: Jason

Last updated