Search code examples
javaandroidjwttokeninterceptor

how to use interceptor for jwt token


here is my code and my log said D/test: user not exist

how to send the jwt token ??

public class ApiClient implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {

        Log.d("test",AccessTokenSharedPreferences.getAccessToken("1"));

        Request request = chain.request()
                .newBuilder()
                .url(BasicInfo.baseUrl)
                .addHeader("X-AUTH-ACCESS-TOKEN", AccessTokenSharedPreferences.getAccessToken("1"))
                .addHeader("X-AUTH-REFRESH-TOKEN", AccessTokenSharedPreferences.getRefreshToken("1"))
                .build();
        Response response = chain.proceed(request);
        return response;
    }

}

thank you


Solution

  • I had the same question, but I didn't find a correct and convenient answer for myself. However, I think I have a solution for you: You can use interfaces - listeners before doing any requests to your server. That is, to receive a token every time you try to send any authorized request and work already from the sent token. For example, you want to send a get request to your server with authorization(JWT-bearer token in my case). At first, i declared a new interface AuthentificationContract:

    public interface AuthentificationContract {
    interface Process{
        void Auth();
        void Auth(String login, String password);
    }
    interface Listener{
        void AuthSuccess(String token);
        void AuthError(String message);
    }}
    

    Where Process is an interface implemented by the authentication class, where you send a request to the server to receive a JWT token, and Listener is a listener that will trigger the main target class, depending on the result of receiving the token. In the Authentication class, you implement the Process interface and implement the Auth method to get the token.

    public class Authentification implements AuthentificationContract.Process{
    private AuthentificationContract.Listener listener;
    public Authentification(AuthentificationContract.Listener _listener){
      this.listener = _listener;
     }
    @Override
    public void Auth(){
    String token = //your request to get a token
    //when your token arrived:
    listener.AuthSuccess(token);
    //else
    listener.AuthError("ERROR");
    }
    }
    

    IMPORTANT!!: here in the constructor you must pass the object that implemented the Listener interface to trigger our target class (or view). In the View or Targer class you should implement the interface Listener:

    public class StartAcitivity extends AppCompatActivity implements AuthentificationContract.Listener{
    
    private Authentification auth;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start_acitivity);
    
        auth = new Authentification(this);
    
        auth.Auth();
    
    }
    
    @Override
    public void AuthSuccess(String token) {
        //your token is here, you can do request with this token, just add it like one of headers
    }
    
    @Override
    public void AuthError(String message) {
        Log.d("ERROR", message);
    }
    }
    

    of course, this is just an example and it is not satisfactory to perform some actions in the view, it is better to use the MVP pattern for this