Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sharedpreferences added #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 66 additions & 0 deletions .idea/other.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ dependencies {
implementation(libs.constraintlayout)
implementation(libs.navigation.fragment)
implementation(libs.navigation.ui)

testImplementation(libs.junit)
androidTestImplementation(libs.ext.junit)
androidTestImplementation(libs.espresso.core)
}
implementation(libs.biometric)



}

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "SignUp-Form";
private static final String DATABASE_NAME = "SignUp";
private static final String TABLE_CONTACTS = "SIGNIN";
private static final String KEY_EMAIL = "Email";
private static final String KEY_USERNAME = "Username";
Expand All @@ -26,7 +26,7 @@ public DatabaseHandler(Context context) {
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_EMAIL + " TEXT PRIMARY KEY," // Change INTEGER to TEXT
+ KEY_EMAIL + " TEXT PRIMARY KEY,"
+ KEY_USERNAME + " TEXT,"
+ KEY_PASSWORD + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
Expand Down
132 changes: 94 additions & 38 deletions app/src/main/java/com/example/signup_signin/SignIn.java
Original file line number Diff line number Diff line change
@@ -1,87 +1,143 @@
package com.example.signup_signin;

import android.content.Intent;
import android.hardware.usb.UsbRequest;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.biometric.BiometricManager;
import androidx.biometric.BiometricPrompt;
import androidx.core.content.ContextCompat;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.concurrent.Executor;

import java.util.List;
public class SignIn extends AppCompatActivity {
Button signInBtn;
EditText useremail;
EditText userpassword;
TextView forgotpass;
TextView singUp;
DatabaseHandler db;
CheckBox checkBox;
Button fingerprintBtn;

@Override
public void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_sign_in);


useremail = findViewById(R.id.email);
userpassword = findViewById(R.id.password);
signInBtn = findViewById(R.id.signInBtn);
forgotpass = findViewById(R.id.forgotPassword);
singUp = findViewById(R.id.SignUp);
checkBox = findViewById(R.id.checkbox);
fingerprintBtn = findViewById(R.id.fingerprint);


SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
db = new DatabaseHandler(this);


signInBtn.setOnClickListener(new View.OnClickListener() {
Executor executor = ContextCompat.getMainExecutor(this);
BiometricPrompt biometricPrompt = new BiometricPrompt(this, executor, new BiometricPrompt.AuthenticationCallback() {
@Override
public void onClick(View view) {
String enteredEmail = useremail.getText().toString().trim();
String enteredPassword = userpassword.getText().toString().trim();
public void onAuthenticationError(int errorCode, @NonNull CharSequence errString) {
super.onAuthenticationError(errorCode, errString);
Toast.makeText(getApplicationContext(), "Authentication error: " + errString, Toast.LENGTH_SHORT).show();
}

if (enteredEmail.isEmpty() || enteredPassword.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter both email and password", Toast.LENGTH_SHORT).show();
return;
}
@Override
public void onAuthenticationSucceeded(@NonNull BiometricPrompt.AuthenticationResult result) {
super.onAuthenticationSucceeded(result);
Toast.makeText(getApplicationContext(), "Authentication succeeded!", Toast.LENGTH_SHORT).show();

UserInfo user = db.getuser(enteredEmail);

if (user != null && user.getEmail().equals(enteredEmail)) {
Toast.makeText(getApplicationContext(), "Successfully Signed In", Toast.LENGTH_SHORT).show();
String savedEmail = prefs.getString("username", null);
String savedPassword = prefs.getString("password", null);

} else {
Toast.makeText(getApplicationContext(), "Incorrect email or password", Toast.LENGTH_SHORT).show();
if (savedEmail != null && savedPassword != null) {
useremail.setText(savedEmail);
userpassword.setText(savedPassword);
signInBtn.performClick();
}
}
});


forgotpass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), forgot_password.class);
startActivity(intent);
public void onAuthenticationFailed() {
super.onAuthenticationFailed();
Toast.makeText(getApplicationContext(), "Authentication failed", Toast.LENGTH_SHORT).show();
}
});
singUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);

BiometricPrompt.PromptInfo promptInfo = new BiometricPrompt.PromptInfo.Builder()
.setTitle("Biometric login for My App")
.setSubtitle("Log in using your biometric credential")
.setNegativeButtonText("Use password instead")
.build();


BiometricManager biometricManager = BiometricManager.from(this);
//BiometricManager.BIOMETRIC_SUCCESS
fingerprintBtn.setVisibility(View.VISIBLE);
fingerprintBtn.setOnClickListener(view -> biometricPrompt.authenticate(promptInfo));


signInBtn.setOnClickListener(view -> {
String enteredEmail = useremail.getText().toString().trim();
String enteredPassword = userpassword.getText().toString().trim();

if (enteredEmail.isEmpty() || enteredPassword.isEmpty()) {
Toast.makeText(getApplicationContext(), "Please enter both email and password", Toast.LENGTH_SHORT).show();
return;
}

UserInfo user = db.getuser(enteredEmail);
if (user != null && user.getEmail().equals(enteredEmail)) {
boolean isChecked = checkBox.isChecked();

SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", enteredEmail);
editor.putString("password", enteredPassword);
editor.putBoolean("checkbox", isChecked);
editor.apply();

displaySharedPreferences(prefs);

Toast.makeText(getApplicationContext(), "Successfully Signed In", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Incorrect email or password", Toast.LENGTH_SHORT).show();
}
});

forgotpass.setOnClickListener(view -> {
Intent intent = new Intent(getApplicationContext(), forgot_password.class);
startActivity(intent);
});

singUp.setOnClickListener(view -> {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
});
}

public void displaySharedPreferences(SharedPreferences prefs) {
String savedEmail = prefs.getString("username", "No email found");
String savedPassword = prefs.getString("password", "No password foun");

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Saved Credentials");
builder.setMessage("Email: " + savedEmail + "\nPassword: " + savedPassword);
builder.setPositiveButton("Ok", null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
15 changes: 15 additions & 0 deletions app/src/main/res/layout/activity_sign_in.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
android:textColor="#ffffff"
android:textSize="22sp"
android:textStyle="bold"/>

<Button
android:id="@+id/fingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:text="fingerprint" />

</LinearLayout>
<LinearLayout
android:id="@+id/container"
Expand Down Expand Up @@ -98,6 +106,11 @@
android:padding="10dp"
android:text=""
android:textSize="14sp" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stay logged in"/>
<TextView
android:id="@+id/forgotPassword"
android:layout_width="wrap_content"
Expand All @@ -108,6 +121,7 @@
android:layout_marginLeft="200dp"
android:padding="6sp"
/>

</LinearLayout>

<RelativeLayout
Expand All @@ -128,6 +142,7 @@
android:minWidth="48dp"
android:minHeight="48dp"/>


<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[versions]
agp = "8.5.0"
biometric = "1.1.0"
junit = "4.13.2"
junitVersion = "1.2.1"
espressoCore = "3.6.1"
Expand All @@ -11,6 +12,7 @@ navigationFragment = "2.6.0"
navigationUi = "2.6.0"

[libraries]
biometric = { module = "androidx.biometric:biometric", version.ref = "biometric" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
Expand Down