Daftar dan Belajar Sekarang!

Get data id dan username dari jwt

foto indriyantongrh
1 Kontribusi 0 Coto
2 bulan yang lalu
Studikasus saya ingin mengambil data id dan username. waktu login sukses dan saya test pake Toast yang muncul respon resul (kode hasil result). dan saya coba decode di jwt.io hasil nya muncul ada id, member dll (data yang ingin saya ambil). disini saya stuck bagaimana saya bisa mengambil data id , username nya dari hasil encode tersebut. JWT menggunakan Library O2System Response
{
    "status": 200,
    "reason": "OK",
    "success": true,
    "message": null,
    "result": "eyJ0eXAiOiJKV1QiLCJhbGciOiJITUFDLVNIQTI1NiJ9.eyJpZCI6IjQ5OSIsImVtYWlsIjoiYmlzbWlsbGFoYmlzYUBleGFtcGxlLmNvbSIsIm1zaXNkbiI6IjA3OTc5Nzg0NjQ5NCIsInVzZXJuYW1lIjoiYmlzbWlsbGFoYmlzYSIsInZlcmlmaWVkTWVtYmVyIjpudWxsLCJwcm9maWxlIjp7ImlkIjoiMzE2IiwiaWRfZ2VvZGlyZWN0b3J5IjpudWxsLCJmdWxsbmFtZSI6ImJpc21pbGxhaGJpc2EiLCJudW1iZXIiOiIyNzQyNDciLCJpbWFnZSI6Imh0dHBzOlwvXC9kZW1vLmtyZWRpdGltcGlhbi5jb21cL3N0b3JhZ2VcL2ltYWdlc1wvZGVmYXVsdFwvYXZhdGFyLmpwZyIsInJlY29yZCI6eyJzdGF0dXMiOiJQVUJMSVNIIiwiY3JlYXRlIjp7InVzZXIiOm51bGwsInRpbWVzdGFtcCI6eyJkYXRlIjoiMjAxOS0xMi0wMyAxNTowODozMi4wMDAwMDAiLCJ0aW1lem9uZV90eXBlIjozLCJ0aW1lem9uZSI6IkFzaWFcL0pha2FydGEifX0sInVwZGF0ZSI6eyJ1c2VyIjpudWxsLCJ0aW1lc3RhbXAiOm51bGx9fSwibWV0YWRhdGEiOnsidXNlcm5hbWUiOiJiaXNtaWxsYWhiaXNhIiwiZW1haWwiOiJiaXNtaWxsYWhiaXNhQGV4YW1wbGUuY29tIiwicGhvbmUiOiIwNzk3OTc4NDY0OTQifSwic3RhdGlzdGljIjpudWxsfX0.zIEhdU5MyNjReG_9_661FWf0_R5eZuJweyl0JNFd7X0"
}
BaseApi.java
@FormUrlEncoded
    @POST("system/users/authenticate")
    Call<ResponseLoginSucces> login_member(@Field("username") String username,
                                           @Field("password") String password);
Login.java
public void LoginUser() {
        //membuat progress dialog
        pDialog = new ProgressDialog(this);
        pDialog.setCancelable(false);
        pDialog.setMessage("Tunggu proses login ...");
        pDialog.show();

        //mengambil data dari edittext

        final String username = txtusername.getText().toString().trim();
        final String password = txtpassword.getText().toString().trim();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(50, TimeUnit.SECONDS)
                .readTimeout(50, TimeUnit.SECONDS).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BuildConfig.BASE_URL).client(client)
                .addConverterFactory(GsonConverterFactory.create(new Gson())).build();

        RequestInterface api = retrofit.create(RequestInterface.class);
        Call<ResponseLoginSucces> call = api.login_member(username,  password);

        call.enqueue(new Callback<ResponseLoginSucces>() {
            @Override
            public void onResponse(Call<ResponseLoginSucces> call, Response<ResponseLoginSucces> response) {

                if(response.isSuccessful()){
                    pDialog.dismiss();


                    if(response.body().getResult() != null){

                        // Jika login berhasil
                        String id = response.body().getResult().toString();
                        String email = response.body().getResult().toString();
                        String username = response.body().getResult().toString();
                        String msisdn = response.body().getResult().toString();
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_ID, id);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_EMAIL, email);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_USERNAME, username);
                        sharedPrefManager.saveSPString(SharedPrefManager.SP_MSISDN, msisdn);

                        Toast.makeText(getApplicationContext(), "Berhasil Login" +email, Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(LoginUser.this, MenuUtama.class);
                        sharedPrefManager.saveSPBoolean(SharedPrefManager.SP_SUDAH_LOGIN, true);
                        startActivity(intent);
                        finish();



                    } else {
                        Toast.makeText(LoginUser.this, "The username or password is incorrect", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(LoginUser.this, "Error! Please try again!", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseLoginSucces> call, Throwable t) {
                t.printStackTrace();
                pDialog.dismiss();
                Toast.makeText(LoginUser.this, "Koneksi internet terputus.", Toast.LENGTH_SHORT).show();
            }
        });



    }
Sharedpref.java
public class SharedPrefManager {

    public static final String SP_Example = "example_v2_demo";

    public static final String SP_ID = "id";
    public static final String SP_EMAIL = "email";
    public static final String SP_USERNAME = "username";
    public static final String SP_MSISDN ="msisdn";

    public static final String SP_SUDAH_LOGIN = "spSudahLogin";

    SharedPreferences sp;
    SharedPreferences.Editor spEditor;

    public SharedPrefManager(Context context){
        sp = context.getSharedPreferences(SP_Example, Context.MODE_PRIVATE);
        spEditor = sp.edit();
    }

    public void saveSPString(String keySP, String value){
        spEditor.putString(keySP, value);
        spEditor.commit();
    }

    public void saveSPInt(String keySP, int value){
        spEditor.putInt(keySP, value);
        spEditor.commit();
    }

    public void saveSPBoolean(String keySP, boolean value){
        spEditor.putBoolean(keySP, value);
        spEditor.commit();
    }

    public String getSPID(){
        return sp.getString(SP_ID, "");
    }

    public String getSPEmail(){
        return sp.getString(SP_EMAIL, "");
    }

    public String getSpUsername(){
        return sp.getString(SP_USERNAME, "");
    }

    public String getSpMsisdn(){
        return sp.getString(SP_MSISDN, "");
    }

    public Boolean getSPSudahLogin(){
        return sp.getBoolean(SP_SUDAH_LOGIN, false);
    }
}
  • bisa tulis informasinya di atas kamu pakai library atau plugin apa? untuk membuat jwt nya - hilmanski
  • Library yang di backend nya ya ? @hilmanski - indriyantongrh
  • php o2system katanya kak @hilmanski - indriyantongrh


Login untuk diksusi di forum sekolah koding