diff --git a/app/src/main/java/com/example/nerbos/service/Authentication.kt b/app/src/main/java/com/example/nerbos/service/Authentication.kt index 5dd7f05369dc0c7d8ff4630ab5a4ae1046b009c2..2e691dd88d10f86095472bdae4e1d6295e44dfaf 100644 --- a/app/src/main/java/com/example/nerbos/service/Authentication.kt +++ b/app/src/main/java/com/example/nerbos/service/Authentication.kt @@ -1,8 +1,11 @@ package com.example.nerbos.service +import android.annotation.SuppressLint import android.content.Context import android.content.Intent import android.content.SharedPreferences +import android.net.ConnectivityManager +import android.net.NetworkCapabilities import android.security.keystore.KeyGenParameterSpec import android.security.keystore.KeyProperties import com.android.volley.Request @@ -60,6 +63,12 @@ class Authentication(private val context: Context) { // Service: API Authentication for login and token checking fun login(email: String, password: String, callback: AuthCallback) { + // check network availability + if (!isNetworkAvailable()) { + callback.onError(context.getString(R.string.no_internet)) + return + } + // create JSON object val jsonObject = JSONObject() jsonObject.put(context.getString(R.string.email), email) @@ -82,7 +91,12 @@ class Authentication(private val context: Context) { }, { error -> // handle error - callback.onError(error.message ?: context.getString(R.string.wrong_credentials)) + removeTokenEmail() + if (error.networkResponse != null && (error.networkResponse.statusCode == 400 || error.networkResponse.statusCode == 401)) { + callback.onError(context.getString(R.string.wrong_credentials)) + } else { + callback.onError(context.getString(R.string.server_error)) + } } ) @@ -138,6 +152,11 @@ class Authentication(private val context: Context) { fun checkToken(callback: AuthCallback) { + // check network availability + if (!isNetworkAvailable()) { + callback.onError(context.getString(R.string.no_internet)) + return + } checkKeyPair() val token = getToken() if (token.isNotEmpty()) { @@ -167,14 +186,18 @@ class Authentication(private val context: Context) { } } - fun logout() { - // remove token and email + private fun removeTokenEmail() { val sharedPreferences : SharedPreferences = context.getSharedPreferences(context.getString(R.string.preferences), Context.MODE_PRIVATE) val editor = sharedPreferences.edit() editor.remove(context.getString(R.string.token)) editor.remove(context.getString(R.string.email)) editor.apply() + } + + fun logout() { + // remove token and email + removeTokenEmail() // Go to login activity val intent = Intent(context, LoginActivity::class.java) @@ -195,11 +218,24 @@ class Authentication(private val context: Context) { val sharedPreferences : SharedPreferences = context.getSharedPreferences(context.getString(R.string.preferences), Context.MODE_PRIVATE) // if no email in shared preferences, return default value - return sharedPreferences.getString(context.getString(R.string.email), context.getString(R.string.default_email)) ?: "" + return sharedPreferences.getString(context.getString(R.string.email), "") ?: "" } fun getNim(): Int { val email = getEmail().split("@")[0] + // if not castable to int, return 0 + if (email.toIntOrNull() == null) { + return 0 + } return email.toInt() } + + @SuppressLint("ServiceCast") + private fun isNetworkAvailable(): Boolean { + val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + val network = connectivityManager.activeNetwork + val networkCapabilities = connectivityManager.getNetworkCapabilities(network) + return networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true + } + } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4581d8e0758b7459fcc034c819f0fc3c2a63d1df..6a6d9fa32baed9091b99b0fb716f0ce165da584c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -19,9 +19,11 @@ <string name="key_alias">key13521000</string> <string name="cipher_transformation">RSA/ECB/PKCS1Padding</string> <string name="android_key_store">AndroidKeyStore</string> - <string name="default_email">13521000@std.stei.itb.ac.id</string> <string name="authorization">Authorization</string> <string name="bearer">Bearer</string> + + <string name="no_internet">No Internet Connection</string> + <string name="server_error">Server Error</string> <string name="preferences">NosPreferences</string> <string name="backend_api_login">https://pbd-backend-2024.vercel.app/api/auth/login</string> <string name="backend_api_token">https://pbd-backend-2024.vercel.app/api/auth/token</string>