javaandroidjwtjjwt

how should i use JWT in android?


I want to use jjwt to create a login activity. so i don't know how to use it . i read it in https://github.com/jwtk/jjwt but i couldn't understand because my English is not fluent. i want you to give me an example to understand it. should i send anything to service ? or just i should save key in my DatabaseHelper with sqlite openhelper . i use OkHttp for sending username and password to service and get it with json. this is my activity :

MainActivity :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }
    public void btnSignin_Clicked(View view){
        String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
        String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
        new SigninTask().execute(username,password);
    }
    public void btnSignup_Clicked(View view){
        ((EditText)findViewById(R.id.txtUsername)).setText("");
        ((EditText)findViewById(R.id.txtPassword)).setText("");
        String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
        String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
        new SignupTask().execute(username,password);
    }

    private class SignupTask extends AsyncTask<String,Object,String>{
        @Override
        protected String doInBackground(String... strings) {

            try {
                return
                new OkHttpClient()
                        .newCall(
                                new Request.Builder()
                                .url("http://localhost:8080/Service/signup?username="+strings[0]+"&password="+strings[1])
                                .build()
                        )
                        .execute()
                        .body()
                        .string();
            }catch (Exception e){
                return null;
            }

        }

        @Override
        protected void onPostExecute(String s) {
            SimpleDialog dialog=new SimpleDialog();
            dialog.setContext(MainActivity.this);
            dialog.setTitle("Error");
          try {
              JSONObject jsonObject=new JSONObject(s);
              Boolean result=jsonObject.getBoolean("value");
              if (result){
                  String username= ((EditText)findViewById(R.id.txtUsername)).getText().toString();
                  dialog.setMessage("Signup completed)");
                  ((EditText)findViewById(R.id.txtUsername)).setText("");
                  ((EditText)findViewById(R.id.txtPassword)).setText("");

              }else {
                  ((EditText)findViewById(R.id.txtUsername)).setText("");
                  ((EditText)findViewById(R.id.txtPassword)).setText("");
                  dialog.setMessage("Sign up failed !");
              }
          }catch (Exception e){
              dialog.setMessage("Error occured !");
          }
          dialog.show(getSupportFragmentManager(),"dialog");
        }
    }

    private class SigninTask extends AsyncTask<String,Object,String>{
        @Override
        protected String doInBackground(String... strings) {
            try {
              return   new OkHttpClient()
                        .newCall(
                                new Request.Builder()
                                        .url("http://localhost:8080/Service/authorize?username="+strings[0]+"&password="+strings[1])
                                        .build()
                        )
                        .execute()
                        .body()
                        .string();
            }catch (Exception e){
                return null;
            }
        }

        @Override
        protected void onPostExecute(String s) {
            SimpleDialog dialog=new SimpleDialog();
            dialog.setContext(MainActivity.this);
            dialog.setTitle("Error");
            try {
                JSONObject jsonObject=new JSONObject(s);
                Boolean result=jsonObject.getBoolean("value");
                if (result){
                    String username=((EditText)findViewById(R.id.txtUsername)).getText().toString();
                    String password=((EditText)findViewById(R.id.txtPassword)).getText().toString();
                    Intent intent=new Intent(MainActivity.this,CategoryActivity.class);
                    User user=new User();
                    user.setUsername(username);
                    user.setPassword(password);

                    intent.putExtra("user",user);
                    startActivity(intent);
                    finish();
                }else {
                    dialog.setMessage("Invalid username or password !");
                }
            }catch (Exception e){
                dialog.setMessage("Error occured !");
            }
            dialog.show(getSupportFragmentManager(),"dialog");
        }
    }
}

Solution

  • Normally you send your login data (user name, password) to the server that authenticates you, and this server sends back a JWT. You can store this token in your application. And then y' send this token within every request to the serve to authenticate you.