How To login and registration using volley with in session manager in android studio?
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
public class SessionManager {
private static final String PREF_NAME = "session";
private static final int PRIVATE_MODE = 0;
private static final String KEY_USER_ID = "user_id";
private static final String KEY_USERNAME = "username";
private static final String KEY_EMAIL = "email";
private static final String KEY_TOKEN = "token";
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private Context context;
public SessionManager(Context context) {
this.context = context;
sharedPreferences = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = sharedPreferences.edit();
}
public void createLoginSession(int userId, String username, String email, String token) {
editor.putInt(KEY_USER_ID, userId);
editor.putString(KEY_USERNAME, username);
editor.putString(KEY_EMAIL, email);
editor.putString(KEY_TOKEN, token);
editor.apply();
}
public boolean isLoggedIn() {
return sharedPreferences.getString(KEY_TOKEN, null) != null;
}
public void logout() {
editor.clear();
editor.apply();
Intent intent = new Intent(context, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
public int getUserId() {
return sharedPreferences.getInt(KEY_USER_ID, -1);
}
public String getUsername() {
return sharedPreferences.getString(KEY_USERNAME, null);
}
public String getEmail() {
return sharedPreferences.getString(KEY_EMAIL, null);
}
public String getToken() {
return sharedPreferences.getString(KEY_TOKEN, null);
}
}
Next, let's refactor the LoginActivity
:
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
private Button loginButton;
private TextView registerTextView;
private SessionManager sessionManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sessionManager = new SessionManager(this);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
registerTextView = findViewById(R.id.registerTextView);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String username = usernameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();
if (!username.isEmpty() && !password.isEmpty()) {
login(username, password);
}
}
});
registerTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
startActivity(intent);
}
});
}
private void login(final String username, final String password) {
String url = "http://example.com/api/login";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
int userId = jsonObject.getInt("user_id");
String token = jsonObject.getString("token");
sessionManager.createLoginSession(userId, username, null, token);
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle error response
}
});
// Add the request to the RequestQueue
VolleySingleton.getInstance(this).addToRequestQueue(stringRequest);
}
}