byteC0de
byteC0de

Reputation: 5273

How to add access token to headers in MVP Pattern

I am using MVP Pattern for my android app, I need to add access token to my request headers. The access token is saved in the SharedPreferences. How to access that SharedPreferences in MVP Pattern. I am using Retrofit for network request.

public class RetrofitInstance {

    private static Retrofit retrofit;
    private static final String BASE_URL = "http://123124.ngrok.io/api/";


    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {

            OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
            okhttpBuilder.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {

                    Request request = chain.request();
                    Request.Builder newRequest = request.newBuilder().addHeader("Authorization", "Bearer "); //need to add value from SharedPreferences

                    return chain.proceed(newRequest.build());
                }
            });

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

}

Upvotes: -1

Views: 429

Answers (2)

Manohar
Manohar

Reputation: 23404

You can use Application Context

Try this way

  public class RetrofitInstance {

    private static Retrofit retrofit;
    private Context context;
    private static final String BASE_URL = "http://123124.ngrok.io/api/";

     public void init(Context context) {
          this.context = context;
     }

    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {

            OkHttpClient.Builder okhttpBuilder = new OkHttpClient.Builder();
            okhttpBuilder.addInterceptor(new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {

                    Request request = chain.request();
                    //use context
                    Request.Builder newRequest = request.newBuilder().addHeader("Authorization", "Bearer "); //need to add value from SharedPreferences

                    return chain.proceed(newRequest.build());
                }
            });

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

}

and in Application class

public class YourApplication extends Application {

 @Override
    public void onCreate() {
        super.onCreate();
         RetrofitInstance.getRetrofitInstance().init(getApplicationContext());
         }
   }

Upvotes: 1

Nabil
Nabil

Reputation: 672

Try:

private static Retrofit authRetrofit = null;        

public static Retrofit getAuthClient(Context context) {
                    if (authRetrofit == null) {
                        final AuthSharedPref authSharedPref = new AuthSharedPref(context);
                        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
                        httpClient.addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(@NonNull Chain chain) throws IOException {
                                Request request = chain.request().newBuilder().addHeader("Authorization", "Bearer "+authSharedPref.getToken()).build();
                                return chain.proceed(request);
                            }
                        });

                      authRetrofit= new retrofit2.Retrofit.Builder()
                     .baseUrl(BASE_URL)
                     .client(okhttpBuilder.build())
                     .addConverterFactory(GsonConverterFactory.create())
                     .build();
                    }
                    return authRetrofit;
}

Here, AuthSharedPref is a Shared Preferences class to store login details you can change it to your own.

Upvotes: 1

Related Questions