Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Android User Authentication with Login and Registration

Tech 1

AuthenticationActivity

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AuthenticationActivity extends AppCompatActivity {

    private final ExecutorService networkExecutor = Executors.newSingleThreadExecutor();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_authentication);

        findViewById(R.id.btn_go_to_enroll).setOnClickListener(v -> 
            startActivity(new Intent(AuthenticationActivity.this, EnrollmentActivity.class))
        );
    }

    public void processLogin(View view) {
        EditText usernameField = findViewById(R.id.et_username);
        EditText passkeyField = findViewById(R.id.et_passkey);

        String account = usernameField.getText().toString();
        String secret = passkeyField.getText().toString();

        // Admin bypass logic
        if ("admin".equals(account) && "admin".equals(secret)) {
            startActivity(new Intent(this, AdminDashboard.class));
            return;
        }

        networkExecutor.execute(() -> {
            UserRepository repo = new UserRepository();
            int authResult = repo.authenticateUser(account, secret);
            
            runOnUiThread(() -> {
                switch (authResult) {
                    case 1:
                        showToast("Access granted.");
                        startActivity(new Intent(AuthenticationActivity.this, UserDashboard.class));
                        break;
                    case 0:
                        showToast("Authentication failed.");
                        break;
                    case 2:
                        showToast("Incorrect passkey.");
                        break;
                    case 3:
                        showToast("User ID not found.");
                        break;
                }
            });
        });
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

EnrollmentActivity

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class EnrollmentActivity extends AppCompatActivity {

    private EditText userIdInput;
    private EditText fullNameInput;
    private EditText contactInput;
    private EditText groupInput;
    private EditText secretInput;

    private final ExecutorService networkExecutor = Executors.newSingleThreadExecutor();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_enrollment);

        userIdInput = findViewById(R.id.et_user_id);
        fullNameInput = findViewById(R.id.et_full_name);
        contactInput = findViewById(R.id.et_contact_number);
        groupInput = findViewById(R.id.et_group_name);
        secretInput = findViewById(R.id.et_passkey);

        findViewById(R.id.btn_return_to_login).setOnClickListener(v -> {
            startActivity(new Intent(EnrollmentActivity.this, AuthenticationActivity.class));
            finish();
        });
    }

    public void processEnrollment(View view) {
        String uid = userIdInput.getText().toString();
        String name = fullNameInput.getText().toString();
        String phone = contactInput.getText().toString();
        String grp = groupInput.getText().toString();
        String pwd = secretInput.getText().toString();

        AppUser candidate = new AppUser(uid, name, phone, grp, pwd);

        networkExecutor.execute(() -> {
            UserRepository repo = new UserRepository();
            AppUser existing = repo.fetchUserById(candidate.getUserId());
            
            if (existing != null) {
                runOnUiThread(() -> showToast("This user ID is already registered."));
            } else {
                boolean isSuccess = repo.insertUser(candidate);
                runOnUiThread(() -> {
                    if (isSuccess) {
                        showToast("Enrollment completed successfully.");
                        Intent resultIntent = new Intent();
                        resultIntent.putExtra("enrollment_status", "completed");
                        setResult(RESULT_OK, resultIntent);
                        finish();
                    } else {
                        showToast("Failed to complete enrollment.");
                    }
                });
            }
        });
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }
}

activity_authentication.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F0F0F0">

    <ImageView
        android:id="@+id/iv_app_logo"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginTop="100dp"
        android:src="@drawable/gb1"/>

    <TextView
        android:id="@+id/tv_form_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="User Authentication"
        android:gravity="center"
        android:textStyle="italic"
        android:textColor="#757575"
        android:textSize="28sp" />

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="14dp"
        android:layout_marginHorizontal="20dp"
        android:layout_marginVertical="8dp"
        android:background="@drawable/translucent_edit"
        android:hint="Username"
        android:textSize="22sp"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_passkey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="14dp"
        android:layout_marginHorizontal="20dp"
        android:layout_marginVertical="8dp"
        android:background="@drawable/translucent_edit"
        android:hint="Password"
        android:textSize="22sp"
        android:maxLength="16"
        android:singleLine="true"
        android:inputType="textPassword" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="16dp">

        <Button
            android:id="@+id/btn_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="14dp"
            android:layout_margin="14dp"
            android:layout_weight="1"
            android:textColor="@color/cardview_light_background"
            android:background="@drawable/translucent_button"
            android:text="Login"
            android:textSize="22sp"
            android:onClick="processLogin"/>

        <Button
            android:id="@+id/btn_go_to_enroll"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="14dp"
            android:layout_margin="14dp"
            android:layout_weight="1"
            android:textColor="@color/cardview_light_background"
            android:background="@drawable/translucent_button"
            android:text="Register"
            android:textSize="22sp" />

    </LinearLayout>

</LinearLayout>

activity_enrollment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F0F0F0"
    android:padding="16dp">

    <ImageView
        android:id="@+id/iv_brand_logo"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginTop="2dp"
        android:src="@drawable/gb1"/>

    <TextView
        android:id="@+id/tv_screen_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginVertical="8dp"
        android:text="User Enrollment"
        android:gravity="center"
        android:textStyle="italic"
        android:textColor="#757575"
        android:textSize="28sp" />

    <EditText
        android:id="@+id/et_user_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_marginVertical="6dp"
        android:background="@drawable/translucent_edit"
        android:hint="User ID"
        android:textSize="22sp"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_full_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_marginVertical="6dp"
        android:background="@drawable/translucent_edit"
        android:hint="Full Name"
        android:textSize="22sp"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_contact_number"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_marginVertical="6dp"
        android:background="@drawable/translucent_edit"
        android:hint="Contact Number"
        android:textSize="22sp"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_group_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_marginVertical="6dp"
        android:background="@drawable/translucent_edit"
        android:hint="Group/Class"
        android:textSize="22sp"
        android:singleLine="true" />

    <EditText
        android:id="@+id/et_passkey"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:layout_marginVertical="6dp"
        android:background="@drawable/translucent_edit"
        android:hint="Password"
        android:textSize="22sp"
        android:maxLength="16"
        android:singleLine="true"
        android:inputType="textPassword" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="16dp">

        <Button
            android:id="@+id/btn_submit_enrollment"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="14dp"
            android:layout_marginEnd="8dp"
            android:background="@drawable/translucent_button"
            android:text="Enroll"
            android:textColor="@color/dark"
            android:textSize="22sp"
            android:onClick="processEnrollment"/>

        <Button
            android:id="@+id/btn_return_to_login"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="14dp"
            android:layout_marginStart="8dp"
            android:textColor="@color/dark"
            android:background="@drawable/translucent_button"
            android:text="Back to Login"
            android:textSize="22sp" />

    </LinearLayout>

</LinearLayout>

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.