From e6834a18e6bb637b49131ae9c21e9d4db59c9c49 Mon Sep 17 00:00:00 2001 From: wiecktobi Date: Mon, 11 May 2020 14:24:01 +0200 Subject: [PATCH 1/3] Created Login with Retrofit and store Token (with exceptions) --- android/app/build.gradle | 3 ++ android/app/src/main/AndroidManifest.xml | 2 + .../java/de/hft/geotracker/GeofenceService.kt | 15 +++++++ .../main/java/de/hft/geotracker/JWToken.kt | 3 ++ .../src/main/java/de/hft/geotracker/Login.kt | 41 ++++++++++++++++++- .../res/drawable/ic_search_black_24dp.xml | 0 6 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 android/app/src/main/java/de/hft/geotracker/GeofenceService.kt create mode 100644 android/app/src/main/java/de/hft/geotracker/JWToken.kt delete mode 100644 android/app/src/main/res/drawable/ic_search_black_24dp.xml 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..357f575 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,7 @@ + + + + + +} \ 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..2904a1c 100644 --- a/android/app/src/main/java/de/hft/geotracker/Login.kt +++ b/android/app/src/main/java/de/hft/geotracker/Login.kt @@ -5,6 +5,17 @@ import android.os.Bundle import androidx.fragment.app.Fragment import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext +import okhttp3.OkHttpClient +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response +import retrofit2.Retrofit +import retrofit2.converter.gson.GsonConverterFactory +import java.net.HttpRetryException /** * A simple [Fragment] subclass. @@ -12,9 +23,18 @@ 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://localhost:5000") + .addConverterFactory(GsonConverterFactory.create()) + .build() + service = retrofit.create(GeofenceService::class.java) + login = findViewById(R.id.button_create_account) login.setOnClickListener { login() @@ -31,7 +51,24 @@ class Login : AppCompatActivity() { } private fun login() { - val intent = Intent(this, MainActivity::class.java) - startActivity(intent) + var call : Call = service.login("wito","tobias") + call.enqueue(object : Callback { + override fun onResponse(call: Call?, response: Response?) { + if(response != null && response.isSuccessful) { + response.body() + token = JWToken(call.toString()) + println("Erfolg") + } else { + println("Fehler1") + } + } + + override fun onFailure(call: Call?, t: Throwable?) { + println("Fehler2 ${t.toString()}") + } + }) +// var token = call.execute().body() +// val intent = Intent(this, MainActivity::class.java) +// startActivity(intent) } } diff --git a/android/app/src/main/res/drawable/ic_search_black_24dp.xml b/android/app/src/main/res/drawable/ic_search_black_24dp.xml deleted file mode 100644 index e69de29..0000000 From b4d6b5dd0c5dd05cb79dc97c76d0402da5f06c66 Mon Sep 17 00:00:00 2001 From: wiecktobi Date: Mon, 11 May 2020 20:59:32 +0200 Subject: [PATCH 2/3] Changed to read header --- .../java/de/hft/geotracker/GeofenceService.kt | 9 ++++----- .../src/main/java/de/hft/geotracker/Login.kt | 20 +++++++++++++------ 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt b/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt index a30c052..31320ae 100644 --- a/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt +++ b/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt @@ -1,14 +1,13 @@ package de.hft.geotracker +import okhttp3.Authenticator import retrofit2.Call -import retrofit2.http.GET -import retrofit2.http.Path -import retrofit2.http.Query +import retrofit2.http.* interface GeofenceService { - @GET("/login?username=user&password=pw") + @POST("/login") fun login(@Query("user") user : String, - @Query("pw") password : String) : Call + @Query("pw") password : String) : Call 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 2904a1c..153899f 100644 --- a/android/app/src/main/java/de/hft/geotracker/Login.kt +++ b/android/app/src/main/java/de/hft/geotracker/Login.kt @@ -9,12 +9,15 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import okhttp3.Authenticator import okhttp3.OkHttpClient import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory +import retrofit2.http.Header +import retrofit2.http.Headers import java.net.HttpRetryException /** @@ -51,21 +54,26 @@ class Login : AppCompatActivity() { } private fun login() { - var call : Call = service.login("wito","tobias") - call.enqueue(object : Callback { - override fun onResponse(call: Call?, response: Response?) { + var call= service.login("wito","tobias") + + call.enqueue(object : Callback { + override fun onResponse(call: Call?, response: Response?) { if(response != null && response.isSuccessful) { - response.body() - token = JWToken(call.toString()) + + var headers = response.headers() + var authentication = headers.get("Authorization") + + token = JWToken(authentication) println("Erfolg") } else { println("Fehler1") } } - override fun onFailure(call: Call?, t: Throwable?) { + override fun onFailure(call: Call?, t: Throwable?) { println("Fehler2 ${t.toString()}") } + }) // var token = call.execute().body() // val intent = Intent(this, MainActivity::class.java) From 9326b88973505ac98569a14506152e68b05d254e Mon Sep 17 00:00:00 2001 From: wiecktobi Date: Sun, 17 May 2020 15:58:42 +0200 Subject: [PATCH 3/3] Working login function with token storage --- android/app/src/main/AndroidManifest.xml | 3 +- .../java/de/hft/geotracker/GeofenceService.kt | 4 +- .../src/main/java/de/hft/geotracker/Login.kt | 46 +++++++++---------- .../java/de/hft/geotracker/ValuesUserLogin.kt | 12 +++++ 4 files changed, 38 insertions(+), 27 deletions(-) create mode 100644 android/app/src/main/java/de/hft/geotracker/ValuesUserLogin.kt diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 357f575..88c9093 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -9,9 +9,10 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" + android:usesCleartextTraffic="true" android:theme="@style/AppTheme"> - + diff --git a/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt b/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt index 31320ae..c9faeb3 100644 --- a/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt +++ b/android/app/src/main/java/de/hft/geotracker/GeofenceService.kt @@ -1,13 +1,11 @@ package de.hft.geotracker -import okhttp3.Authenticator import retrofit2.Call import retrofit2.http.* interface GeofenceService { @POST("/login") - fun login(@Query("user") user : String, - @Query("pw") password : String) : Call + fun login(@Body login_data : ValuesUserLogin) : Call 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 153899f..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,21 +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 kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch -import kotlinx.coroutines.withContext -import okhttp3.Authenticator -import okhttp3.OkHttpClient import retrofit2.Call import retrofit2.Callback import retrofit2.Response import retrofit2.Retrofit import retrofit2.converter.gson.GsonConverterFactory -import retrofit2.http.Header -import retrofit2.http.Headers -import java.net.HttpRetryException /** * A simple [Fragment] subclass. @@ -28,18 +20,20 @@ class Login : AppCompatActivity() { 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://localhost:5000") + .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) @@ -54,29 +48,35 @@ class Login : AppCompatActivity() { } private fun login() { - var call= service.login("wito","tobias") + 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?) { + 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") - var headers = response.headers() - var authentication = headers.get("Authorization") + token = JWToken(authentication) + println(response.code()) + println(token.token) - token = JWToken(authentication) - println("Erfolg") + startActivity(intent) } else { - println("Fehler1") + 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("Fehler2 ${t.toString()}") + override fun onFailure(call: Call?, t: Throwable?) { + println("Error: ${t.toString()}") } }) -// var token = call.execute().body() -// val intent = Intent(this, MainActivity::class.java) -// startActivity(intent) } } 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