Fixed login issue where token was not being cleared before loggin in again

This commit is contained in:
Alex
2026-03-13 22:14:19 -06:00
parent 4a1f3f2284
commit cf0329e0a5
5 changed files with 15 additions and 13 deletions

View File

@@ -52,6 +52,7 @@ dependencies {
implementation("androidx.camera:camera-lifecycle:1.4.0")
implementation("androidx.camera:camera-view:1.4.0")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")
implementation(libs.swiperefreshlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)

View File

@@ -53,6 +53,9 @@ public class MainActivity extends AppCompatActivity {
//clear login status
tvLoginStatus.setText("");
// Clear old login data before logging in
TokenManager.getInstance(this).clearLoginData();
//Set click listener for login button
btnLogin.setOnClickListener(v -> {
//Get user name and password from text fields
@@ -85,19 +88,8 @@ public class MainActivity extends AppCompatActivity {
Toast.makeText(MainActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
finish();
} else {
// Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
// tvLoginStatus.setText("Login failed");
try {
String errorBody = response.errorBody().string();
Log.e("LOGIN", "Code: " + response.code() + " Error: " + errorBody);
} catch (Exception e) {
Log.e("LOGIN", "Code: " + response.code());
}
Toast.makeText(MainActivity.this,
"Login failed: " + response.code(),
Toast.LENGTH_SHORT).show();
tvLoginStatus.setText("Login failed: " + response.code());
Toast.makeText(MainActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
tvLoginStatus.setText("Login failed");
}
}

View File

@@ -23,6 +23,11 @@ public class AuthInterceptor implements Interceptor {
@Override
public Response intercept(@NonNull Chain chain) throws IOException {
String token = tokenManager.getToken();
String url = chain.request().url().toString();
if (url.contains("auth/login") || url.contains("auth/register")) {
return chain.proceed(chain.request());
}
//If we have a token then add it to the request
if (token != null) {

View File

@@ -26,6 +26,7 @@ import android.widget.TextView;
import com.example.petstoremobile.R;
import com.example.petstoremobile.activities.MainActivity;
import com.example.petstoremobile.api.Auth.TokenManager;
import java.io.File;
@@ -229,6 +230,7 @@ public class ProfileFragment extends Fragment {
//Logout button
btnLogout.setOnClickListener(v -> {
TokenManager.getInstance(requireContext()).clearLoginData(); // clear the token for next login
//get the intent to the main activity and clear the back stack so the back button won't allow the user to go back to the previous screen
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);