Implement registering user

This commit is contained in:
Reimar 2025-04-29 13:55:50 +02:00
parent 93057d43d5
commit 5e32ad7133
Signed by: Reimar
GPG Key ID: 93549FA07F0AE268
3 changed files with 33 additions and 9 deletions

View File

@ -3,10 +3,12 @@ package tech.mercantec.easyeat
import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import tech.mercantec.easyeat.helpers.ApiRequestException
import tech.mercantec.easyeat.helpers.register
import kotlin.concurrent.thread
class RegisterActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
@ -15,10 +17,27 @@ class RegisterActivity : AppCompatActivity() {
setContentView(R.layout.activity_register)
findViewById<Button>(R.id.register).setOnClickListener {
// TODO create account
val username = findViewById<EditText>(R.id.username).text.toString()
val email = findViewById<EditText>(R.id.email).text.toString()
val password = findViewById<EditText>(R.id.password).text.toString()
val confirmPassword = findViewById<EditText>(R.id.confirm_password).text.toString()
startActivity(Intent(this, LoginActivity::class.java))
finish()
if (password != confirmPassword) {
Toast.makeText(this, "Passwords do not match", Toast.LENGTH_LONG).show()
return@setOnClickListener
}
thread {
try {
register(email, username, password)
} catch (e: ApiRequestException) {
Toast.makeText(this, e.message, Toast.LENGTH_LONG).show()
return@thread
}
startActivity(Intent(this, LoginActivity::class.java))
finish()
}
}
findViewById<Button>(R.id.back).setOnClickListener {

View File

@ -24,14 +24,16 @@ fun request(method: String, path: String, data: String?): HttpResponse {
requestMethod = method
if (data != null) {
setRequestProperty("Content-Type", "application/json")
outputStream.write(data.toByteArray())
outputStream.flush()
}
if (responseCode >= 400)
return HttpResponse(errorStream.readBytes().toString(), responseCode)
return HttpResponse(errorStream.readBytes().decodeToString(), responseCode)
return HttpResponse(inputStream.readBytes().toString(), responseCode)
return HttpResponse(inputStream.readBytes().decodeToString(), responseCode)
}
} catch (e: IOException) {
Log.e("EasyEat", e.toString())

View File

@ -24,10 +24,10 @@
/>
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username_hint"
android:autofillHints="emailAddress"
/>
<TextView
@ -39,6 +39,7 @@
/>
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email_hint"
@ -54,6 +55,7 @@
/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password_hint"
@ -70,6 +72,7 @@
/>
<EditText
android:id="@+id/confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/password_hint"