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

Fix: add Id Uniqueness Validation #148

Merged
merged 3 commits into from
Dec 9, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public interface AccountApi {
@POST("/account/signup/")
Call<ResponseBody> postSignUpData(@Body SignUpData data);

@POST("/account/validate_id/")
Call<ResponseBody> postIdValidationData(@Body IdValidationData data);

@POST("/account/login/")
Call<ResponseBody> postLoginData(@Body AccountData data);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.example.runusandroid;

import com.google.gson.annotations.SerializedName;

public class IdValidationData {
@SerializedName("username")
String username;

public IdValidationData(String username) {
this.username = username;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
Expand Down Expand Up @@ -40,6 +41,7 @@ public class LoginActivity extends AppCompatActivity {
private TextView passwordInput;
private Button loginButton;
private AccountApi accountApi;
private long loginButtonLastClickTime = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -132,88 +134,87 @@ public void updateDrawState(TextPaint ds) {
forgotPasswordText.setText(ssforpw);
forgotPasswordText.setMovementMethod(LinkMovementMethod.getInstance());

loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userName = idInput.getText().toString();
String password = passwordInput.getText().toString();

AccountData requestData = new AccountData(userName, password);

accountApi.postLoginData(requestData).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
Log.d("Login", "Login Success");
Toast.makeText(LoginActivity.this, "반가워요!", Toast.LENGTH_SHORT).show();
JSONObject responseBody = null;
try {
String responseBodyString = response.body().string();
responseBody = new JSONObject(responseBodyString);
Log.d("response", responseBodyString);
JSONObject userObject = responseBody.getJSONObject("user");
Long user_id = userObject.getLong("user_id");
String username = userObject.getString("username");
String nickname = userObject.getString("nickname");
String email = userObject.getString("email");
String profileImageUrl = userObject.optString("profile_image", "");
String phone_num = userObject.getString("phone_num");
int gender = userObject.getInt("gender");
float height = (float) userObject.getDouble("height");
float weight = (float) userObject.getDouble("weight");
int age = userObject.getInt("age");
int exp = userObject.getInt("exp");
int level = ExpSystem.getLevel(exp);
int badge_collection = userObject.getInt("badge_collection");
long lastLoginTime = System.currentTimeMillis();
Log.d("exp", "user's exp is " + exp);

String token = responseBody.getJSONObject("jwt_token").getString("access_token");
String refreshToken = responseBody.getJSONObject("jwt_token")
.getString("refresh_token");

setAuthToken(token);
// 로그인 성공 시 SharePreferences에 유저 정보 및 토큰 저장
SharedPreferences sharedPreferences = getSharedPreferences("user_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("userid", user_id);
editor.putString("token", token);
editor.putString("refresh_token", refreshToken);
editor.putString("username", username);
editor.putString("nickname", nickname);
editor.putString("email", email);
editor.putString("profile_image", profileImageUrl);
editor.putString("phone_num", phone_num);
editor.putInt("gender", gender);
editor.putFloat("height", height);
editor.putFloat("weight", weight);
editor.putInt("age", age);
editor.putInt("exp", exp);
editor.putInt("level", level);
editor.putInt("badge_collection", badge_collection);
editor.putLong("lastLoginTime", lastLoginTime);
editor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
startActivity(intent);
} catch (JSONException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
Log.d("Login", "Login Failed, Status Code : " + response.code());
Toast.makeText(LoginActivity.this, "로그인 실패", Toast.LENGTH_SHORT).show();
loginButton.setOnClickListener(view -> {
if (SystemClock.elapsedRealtime() - loginButtonLastClickTime < 2000) {
return;
}
loginButtonLastClickTime = SystemClock.elapsedRealtime();
String userName = idInput.getText().toString();
String password = passwordInput.getText().toString();

AccountData requestData = new AccountData(userName, password);

accountApi.postLoginData(requestData).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
Log.d("Login", "Login Success");
Toast.makeText(LoginActivity.this, "반가워요!", Toast.LENGTH_SHORT).show();
JSONObject responseBody = null;
try {
String responseBodyString = response.body().string();
responseBody = new JSONObject(responseBodyString);
Log.d("response", responseBodyString);
JSONObject userObject = responseBody.getJSONObject("user");
Long user_id = userObject.getLong("user_id");
String username = userObject.getString("username");
String nickname = userObject.getString("nickname");
String email = userObject.getString("email");
String profileImageUrl = userObject.optString("profile_image", "");
String phone_num = userObject.getString("phone_num");
int gender = userObject.getInt("gender");
float height = (float) userObject.getDouble("height");
float weight = (float) userObject.getDouble("weight");
int age = userObject.getInt("age");
int exp = userObject.getInt("exp");
int level = ExpSystem.getLevel(exp);
int badge_collection = userObject.getInt("badge_collection");
long lastLoginTime = System.currentTimeMillis();
Log.d("exp", "user's exp is " + exp);

String token = responseBody.getJSONObject("jwt_token").getString("access_token");
String refreshToken = responseBody.getJSONObject("jwt_token")
.getString("refresh_token");

setAuthToken(token);
// 로그인 성공 시 SharePreferences에 유저 정보 및 토큰 저장
SharedPreferences sharedPreferences = getSharedPreferences("user_prefs", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong("userid", user_id);
editor.putString("token", token);
editor.putString("refresh_token", refreshToken);
editor.putString("username", username);
editor.putString("nickname", nickname);
editor.putString("email", email);
editor.putString("profile_image", profileImageUrl);
editor.putString("phone_num", phone_num);
editor.putInt("gender", gender);
editor.putFloat("height", height);
editor.putFloat("weight", weight);
editor.putInt("age", age);
editor.putInt("exp", exp);
editor.putInt("level", level);
editor.putInt("badge_collection", badge_collection);
editor.putLong("lastLoginTime", lastLoginTime);
editor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity2.class);
startActivity(intent);
} catch (JSONException | IOException e) {
throw new RuntimeException(e);
}

}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
} else {
Log.d("Login", "Login Failed, Status Code : " + response.code());
Toast.makeText(LoginActivity.this, "로그인 실패", Toast.LENGTH_SHORT).show();
Log.e("Retrofit", "Error: " + t.getMessage());
}
});
}

}

@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(LoginActivity.this, "로그인 실패", Toast.LENGTH_SHORT).show();
Log.e("Retrofit", "Error: " + t.getMessage());
}
});
});

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.text.InputFilter;
import android.text.Spanned;
import android.view.MotionEvent;
Expand All @@ -15,12 +16,19 @@

