Odradjeno register i login primer 26124006: password

This commit is contained in:
Daniel 2025-12-13 18:31:36 +01:00
parent e57e1248d7
commit e3f465d925
16 changed files with 541 additions and 56 deletions

View File

@ -37,6 +37,10 @@ dependencies {
implementation(libs.material)
implementation(libs.activity)
implementation(libs.constraintlayout)
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
@ -11,7 +11,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Gallery"
tools:targetApi="31">
tools:targetApi="31"
android:networkSecurityConfig="@xml/network_security_config">
<activity
android:name=".RegisterActivity"
android:exported="false" />
@ -27,6 +28,7 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@ -1,24 +1,103 @@
package com.example.gallery;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.gallery.api.ApiClient;
import com.example.gallery.api.ApiService;
import com.example.gallery.models.LoginRequest;
import com.example.gallery.models.LoginResponse;
import com.example.gallery.UserResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class LoginActivity extends AppCompatActivity {
EditText usernameEdit, passwordEdit;
Button loginBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_login);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
// Updated ids from your XML
usernameEdit = findViewById(R.id.usernameTxt);
passwordEdit = findViewById(R.id.userpasswordTxt);
loginBtn = findViewById(R.id.btnLogin);
loginBtn.setOnClickListener(v -> {
String username = usernameEdit.getText().toString().trim();
String password = passwordEdit.getText().toString().trim();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(LoginActivity.this, "All fields are required", Toast.LENGTH_SHORT).show();
return;
}
ApiService apiService = ApiClient.getClient().create(ApiService.class);
LoginRequest request = new LoginRequest(username, password);
// 🔹 Login API call
apiService.login(request).enqueue(new Callback<LoginResponse>() {
@Override
public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
if (response.isSuccessful() && response.body() != null) {
String token = "Token " + response.body().getToken();
// 🔹 Save token & login state in SharedPreferences
getSharedPreferences("MyAppPrefs", MODE_PRIVATE)
.edit()
.putString("authToken", token)
.putBoolean("isLoggedIn", true)
.apply();
Toast.makeText(LoginActivity.this, "Login successful", Toast.LENGTH_SHORT).show();
// 🔹 Verify token with /api/auth/me/
apiService.getMe(token).enqueue(new Callback<UserResponse>() {
@Override
public void onResponse(Call<UserResponse> call, Response<UserResponse> response) {
if (response.isSuccessful() && response.body() != null) {
Toast.makeText(LoginActivity.this,
"Welcome " + response.body().getUsername(),
Toast.LENGTH_SHORT).show();
// Go to MainActivity
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
Toast.makeText(LoginActivity.this,
"Failed to verify user",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<UserResponse> call, Throwable t) {
Toast.makeText(LoginActivity.this,
"Verification error: " + t.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(LoginActivity.this, "Login failed", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<LoginResponse> call, Throwable t) {
Toast.makeText(LoginActivity.this, "Server error: " + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
});
}
}

View File

@ -7,13 +7,11 @@ import android.widget.Button;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button loginBtn, registerBtn;
Button loginBtn, registerBtn, logoutBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -22,21 +20,46 @@ public class MainActivity extends AppCompatActivity {
loginBtn = findViewById(R.id.Loginbtn);
registerBtn = findViewById(R.id.Registerbtn);
logoutBtn = findViewById(R.id.btnLogout);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
}
// Button listeners
loginBtn.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
});
registerBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
}
registerBtn.setOnClickListener(v -> {
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivity(intent);
});
// Check login state from SharedPreferences
boolean isLoggedIn = getSharedPreferences("MyAppPrefs", MODE_PRIVATE)
.getBoolean("isLoggedIn", false);
if (isLoggedIn) {
// User is logged in hide login/register, show logout
loginBtn.setVisibility(View.GONE);
registerBtn.setVisibility(View.GONE);
logoutBtn.setVisibility(View.VISIBLE);
} else {
// User not logged in show login/register, hide logout
loginBtn.setVisibility(View.VISIBLE);
registerBtn.setVisibility(View.VISIBLE);
logoutBtn.setVisibility(View.GONE);
}
// Logout button
logoutBtn.setOnClickListener(v -> {
// Clear token and login state
getSharedPreferences("MyAppPrefs", MODE_PRIVATE)
.edit()
.remove("authToken") // remove saved token
.putBoolean("isLoggedIn", false) // set logged out
.apply();
// Refresh activity to reflect state change
recreate();
});
}
}

View File

@ -1,24 +1,133 @@
package com.example.gallery;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.example.gallery.api.ApiClient;
import com.example.gallery.api.ApiService;
import com.example.gallery.models.RegisterRequest;
import com.example.gallery.models.RegisterResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class RegisterActivity extends AppCompatActivity {
EditText username, email, ime, prezime, index, password;
Button btnRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_register);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
username = findViewById(R.id.usernameEditText);
email = findViewById(R.id.emailEditText);
ime = findViewById(R.id.imeEditText);
prezime = findViewById(R.id.prezimeEditText);
index = findViewById(R.id.indexEditText);
password = findViewById(R.id.passwordEditText);
btnRegister = findViewById(R.id.btnRegister);
Toast.makeText(this, "RegisterActivity loaded", Toast.LENGTH_SHORT).show();
btnRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String u = username.getText().toString().trim();
String e = email.getText().toString().trim();
String f = ime.getText().toString().trim();
String l = prezime.getText().toString().trim();
String s = index.getText().toString().trim();
String p = password.getText().toString().trim();
// ---------------------------
// FRONTEND VALIDATIONS
// ---------------------------
if (u.isEmpty() || u.length() > 150) {
username.setError("Username must be 1-150 characters");
return;
}
if (e.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(e).matches()) {
email.setError("Enter a valid email");
return;
}
if (f.isEmpty() || f.length() > 30) {
ime.setError("First name must be 1-30 characters");
return;
}
if (l.isEmpty() || l.length() > 30) {
prezime.setError("Last name must be 1-30 characters");
return;
}
if (s.isEmpty() || s.length() > 8) {
index.setError("School index must be 1-8 characters");
return;
}
if (p.isEmpty() || p.length() < 8) {
password.setError("Password must be at least 8 characters");
return;
}
// ---------------------------
// 🔹 API CALL
// ---------------------------
ApiService apiService = ApiClient.getClient().create(ApiService.class);
RegisterRequest request = new RegisterRequest(u, e, f, l, s, p);
apiService.register(request).enqueue(new Callback<RegisterResponse>() {
@Override
public void onResponse(Call<RegisterResponse> call, Response<RegisterResponse> response) {
Toast.makeText(RegisterActivity.this,
"HTTP code: " + response.code(),
Toast.LENGTH_SHORT).show();
if (response.isSuccessful() && response.body() != null) {
Toast.makeText(RegisterActivity.this,
response.body().getMessage(),
Toast.LENGTH_SHORT).show();
// Go to login
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent); // go back to previous activity
} else {
try {
String errorBody = response.errorBody() != null
? response.errorBody().string()
: "No error body";
Toast.makeText(RegisterActivity.this,
errorBody,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(RegisterActivity.this,
"Error reading errorBody",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<RegisterResponse> call, Throwable t) {
Toast.makeText(RegisterActivity.this,
"Server error: " + t.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
});
}
}

View File

@ -0,0 +1,40 @@
package com.example.gallery;
import com.google.gson.annotations.SerializedName;
public class UserResponse {
private String username;
@SerializedName("email")
private String email;
@SerializedName("first_name")
private String firstName;
@SerializedName("last_name")
private String lastName;
@SerializedName("school_index")
private String schoolIndex;
public String getUsername() {
return username;
}
public String getEmail() {
return email;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getSchoolIndex() {
return schoolIndex;
}
}

View File

@ -0,0 +1,21 @@
package com.example.gallery.api;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class ApiClient {
private static final String BASE_URL = "https://gallery.steve-dekart.xyz/";
private static Retrofit retrofit;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}

View File

@ -0,0 +1,25 @@
package com.example.gallery.api;
import com.example.gallery.UserResponse;
import com.example.gallery.models.LoginRequest;
import com.example.gallery.models.LoginResponse;
import com.example.gallery.models.RegisterRequest;
import com.example.gallery.models.RegisterResponse;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
public interface ApiService {
@POST("api/auth/register/")
Call<RegisterResponse> register(@Body RegisterRequest request);
@POST("api/auth/login/")
Call<LoginResponse> login(@Body LoginRequest request);
@GET("api/auth/me/")
Call<UserResponse> getMe(@Header("Authorization") String token);
}

View File

@ -0,0 +1,11 @@
package com.example.gallery.models;
public class LoginRequest {
private String username;
private String password;
public LoginRequest(String username, String password) {
this.username = username;
this.password = password;
}
}

View File

@ -0,0 +1,9 @@
package com.example.gallery.models;
public class LoginResponse {
private String token;
public String getToken() {
return token;
}
}

View File

@ -0,0 +1,30 @@
package com.example.gallery.models;
import com.google.gson.annotations.SerializedName;
public class RegisterRequest {
private String username;
private String email;
@SerializedName("first_name")
private String ime;
@SerializedName("last_name")
private String prezime;
@SerializedName("school_index")
private String index;
private String password;
public RegisterRequest(String username, String email, String ime,
String prezime, String index, String password) {
this.username = username;
this.email = email;
this.ime = ime;
this.prezime = prezime;
this.index = index;
this.password = password;
}
}

View File

@ -0,0 +1,14 @@
package com.example.gallery.models;
public class RegisterResponse {
private boolean success;
private String message;
public boolean isSuccess() {
return success;
}
public String getMessage() {
return message;
}
}

View File

@ -10,30 +10,38 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
android:orientation="vertical">
<TextView
android:id="@+id/usernameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_width="144dp"
android:layout_height="43dp"
android:text="Username"
/>
android:textSize="21dp" />
<EditText
android:id="@+id/usernameTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
android:layout_height="72dp"
android:hint="Korisničko ime" />
<TextView
android:id="@+id/userpasswordTV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Password"
/>
android:layout_width="126dp"
android:layout_height="31dp"
android:text="Password" />
<EditText
android:id="@+id/userpasswordTxt"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
android:layout_width="322dp"
android:layout_height="76dp"
android:hint="Lozinka" />
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in" />
</LinearLayout>

View File

@ -59,4 +59,16 @@
android:text="Register"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btnLogout"
android:layout_width="103dp"
android:layout_height="51dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:text="Logout"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -6,10 +6,100 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".RegisterActivity">
<TextView
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="REGISTER"
/>
android:orientation="vertical">
<TextView
android:id="@+id/usernameTextView"
android:layout_width="144dp"
android:layout_height="43dp"
android:text="Username"
android:textSize="21dp" />
<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="Username"
android:inputType="textPersonName"
android:maxLength="150" />
<TextView
android:id="@+id/emailTextView"
android:layout_width="126dp"
android:layout_height="31dp"
android:text="Email Adresa" />
<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="61dp"
android:hint="Email"
android:inputType="textEmailAddress"
android:maxLength="254" />
<TextView
android:id="@+id/imeTextView"
android:layout_width="126dp"
android:layout_height="31dp"
android:text="Ime" />
<EditText
android:id="@+id/imeEditText"
android:layout_width="match_parent"
android:layout_height="67dp"
android:hint="First Name"
android:maxLength="30" />
<TextView
android:id="@+id/prezimeTextView"
android:layout_width="126dp"
android:layout_height="31dp"
android:text="Prezime" />
<EditText
android:id="@+id/prezimeEditText"
android:layout_width="match_parent"
android:layout_height="96dp"
android:hint="Last Name"
android:maxLength="30" />
<TextView
android:id="@+id/indexTextView"
android:layout_width="126dp"
android:layout_height="31dp"
android:text="Indeks" />
<EditText
android:id="@+id/indexEditText"
android:layout_width="match_parent"
android:layout_height="67dp"
android:hint="School Index"
android:inputType="text"
android:maxLength="8" />
<TextView
android:id="@+id/passwordTextView"
android:layout_width="126dp"
android:layout_height="31dp"
android:text="Password" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="59dp"
android:hint="Password"
android:inputType="textPassword"
android:maxLength="128" />
<Button
android:id="@+id/btnRegister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<network-security-config xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingPrefix">
<domain-config cleartextTrafficPermitted="true">
<domain>10.0.2.2</domain>
</domain-config>
</network-security-config>