How to perform Authentication with Retrofit? — Developer’s Commute Newsletter

Hitesh Kohli
3 min readDec 11, 2023

--

Photo by Balázs Kétyi on Unsplash

In this newsletter, we will explore more about API authentication with Retrofit. I will try to break down the basic concepts so you can understand and apply them in your projects.

Let us get started.

— — —

Do you want these topics in your Email Inbox:

Interesting facts about apps
Expert Advice
Core Concepts
Developer Stories
Apps Reviews
Human error
Code Philosophy

Additional:
- Coding Memes
- Interesting Tweets & Articles
- Motivation

— — —

First, let us create a simple data class of Login Information. We will structure our login data with this data class and send it with the body of authentication.

We are using Dagger Hilt with Retrofit to perform Authentication. I hope you understand Dagger Hilt. If you do not understand Dagger Hilt here is a good introduction. So I have created a companion object which has the Base Url of the API or the endpoint required for Auth.

Then we have a simple Data class Injection. You don’t need it but it is interesting to know that data class can also be injected with dagger hilt.

After that, we created the Retrofit Instance which is used to access the API endpoint. I hope you can do the basic Retrofit API calls. In the Retrofit Instance, we are using a client called OkHttpClient. If you understand the basics of Retrofit then you will know that OkHttp can be used to customize the Retrofit calls further.

BuildApiResponse is just a POST request that can be used with the Retrofit to call the API.

In the okHttp function, we are using a Logging Interceptor which has the basic function of intercepting the API call and inserting the Bearer Token. It allows us to access the data present on the server.

If you want to know more about the Backend for Android Developers then stay tuned.

Here is the Post Request which is used to call the API endpoint and insert the Login body.

Here is the construction of the Interceptor which takes in the Tokentype and accessToken and passes them to the API Call.

Here is a simple Sealed class that will be used to see if the request was successful or not

This is a simple function that is used to see if the response becomes successful or not

You do not need to make complex functions to perform the API call just a sealed class to see if the call is successful or not would be fine as well.

I hope this code helped in API auth with Retrofit.

Thank you for Reading.

If I have written something wrong please forgive me.

Your Developer Commute’s Captain

— — —

Do you want these topics in your Email Inbox:

Interesting facts about apps
Expert Advice
Core Concepts
Developer Stories
Apps Reviews
Human error
Code Philosophy

Additional:
- Coding Memes
- Interesting Tweets & Articles
- Motivation

— — —

--

--

Hitesh Kohli
Hitesh Kohli

Written by Hitesh Kohli

Hi, my name is Hitesh Kohli, I work at Geeks for Geeks as an Android developer. I love messing around with apps and games.

No responses yet