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

Android Authentication by API #202

Merged
merged 3 commits into from
Jun 20, 2022
Merged
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
8 changes: 5 additions & 3 deletions android-client/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,24 @@ android {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_1_9
targetCompatibility JavaVersion.VERSION_1_9
}
buildFeatures {
viewBinding true
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.navigation:navigation-fragment:2.4.2'
implementation 'androidx.navigation:navigation-ui:2.4.2'
implementation 'androidx.preference:preference:1.2.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'androidx.room:room-common:2.4.2'
implementation 'androidx.room:room-runtime:2.4.2'
implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
Expand Down
4 changes: 3 additions & 1 deletion android-client/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MaKore"
tools:targetApi="31">
tools:targetApi="31"
android:usesCleartextTraffic="true"
>
<activity
android:name=".chat.AddContactActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.example.makore.api;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserAPI {
Retrofit retrofit;
UserServiceAPI userServiceAPI;

public UserAPI() {
retrofit = new Retrofit.Builder().
baseUrl("http://10.0.2.2:54321").
addConverterFactory(GsonConverterFactory.create()).
build();
userServiceAPI = retrofit.create(UserServiceAPI.class);
}

public Call<Map<String, String>> signin(String username, String password) {
return userServiceAPI.signin(Map.of("username", username, "password", password));
}

public Call<Void> signup(String username, String password, String name) {
return userServiceAPI.signup(Map.of("username", username, "password", password, "name", name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.makore.api;

import java.util.Map;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

public interface UserServiceAPI {
@POST("/api/contacts/signin")
Call<Map<String, String>> signin(@Body Map<String, String> user);

@POST("/api/contacts/signup")
Call<Void> signup(@Body Map<String, String> user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.makore.MainActivity;
import com.example.makore.api.UserAPI;
import com.example.makore.databinding.ActivitySignInBinding;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignInActivity extends AppCompatActivity {

private ActivitySignInBinding binding;
Expand Down Expand Up @@ -38,19 +46,33 @@ protected void onCreate(Bundle savedInstanceState) {
// Show error message
binding.editTextPassword.setError("Password is empty");
} else {
// Check if the username and password is correct
if (username.equals("admin") && password.equals("admin")) {
// Save the username in the SharedPreferences
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("username", username);
editor.apply();
// Go to the main screen
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
} else {
// Show error message
binding.editTextUsername.setError("One of the fields is invalid");
}
UserAPI userAPI = new UserAPI();
Call<Map<String, String>> call = userAPI.signin(username, password);
call.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<Map<String, String>> call, @NonNull Response<Map<String, String>> response) {
boolean success = response.isSuccessful();
if (success) {
// Save username and password to shared preferences
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("username", username);
editor.putString("token", response.body().get("token"));
editor.apply();
// Go to main activity
Intent intent = new Intent(SignInActivity.this, MainActivity.class);
startActivity(intent);
} else {
// Show error message
binding.editTextUsername.setError("Invalid username or password");
}
}

@Override
public void onFailure(@NonNull Call<Map<String, String>> call, @NonNull Throwable t) {
// Show error message
binding.editTextUsername.setError("Error connecting to server");
}
});
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.makore.MainActivity;
import com.example.makore.api.UserAPI;
import com.example.makore.databinding.ActivitySignUpBinding;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpActivity extends AppCompatActivity {

private ActivitySignUpBinding binding;
Expand Down Expand Up @@ -71,15 +79,45 @@ protected void onCreate(Bundle savedInstanceState) {
} else if (!displayName.matches("^[a-zA-Z '-.,]+$")) {
binding.editTextDisplayName.setError("Display name can only contain letters, spaces, hyphens, periods, dots, and commas");
}
// If all the fields are valid, go to the main screen

UserAPI userAPI = new UserAPI();
if (isValid) {
// Save the username in the SharedPreferences
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("username", username);
editor.apply();
// Go to the main screen
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(intent);
Call<Void> signunCall = userAPI.signup(username, password, displayName);
signunCall.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<Void> call, @NonNull retrofit2.Response<Void> response) {
if (response.isSuccessful()) {
Call<Map<String, String>> signinCall = userAPI.signin(username, password);
signinCall.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<Map<String, String>> call, @NonNull Response<Map<String, String>> response) {
if (response.isSuccessful()) {
Map<String, String> body = response.body();
String token = body.get("token");
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("token", token);
editor.putString("username", username);
editor.apply();
Intent intent = new Intent(SignUpActivity.this, MainActivity.class);
startActivity(intent);
}
}

@Override
public void onFailure(@NonNull Call<Map<String, String>> call, @NonNull Throwable t) {
t.printStackTrace();
}
});
} else {
binding.editTextUsername.setError("Username already exists");
}
}

@Override
public void onFailure(@NonNull Call<Void> call, @NonNull Throwable t) {

}
});
}
});
}
Expand Down
10 changes: 2 additions & 8 deletions web-api/Controllers/ContactsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,19 @@ public IActionResult SignUp([FromBody] JsonElement body)
{
// Sign up new user

string? username, password, confirmPassword, name;
string? username, password, name;
try
{
username = body.GetProperty("username").GetString();
password = body.GetProperty("password").GetString();
confirmPassword = body.GetProperty("confirmPassword").GetString();
name = body.GetProperty("name").GetString();
}
catch (Exception)
{
return BadRequest();
}

if (username == null || password == null || confirmPassword == null || name == null)
if (username == null || password == null || name == null)
{
return BadRequest();
}
Expand All @@ -162,11 +161,6 @@ public IActionResult SignUp([FromBody] JsonElement body)
return BadRequest();
}

// Ensure passwords match
if (password != confirmPassword)
{
return BadRequest();
}

// Check if password contains at least one number, one lowercase and one uppercase character
if (password.Length < 6 || !Regex.IsMatch(password, @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).+$"))
Expand Down