Working login function with token storage
This commit is contained in:
parent
b4d6b5dd0c
commit
9326b88973
@ -9,9 +9,10 @@
|
|||||||
android:label="@string/app_name"
|
android:label="@string/app_name"
|
||||||
android:roundIcon="@mipmap/ic_launcher_round"
|
android:roundIcon="@mipmap/ic_launcher_round"
|
||||||
android:supportsRtl="true"
|
android:supportsRtl="true"
|
||||||
|
android:usesCleartextTraffic="true"
|
||||||
android:theme="@style/AppTheme">
|
android:theme="@style/AppTheme">
|
||||||
|
|
||||||
<activity android:name=".Register"></activity>
|
<activity android:name=".Register" />
|
||||||
<activity
|
<activity
|
||||||
android:name=".Settings"
|
android:name=".Settings"
|
||||||
android:label="@string/title_activity_settings" />
|
android:label="@string/title_activity_settings" />
|
||||||
|
@ -1,13 +1,11 @@
|
|||||||
package de.hft.geotracker
|
package de.hft.geotracker
|
||||||
|
|
||||||
import okhttp3.Authenticator
|
|
||||||
import retrofit2.Call
|
import retrofit2.Call
|
||||||
import retrofit2.http.*
|
import retrofit2.http.*
|
||||||
|
|
||||||
interface GeofenceService {
|
interface GeofenceService {
|
||||||
@POST("/login")
|
@POST("/login")
|
||||||
fun login(@Query("user") user : String,
|
fun login(@Body login_data : ValuesUserLogin) : Call<Void>
|
||||||
@Query("pw") password : String) : Call<Headers>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,21 +4,13 @@ import android.content.Intent
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
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.Call
|
||||||
import retrofit2.Callback
|
import retrofit2.Callback
|
||||||
import retrofit2.Response
|
import retrofit2.Response
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
import retrofit2.http.Header
|
|
||||||
import retrofit2.http.Headers
|
|
||||||
import java.net.HttpRetryException
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A simple [Fragment] subclass.
|
* A simple [Fragment] subclass.
|
||||||
@ -28,18 +20,20 @@ class Login : AppCompatActivity() {
|
|||||||
lateinit var reg : TextView
|
lateinit var reg : TextView
|
||||||
lateinit var service : GeofenceService
|
lateinit var service : GeofenceService
|
||||||
lateinit var token : JWToken
|
lateinit var token : JWToken
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_login)
|
setContentView(R.layout.activity_login)
|
||||||
|
|
||||||
val retrofit = Retrofit.Builder()
|
val retrofit = Retrofit.Builder()
|
||||||
.baseUrl("http://localhost:5000")
|
.baseUrl("http://plesk.icaotix.de:5000")
|
||||||
.addConverterFactory(GsonConverterFactory.create())
|
.addConverterFactory(GsonConverterFactory.create())
|
||||||
.build()
|
.build()
|
||||||
service = retrofit.create(GeofenceService::class.java)
|
service = retrofit.create(GeofenceService::class.java)
|
||||||
|
|
||||||
login = findViewById(R.id.button_create_account)
|
login = findViewById(R.id.button_create_account)
|
||||||
login.setOnClickListener {
|
login.setOnClickListener {
|
||||||
|
intent = Intent(this, MainActivity::class.java)
|
||||||
login()
|
login()
|
||||||
}
|
}
|
||||||
reg = findViewById(R.id.button_register)
|
reg = findViewById(R.id.button_register)
|
||||||
@ -54,29 +48,35 @@ class Login : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun login() {
|
private fun login() {
|
||||||
var call= service.login("wito","tobias")
|
val name = findViewById<TextView>(R.id.setting_input_username).text.toString()
|
||||||
|
val pswd = findViewById<TextView>(R.id.input_password).text.toString()
|
||||||
|
var call= service.login(ValuesUserLogin(name, pswd))
|
||||||
|
|
||||||
call.enqueue(object : Callback<Headers> {
|
call.enqueue(object : Callback<Void> {
|
||||||
override fun onResponse(call: Call<Headers>?, response: Response<Headers>?) {
|
override fun onResponse(call: Call<Void>?, response: Response<Void>?) {
|
||||||
if(response != null && response.isSuccessful) {
|
if(response != null && response.isSuccessful) {
|
||||||
|
|
||||||
var headers = response.headers()
|
var headers = response.headers()
|
||||||
var authentication = headers.get("Authorization")
|
var authentication = headers.get("Authorization")
|
||||||
|
|
||||||
token = JWToken(authentication)
|
token = JWToken(authentication)
|
||||||
println("Erfolg")
|
println(response.code())
|
||||||
|
println(token.token)
|
||||||
|
|
||||||
|
startActivity(intent)
|
||||||
} else {
|
} 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<Headers>?, t: Throwable?) {
|
override fun onFailure(call: Call<Void>?, t: Throwable?) {
|
||||||
println("Fehler2 ${t.toString()}")
|
println("Error: ${t.toString()}")
|
||||||
}
|
}
|
||||||
|
|
||||||
})
|
})
|
||||||
// var token = call.execute().body()
|
|
||||||
// val intent = Intent(this, MainActivity::class.java)
|
|
||||||
// startActivity(intent)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user