import androidx.appcompat.app.AppCompatActivity;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SignUpStep1Activity extends AppCompatActivity {
private TextView signUpIdInput;
private TextView signUpPasswordInput;
private TextView signUpEmailInput;
private Button nextButton1;
private ImageButton backButton;
private AccountApi accountApi;
private long nextButtonLastClickTime = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -49,6 +57,8 @@ protected void onCreate(Bundle savedInstanceState) {
signUpPasswordInput.setText(password);
signUpEmailInput.setText(email);

accountApi = RetrofitClient.getClient().create(AccountApi.class);

//아이디는 영어 대,소문자와 숫자만 입력 가능함.
InputFilter Idfilter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
Expand All @@ -63,43 +73,61 @@ public CharSequence filter(CharSequence source, int start, int end, Spanned dest
IdText.setFilters(new InputFilter[]{Idfilter});


nextButton1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Id = signUpIdInput.getText().toString();
String password = signUpPasswordInput.getText().toString();
String email = signUpEmailInput.getText().toString();

// 이메일 주소가 올바르지 않으면 다음으로 넘어가지 않는다.
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
signUpEmailInput.setError("유효한 이메일 주소를 입력하세요");
return;
}
nextButton1.setOnClickListener(view -> {
if (SystemClock.elapsedRealtime() - nextButtonLastClickTime < 2000) {
return;
}
nextButtonLastClickTime = SystemClock.elapsedRealtime();
String Id = signUpIdInput.getText().toString();
String password1 = signUpPasswordInput.getText().toString();
String email1 = signUpEmailInput.getText().toString();

// 이메일 주소가 올바르지 않으면 다음으로 넘어가지 않는다.
if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email1).matches()) {
signUpEmailInput.setError("유효한 이메일 주소를 입력하세요");
return;
}

// 비밀번호는 8자리 이상이어야 한다.
if (password.length() < 8) {
signUpPasswordInput.setError("비밀번호는 8자리 이상이어야 합니다");
return;
// 비밀번호는 8자리 이상이어야 한다.
if (password1.length() < 8) {
signUpPasswordInput.setError("비밀번호는 8자리 이상이어야 합니다");
return;
}

// 아이디는 있는지만 검사한다.
if (Id.length() == 0) {
signUpIdInput.setError("아이디를 입력하세요.");
return;
}
IdValidationData requestData = new IdValidationData(Id);
accountApi.postIdValidationData(requestData).enqueue(new Callback<ResponseBody>(){

@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()) {
Intent intent1 = new Intent(SignUpStep1Activity.this, SignUpStep2Activity.class);
intent1.putExtra("userName", Id);
intent1.putExtra("password", password1);
intent1.putExtra("email", email1);
intent1.putExtra("nickname", nickname);
intent1.putExtra("phoneNumber", phoneNumber);
intent1.putExtra("height", height);
intent1.putExtra("weight", weight);
intent1.putExtra("gender", gender);
intent1.putExtra("age", age);
startActivity(intent1);
}
else {
signUpIdInput.setError("동일한 아이디가 이미 존재합니다.");
}
}

// 아이디는 있는지만 검사한다.
if (Id.length() == 0) {
signUpIdInput.setError("아이디를 입력하세요.");
return;
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
signUpIdInput.setError("동일한 아이디가 이미 존재합니다.");
}
});

Intent intent = new Intent(SignUpStep1Activity.this, SignUpStep2Activity.class);
intent.putExtra("userName", Id);
intent.putExtra("password", password);
intent.putExtra("email", email);
intent.putExtra("nickname", nickname);
intent.putExtra("phoneNumber", phoneNumber);
intent.putExtra("height", height);
intent.putExtra("weight", weight);
intent.putExtra("gender", gender);
intent.putExtra("age",age);
startActivity(intent);
}
});

backButton.setOnClickListener(new View.OnClickListener() {
Expand Down
Loading
Loading