Commit 00d9c4c1 authored by Daniel Cesanelli's avatar Daniel Cesanelli
Browse files

Agregado registración y login con pass hashed

parent c7886e09
......@@ -2,6 +2,7 @@ package com.cesanelli.lajusta.io;
import com.cesanelli.lajusta.io.models.Producer;
import com.cesanelli.lajusta.io.models.Product;
import com.cesanelli.lajusta.io.models.Signup;
import com.cesanelli.lajusta.io.models.Token;
import com.cesanelli.lajusta.io.models.TokenBody;
import com.cesanelli.lajusta.io.models.User;
......@@ -32,8 +33,9 @@ public interface ApiService {
Call<Token> login(@Body TokenBody body);
@POST("user/signup")
Call<User> signup(@Body User body);
Call<User> signup(@Body Signup body);
@GET("user/{id}")
Call<User> getUser(@Path("id") int id, @Header("Authorization") String authHeader);
}
package com.cesanelli.lajusta.io.models;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class Signup {
private String firstName;
private String lastName;
private String email;
private String phone;
private String encryptedPassword;
public Signup() {
}
public Signup(String firstName, String lastName, String email, String phone, String encryptedPassword) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
this.encryptedPassword = md5(encryptedPassword);
}
public static final String md5(final String s) {
try {
// Create MD5 Hash
MessageDigest digest = java.security.MessageDigest
.getInstance("MD5");
digest.update(s.getBytes());
byte messageDigest[] = digest.digest();
// Create Hex String
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String h = Integer.toHexString(0xFF & messageDigest[i]);
while (h.length() < 2)
h = "0" + h;
hexString.append(h);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return "";
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEncryptedPassword() {
return encryptedPassword;
}
public void setEncryptedPassword(String encryptedPassword) {
this.encryptedPassword = encryptedPassword;
}
}
......@@ -3,7 +3,6 @@ package com.cesanelli.lajusta.ui;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
......@@ -19,9 +18,9 @@ import androidx.navigation.Navigation;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.Signup;
import com.cesanelli.lajusta.io.models.Token;
import com.cesanelli.lajusta.io.models.TokenBody;
import com.cesanelli.lajusta.ui.viewmodel.ProductListViewModel;
import com.cesanelli.lajusta.ui.viewmodel.UserViewModel;
import retrofit2.Call;
......@@ -66,7 +65,7 @@ public class LoginFragment extends Fragment {
public void onClick(View v) {
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(getContext());
Call<Token> call = ApiAdapter.getApiService().login(new TokenBody(login_user.getText().toString(), login_password.getText().toString()));
Call<Token> call = ApiAdapter.getApiService().login(new TokenBody(login_user.getText().toString().trim(), Signup.md5(login_password.getText().toString().trim())));
call.enqueue(new Callback<Token>() {
......@@ -79,7 +78,7 @@ public class LoginFragment extends Fragment {
sharedpreferences.edit().putString("UserID", Integer.toString(token.getUser().getId())).apply();
UserViewModel userViewModel = new ViewModelProvider(getActivity()).get(UserViewModel.class);
userViewModel.setToken(token);
Toast.makeText(getContext(),"¡Bienvenido "+ token.getUser().getName()+"!",Toast.LENGTH_SHORT).show();
Toast.makeText(getContext(), "¡Bienvenido " + token.getUser().getFirstName() + "!", Toast.LENGTH_SHORT).show();
Navigation.findNavController(v).navigate(R.id.nav_products);
}
} catch (Exception ex) {
......
......@@ -5,15 +5,33 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import com.cesanelli.lajusta.R;
import com.cesanelli.lajusta.io.ApiAdapter;
import com.cesanelli.lajusta.io.models.Signup;
import com.cesanelli.lajusta.io.models.Token;
import com.cesanelli.lajusta.io.models.User;
import com.cesanelli.lajusta.ui.viewmodel.UserViewModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class RegisterFragment extends Fragment {
private EditText user_firstname;
private EditText user_lastname;
private EditText user_email;
private EditText user_phone;
private EditText user_password;
private EditText user_retry_password;
public RegisterFragment() {
// Required empty public constructor
......@@ -40,12 +58,96 @@ public class RegisterFragment extends Fragment {
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button btnRegistrar = view.findViewById(R.id.btn_register);
user_firstname = view.findViewById(R.id.user_firstname);
user_lastname = view.findViewById(R.id.user_lastname);
user_email = view.findViewById(R.id.user_email);
user_phone = view.findViewById(R.id.user_phone);
user_password = view.findViewById(R.id.user_password);
user_retry_password = view.findViewById(R.id.user_retry_password);
btnRegistrar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Navigation.findNavController(v).navigate(R.id.nav_cart);
if (validateFirstname() && validateLastname() && validateEmail() && validatePhone() && validatePassword()) {
Signup user = new Signup(user_firstname.getText().toString().trim(),
user_lastname.getText().toString().trim(),
user_email.getText().toString().trim(),
user_phone.getText().toString().trim(),
user_password.getText().toString().trim());
ApiAdapter.getApiService().signup(user).enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
try {
if (response.isSuccessful()) {
Toast.makeText(getContext(), "Usuario registrado correctamente!", Toast.LENGTH_LONG).show();
Navigation.findNavController(v).navigate(R.id.nav_products);
}
} catch (Exception ex) {
Toast.makeText(getContext(), ex.getMessage(), Toast.LENGTH_LONG).show();
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
});
}
private boolean validateFirstname() {
if (user_firstname.getText().toString().trim().length() < 3) {
user_firstname.setError("El nombre debe tener más de 3 letras.");
return false;
}
return true;
}
private boolean validateLastname() {
if (user_lastname.getText().toString().trim().length() < 3) {
user_lastname.setError("El apellido debe tener más de 3 letras.");
return false;
}
return true;
}
private boolean validateEmail() {
String email = user_email.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
if (!email.matches(emailPattern)) {
user_email.setError("El email es inválido.");
return false;
}
return true;
}
private boolean validatePhone() {
String phone = user_phone.getText().toString().trim();
String phonePattern = "^[+]?[0-9]{7,13}$";
if (!phone.matches(phonePattern)) {
user_phone.setError("El teléfono es inválido.");
return false;
}
return true;
}
private boolean validatePassword() {
String pass = user_password.getText().toString().trim();
String pass_r = user_retry_password.getText().toString().trim();
if (pass.length() < 3) {
user_password.setError("El Password debe tener más de 3 letras.");
return false;
}
if (!pass.equals(pass_r)) {
user_retry_password.setError("Reiteración de password incorrecta.");
return false;
}
return true;
}
}
\ No newline at end of file
......@@ -41,7 +41,7 @@ public class ProductListViewModel extends ViewModel {
}
ArrayList<Category> categories = new ArrayList<Category>();
categories.add(new Category(0, ""));
categories.add(new Category(0, "Todas"));
categories.addAll(categoriesList);
this.products.setValue(products);
......
......@@ -13,7 +13,7 @@
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/txt_username"
android:inputType="textPersonName"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toTopOf="@+id/login_password"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment