diff --git a/android/app/build.gradle b/android/app/build.gradle index 0eab8f5..c015307 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -45,4 +45,7 @@ dependencies { implementation 'androidx.navigation:navigation-ui-ktx:2.2.2' implementation "android.arch.navigation:navigation-fragment-ktx:2.2.2" implementation "android.arch.navigation:navigation-ui-ktx:2.2.2" + + implementation 'com.squareup.retrofit2:retrofit:2.1.0' + implementation 'com.squareup.retrofit2:converter-gson:2.1.0' } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 1e18427..88c9093 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + - + + diff --git a/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt b/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt new file mode 100644 index 0000000..c9faeb3 --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt @@ -0,0 +1,12 @@ +package de.hft.geotracker + +import retrofit2.Call +import retrofit2.http.* + +interface GeofenceService { + @POST("/login") + fun login(@Body login_data : ValuesUserLogin) : Call + + + +} \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/JWToken.kt b/android/app/src/main/java/de/hft/geotracker/JWToken.kt new file mode 100644 index 0000000..c9af2dd --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/JWToken.kt @@ -0,0 +1,3 @@ +package de.hft.geotracker + +data class JWToken (var token : String) \ No newline at end of file diff --git a/android/app/src/main/java/de/hft/geotracker/Login.kt b/android/app/src/main/java/de/hft/geotracker/Login.kt index 8fa2428..30af167 100644 --- a/android/app/src/main/java/de/hft/geotracker/Login.kt +++ b/android/app/src/main/java/de/hft/geotracker/Login.kt @@ -4,7 +4,13 @@ import android.content.Intent import android.os.Bundle import androidx.fragment.app.Fragment import android.widget.TextView +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory /** * A simple [Fragment] subclass. @@ -12,11 +18,22 @@ import androidx.appcompat.app.AppCompatActivity class Login : AppCompatActivity() { lateinit var login : TextView lateinit var reg : TextView + lateinit var service : GeofenceService + lateinit var token : JWToken + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_login) + + val retrofit = Retrofit.Builder() + .baseUrl("http://plesk.icaotix.de:5000") + .addConverterFactory(GsonConverterFactory.create()) + .build() + service = retrofit.create(GeofenceService::class.java) + login = findViewById(R.id.button_create_account) login.setOnClickListener { + intent = Intent(this, MainActivity::class.java) login() } reg = findViewById(R.id.button_register) @@ -31,7 +48,35 @@ class Login : AppCompatActivity() { } private fun login() { - val intent = Intent(this, MainActivity::class.java) - startActivity(intent) + val name = findViewById(R.id.setting_input_username).text.toString() + val pswd = findViewById(R.id.input_password).text.toString() + var call= service.login(ValuesUserLogin(name, pswd)) + + call.enqueue(object : Callback { + override fun onResponse(call: Call?, response: Response?) { + if(response != null && response.isSuccessful) { + var headers = response.headers() + var authentication = headers.get("Authorization") + + token = JWToken(authentication) + println(response.code()) + println(token.token) + + startActivity(intent) + } else { + if (response != null) { + println(response.code()) + Toast.makeText(this@Login, "Wrong Username or Password!", Toast.LENGTH_LONG).show() + } else { + println("Response is null") + } + } + } + + override fun onFailure(call: Call?, t: Throwable?) { + println("Error: ${t.toString()}") + } + + }) } } diff --git a/android/app/src/main/java/de/hft/geotracker/ValuesUserLogin.kt b/android/app/src/main/java/de/hft/geotracker/ValuesUserLogin.kt new file mode 100644 index 0000000..429b9b2 --- /dev/null +++ b/android/app/src/main/java/de/hft/geotracker/ValuesUserLogin.kt @@ -0,0 +1,12 @@ +package de.hft.geotracker + +import com.google.gson.annotations.SerializedName + +class ValuesUserLogin (name : String, pswd : String) { + + @SerializedName("username") + var username = name + @SerializedName("password") + var password = pswd + +} \ No newline at end of file