Fixed login issue where token was not being cleared before loggin in again
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -7,6 +7,7 @@ appcompat = "1.7.1"
|
||||
material = "1.13.0"
|
||||
activity = "1.12.4"
|
||||
constraintlayout = "2.2.1"
|
||||
swiperefreshlayout = "1.2.0"
|
||||
|
||||
[libraries]
|
||||
junit = { group = "junit", name = "junit", version.ref = "junit" }
|
||||
@@ -16,6 +17,7 @@ appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "a
|
||||
material = { group = "com.google.android.material", name = "material", version.ref = "material" }
|
||||
activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
|
||||
constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
|
||||
swiperefreshlayout = { group = "androidx.swiperefreshlayout", name = "swiperefreshlayout", version.ref = "swiperefreshlayout" }
|
||||
|
||||
[plugins]
|
||||
android-application = { id = "com.android.application", version.ref = "agp" }
|
||||
|
||||
Reference in New Issue
Block a user