I have an android studio application that connects to a nodejs backend server with user authentication. I can log in and register from my app but it does not store a session. So I can not get session based functionality yet.
I need to add functionality to store a session. For this how do I do this with the retrofit interface. I want to log in start a session so I can have user logged in access to other routes on the server.
Or is there another interface for android studio that will allow for cookies and sessions? Retrofit interface
public interface RetrofitInterface {
@POST("/login")
Call<Login_result> executeLogin(@Body HashMap<String, String> map);
@POST("/signup")
Call<Void> executeSignup(@Body HashMap<String, String>map);
@POST("/add_data")
Call<Void> executeAdd_data(@Body HashMap<String, String>map);
@POST("/logout")
Call<Void> executeLogout(@Body HashMap<String, String>map);
@GET("/test")
Call<Void> executeTest();
}
**Main code**
```java
/*Updated this*/
Context context = this;
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(new OkhttpClient.builder()
.addInterceptor(new ReceivedCookiesInterceptor(context)
.addInterceptor(new AddCookiesInterceptor(context)
).build())
.addConverterFactory(GsonConverterFactory.create())
.build();
retrofitInterface = retrofit.create(RetrofitInterface.class);
Log in code
HashMap<String,String> map = new HashMap<>();
//map.put("email",emailEdit.getText().toString());//
map.put("username", usernameEdit.getText().toString());
map.put("password", passwordEdit.getText().toString());
Call<Login_result> call =
retrofitInterface.executeLogin(map);//Run the post
call.enqueue(new Callback<Login_result>()
{
@Override
public void onResponse(Call<Login_result> call, Response<Login_result> response) {
if(response.code() == 200)
{
/*Login_result result = response.body();
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setTitle(result.getUsernname());
builder1.setMessage(result.getEmail());
builder1.show();*/
Toast.makeText(MainActivity.this, "Logged in", Toast.LENGTH_SHORT).show();
}else if(response.code() == 404)
{
Toast.makeText(MainActivity.this, "Incorrect usernanme or password", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Login_result> call, Throwable t) {
Toast.makeText(MainActivity.this, t.getMessage(),Toast.LENGTH_LONG).show();
}
});