# Authenticate a User

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.

{% tabs %}
{% tab title="Kotlin" %}
{% code title="MyApp.kt" %}

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

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code title="MyApp.java" %}

```java
UserAuth userAuth = new UserAuth(
    "8ed92f3c-0696-4513-a842-085e3cee589e",
    "eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJtaXR0ZXItaW8iLCJ1c2VyVG9rZW5JZCI6ImJtRlI5bWNQaDhkQnJHaWIiLCJ1c2VydG9rZW4iOiJiM2dvY2ZhZ3ZyNWlrNHJkbXJlc29wNnNlcyJ9.dlE1QOYmUJpqoh1kORm3hEI3KbBM0v8kKZQnQQwXR6TZuFiCaDQrJMlp-2dgNP1CTYCPMFYoqGctRWQ5JyNiOQ"
);
```

{% endcode %}
{% endtab %}
{% endtabs %}

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:

{% tabs %}
{% tab title="Kotlin" %}
{% code title="MyApp.kt" %}

```kotlin
mitter = Mitter(
    context = this,
    mitterConfig = mitterConfig,
    userAuth = userAuth
)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code title="MyApp.java" %}

```java
mitter = new Mitter(
    this,
    mitterConfig,
    userAuth
);
```

{% endcode %}
{% endtab %}
{% endtabs %}

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.

{% tabs %}
{% tab title="Kotlin" %}
{% code title="MainActivity.kt" %}

```kotlin
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
    }
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code title="MainActivity.java" %}

```java
public class MainActivity extends AppCompatActivity {
    private Mitter mitter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mitter = ((MyApp) getApplication()).mitter;
    }
}
```

{% endcode %}
{% endtab %}
{% endtabs %}

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:

{% tabs %}
{% tab title="Kotlin" %}
{% code title="MainActivity.kt" %}

```kotlin
val users = mitter.Users()
val channels = mitter.Channels()
val messaging = mitter.Messaging()
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code title="MainActivity.java" %}

```java
Mitter.Users users = mitter.new Users();
Mitter.Channels channels = mitter.new Channels();
Mitter.Messaging messaging = mitter.new Messaging();
```

{% endcode %}
{% endtab %}
{% endtabs %}

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.

{% tabs %}
{% tab title="Kotlin" %}
{% code title="MainActivity.kt" %}

```kotlin
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}")
        }
    }
)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code title="MainActivity.java" %}

```java
users.getCurrentUser(new Mitter.OnValueAvailableCallback<User>() {
    @Override
    public void onValueAvailable(User user) {
        Log.d("MSA", "User is: "+ user.getScreenName().getScreenName());
    }

    @Override
    public void onError(ApiError apiError) {
        Log.d("MSA", "Error while fetching user: " + apiError);
    }
});
```

{% endcode %}
{% endtab %}
{% endtabs %}

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

```
D/MSA: User is: Jason
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.mitter.io/getting-started/build-your-first-android-app/authenticate-a-user.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
