- Developer's Commute
- Posts
- How to perform Authentication with Retrofit? - Developer Commute
How to perform Authentication with Retrofit? - Developer Commute
Today we will explore performing API auth with Retrofit. Also, I have some interesting tweets, articles, memes and in the end, Coding Motivation
Good Morning, It’s your captain speaking. Welcome aboard the Developer’s Commute. Today we will explore performing API auth with Retrofit. Also, I have some interesting tweets, articles, memes and in the end, Coding Motivation to keep you moving We hope that you enjoy this ride with us and you have a pleasant journey ahead.
Code Philosophy
How to perform Authentication with Retrofit?
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.
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
Free Ebook
Developer Meme
Tweets That You Might Love
Exploring Interesting Tweets
Tweets That You Will Find Interesting
Do you want to learn about JWT Authentication?
Here is an interesting tweet for you →
Android developer aye?
Have you heard about JWT token?
If yes, you must have used it in Authentication of your app.
If no, we are going to explore it in this series of thread.
Here is everything:
(1/)
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
4:06 AM • Nov 21, 2023
Netflix Tech Stack and CI CD Pipeline
Then you might find this tweet interesting→
Netflix Tech Stack - CI/CD Pipeline
This post is based on research from many Netflix engineering blogs and open-source projects. If you come across any inaccuracies, please feel free to inform us.
Planing: Netflix Engineering uses JIRA for planning and Confluence for… twitter.com/i/web/status/1…— Bytebytego (@bytebytego)
6:55 AM • Nov 23, 2023
Do you want to learn about Backend development as an Android developer?
Here is something interesting for you→
Android Developer aye?
If you are an app developer, you should not only understand app development but also backend dev.
Why?
If you have no idea how the basics of the backend work.
It is hard to write code for the frontend
Here is everything you want to know:
(1/20)
— Hitesh Kohli | Android Developer (@Hitesh__kohli)
12:33 PM • Nov 18, 2023
ArticlesThat You Might Love
Exploring Interesting Articles
Articles That You Will Find Interesting
Foundation of Kotlin Flows- SharedFlow vs Stateflow
Here is an interesting article that you might want to check out →
Best Architecture In Kotlin Multiplatform
Here is an interesting article for you →
Why do you think users like apps over websites?
Here is an interesting article for you →