Coder Social home page Coder Social logo

android_django_retrofit_register_user's Introduction

Djoser, Retrofit, Java

How to perform user registration with django, djoser, android, retrofit, gson, internet, same device.

How to integrate django authentication with android java retrofit

To integrate Django authentication with an Android app using Retrofit, you'll need to set up both the Django backend and the Android frontend to handle the authentication process. Here’s a step-by-step guide:

1. Set Up Django Backend for Authentication

a. Install Django and Django REST framework

pip install django djangorestframework djangorestframework-simplejwt

b. Configure Django Settings

Add the required apps to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'rest_framework_simplejwt',
    ...
]

Configure Django REST framework to use JWT authentication:

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_simplejwt.authentication.JWTAuthentication',
    ),
}

Add URL patterns for the JWT authentication views in urls.py:

from django.urls import path
from rest_framework_simplejwt.views import (
    TokenObtainPairView,
    TokenRefreshView,
)

urlpatterns = [
    ...
    path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
    path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
]

c. Create a View for Protected Resources

Create a simple view that requires authentication:

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

@api_view(['GET'])
@permission_classes([IsAuthenticated])
def protected_view(request):
    return Response({'message': 'This is a protected view!'})

Add this view to your urls.py:

urlpatterns += [
    path('api/protected/', protected_view, name='protected'),
]

2. Set Up Android App with Retrofit

a. Add Dependencies

In your build.gradle file, add Retrofit and OkHttp dependencies:

dependencies {
    implementation 'com.squareup.retrofit2:retrofit:2.9.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    implementation 'com.squareup.okhttp3:okhttp:4.9.1'
    implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
}

b. Create Retrofit Instance

Create a singleton class for Retrofit instance:

public class RetrofitClient {
    private static RetrofitClient instance = null;
    private static Retrofit retrofit;
    private static final String BASE_URL = "http://your-django-api-url/";

    private RetrofitClient() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public static synchronized RetrofitClient getInstance() {
        if (instance == null) {
            instance = new RetrofitClient();
        }
        return instance;
    }

    public Api getApi() {
        return retrofit.create(Api.class);
    }
}

c. Define API Interface

Create an interface for API endpoints:

public interface Api {
    @POST("api/token/")
    Call<TokenResponse> getToken(@Body TokenRequest tokenRequest);

    @GET("api/protected/")
    Call<ProtectedResponse> getProtectedResource(@Header("Authorization") String token);
}

Define TokenRequest, TokenResponse, and ProtectedResponse models:

public class TokenRequest {
    private String username;
    private String password;

    public TokenRequest(String username, String password) {
        this.username = username;
        this.password = password;
    }
}

public class TokenResponse {
    private String access;
    private String refresh;

    // getters and setters
}

public class ProtectedResponse {
    private String message;

    // getters and setters
}

d. Make API Calls

In your Activity or ViewModel, use Retrofit to make API calls:

RetrofitClient.getInstance().getApi().getToken(new TokenRequest("username", "password"))
        .enqueue(new Callback<TokenResponse>() {
            @Override
            public void onResponse(Call<TokenResponse> call, Response<TokenResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    String accessToken = "Bearer " + response.body().getAccess();

                    RetrofitClient.getInstance().getApi().getProtectedResource(accessToken)
                            .enqueue(new Callback<ProtectedResponse>() {
                                @Override
                                public void onResponse(Call<ProtectedResponse> call, Response<ProtectedResponse> response) {
                                    if (response.isSuccessful() && response.body() != null) {
                                        // Handle the protected resource response
                                    }
                                }

                                @Override
                                public void onFailure(Call<ProtectedResponse> call, Throwable t) {
                                    // Handle failure
                                }
                            });
                }
            }

            @Override
            public void onFailure(Call<TokenResponse> call, Throwable t) {
                // Handle failure
            }
        });

Summary

This setup allows your Android app to authenticate with a Django backend using JWT tokens. The user logs in, receives a JWT token, and uses it to access protected resources.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.