Skip to content

Commit

Permalink
회원가입, 로그인, 로그아웃, 자동 로그인 (#32)
Browse files Browse the repository at this point in the history
* #31 회원가입 요청 코드 작성(초안)

* #31 회원가입, 로그인, 로그아웃, 자동 로그인 코드 작성

Co-authored-by: unknown <[email protected]>
  • Loading branch information
otcroz and yellow-jam authored Jun 29, 2022
1 parent 1f7f4ad commit 40ec89c
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 87 deletions.
2 changes: 1 addition & 1 deletion SHAPEUP2022/.idea/deploymentTargetDropDown.xml

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

2 changes: 2 additions & 0 deletions SHAPEUP2022/.idea/misc.xml

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

60 changes: 24 additions & 36 deletions SHAPEUP2022/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,40 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.SHAPEUP2022">
<activity
android:name=".YoutubeActivity"
android:exported="false" />
<activity
android:name=".SimulationActivity"
android:exported="false" />
android:theme="@style/Theme.SHAPEUP2022"
android:usesCleartextTraffic="true" >


<activity
android:name=".TempMainActivity"
android:exported="true">
</activity>
<activity
android:name=".SettingActivity"
android:exported="false" />
<activity
android:name=".MyPageActivity"
android:exported="false" />
<activity
android:name=".AchieveActivity"
android:exported="false" />
<activity
android:name=".preferenceActivity"
android:exported="false" /> <!-- 시작 및 회원가입/로그인 -->
<activity
android:name=".StartActivity"
android:exported="true" />
<activity
android:name=".LoginActivity"
android:exported="true" />
<activity
android:name=".JoinActivity"
android:exported="true" />
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<activity
android:name=".ToDoActivity"
android:exported="false" /> <!-- 정보 -->
<activity android:name=".AlarmActivity" />
<activity android:name=".BudgetActivity" />
android:name=".MainActivity"
android:exported="true">
</activity>


<activity android:name=".AchieveActivity" android:exported="false" />
<activity android:name=".AlarmActivity" android:exported="false" />
<activity android:name=".BudgetActivity" android:exported="false" />
<activity android:name=".JoinActivity" android:exported="false" />
<activity android:name=".LoginActivity" android:exported="false" />
<activity android:name=".MyPageActivity" android:exported="false" />
<activity android:name=".preferenceActivity" android:exported="false" />
<activity android:name=".SettingActivity" android:exported="false" />
<activity android:name=".SimulationActivity" android:exported="false" />
<activity android:name=".StartActivity" android:exported="false" />
<activity android:name=".ToDoActivity" android:exported="false" />
<activity android:name=".YoutubeActivity" android:exported="false" />

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.example.shape_up_2022.databinding.FragmentJoinCreateBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import android.util.Log


// TODO: Rename parameter arguments, choose names that match
Expand Down Expand Up @@ -39,21 +43,26 @@ class JoinCreate : Fragment(){
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val binding = FragmentJoinCreateBinding.inflate(inflater, container, false)

// EditText binding
val input_name = binding.inputName
val input_id = binding.newId
val new_password = binding.inputNewpassword
val check_password = binding.checkPassword

// 버튼 view 바인딩
val binding = FragmentJoinCreateBinding.inflate(inflater, container, false)
binding.nextCreate.setOnClickListener{
//회원 등록
registerUser(input_name.text.toString(), input_id.text.toString(), new_password.text.toString())

// 화면 이동
(activity as JoinActivity).replaceFragment(JoinShare())
}

binding.nextCreate.isEnabled = false
binding.nextCreate.setBackgroundColor(Color.parseColor("#d3d3d3"));

// EditText binding
val input_name = binding.inputName
val input_id = binding.newId
val new_password = binding.inputNewpassword
val check_password = binding.checkPassword


// 핸들러 적용
Expand All @@ -65,7 +74,7 @@ class JoinCreate : Fragment(){
if (input_name.length() > 0 && input_id.length() > 0 && new_password.length() > 0 && check_password.length() > 0){
binding.nextCreate.isEnabled = true
binding.nextCreate.setBackgroundColor(Color.parseColor("#FF9966"));
} else{
} else {
binding.nextCreate.isEnabled = false
binding.nextCreate.setBackgroundColor(Color.parseColor("#d3d3d3"));
}
Expand All @@ -82,9 +91,31 @@ class JoinCreate : Fragment(){
new_password.addTextChangedListener(textWatcher)
check_password.addTextChangedListener(textWatcher)





return binding.root
}

private fun registerUser(userName: String, email: String, password: String){
val call: Call<RegisterRes> = MyApplication.networkServiceAuth.register(
RegisterReq(userName,email, password)
)

call?.enqueue(object : Callback<RegisterRes>{
override fun onResponse(call: Call<RegisterRes>, response: Response<RegisterRes>) {
if(response.isSuccessful){
Log.d("mobileApp", "$response ${response.body()}")
}
}

override fun onFailure(call: Call<RegisterRes>, t: Throwable) {
Log.d("mobileApp", "onFailure $t")
}
})
}

companion object {
/**
* Use this factory method to create a new instance of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class JoinShare : Fragment() {
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

// 반려견 공유 코드를 입력하면 다음으로
val binding = FragmentJoinShareBinding.inflate(inflater, container, false)
binding.nextShare.setOnClickListener {
(activity as JoinActivity).gotoMainActivity()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package com.example.shape_up_2022
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.shape_up_2022.databinding.SignInBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response


class LoginActivity : AppCompatActivity() {

Expand All @@ -19,38 +23,39 @@ class LoginActivity : AppCompatActivity() {
binding.gotoSignUp.setOnClickListener{
val intent = Intent(this, JoinActivity::class.java)
startActivity(intent)
this.finish()
finish()
}

// 로그인 버튼을 눌렀을 때
binding.btnLogin.setOnClickListener{
val inputID = binding.inputId.text.toString() // 아이디에 대한 String
val inputPW = binding.inputPw.text.toString() // 비밀번호에 대한 String

// DB에서 아이디가 있는지 검색, 아이디가 있으면 아이디에 해당하는 비밀번호가 맞는지 검색
if (inputID == "" || inputPW == ""){ // ID or 패스워드를 입력하지 않았을 때
Log.d("app_test", "다시 입력하세요")
binding.reEnterIdPw.visibility = View.VISIBLE
}
else if (inputID == "1234"){ // 해당하는 아이디가 존재할 때
if(inputPW == "1234"){
Log.d("app_test", "로그인 완료")

// intent: mainActivity로 이동
val intent = Intent(
this, MainActivity::class.java)
startActivity(intent)
this.finish()
// 로그인 이력 저장, 다음에는 로그인 창 없이 바로 메인 페이지로 이동하도록 구현
} else{ // 비밀번호가 틀린 경우

// DB에 저장된 아이디 값
val call: Call<LoginRes> = MyApplication.networkServiceAuth.login(
LoginReq(inputID, inputPW)
)

call?.enqueue(object : Callback<LoginRes> {
override fun onResponse(call: Call<LoginRes>, response: Response<LoginRes>) {
if(response.isSuccessful){
Log.d("mobileApp", "$response ${response.body()}")
// 프리퍼런스에 값 저장
SaveSharedPreference.setUserEmail(baseContext, inputID)

// 메인 페이지로 이동
val intent = Intent(baseContext, MainActivity::class.java)
startActivity(intent)
finish()
}
}

override fun onFailure(call: Call<LoginRes>, t: Throwable) {
Log.d("mobileApp", "onFailure $t")
Toast.makeText(baseContext, "아이디 또는 비밀번호를 확인해주세요.", Toast.LENGTH_SHORT).show()
}
}
else{ // 아이디와 비밀번호가 틀린 경우

Log.d("app_test", "로그인 실패 ${inputID}, ${inputPW}")
// 리렌더링 후 아이디와 비밀번호가 틀렸음을 알린다. (모달 띄우는 것보다는 xml에서 알리면 좋을 듯)
}
})

}
//간편 로그인 연동 => api
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@ import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class MyApplication: Application() {
companion object{
companion object {
//유튜브
var networkService2 : NetworkService2
var networkService2: NetworkService2
val retrofit2: Retrofit
get()= Retrofit.Builder()
get() = Retrofit.Builder()
.baseUrl("https://www.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create())
.build()

// 로그인
var networkServiceAuth: NetworkService
val retrofitAuth: Retrofit
get() = Retrofit.Builder()
.baseUrl("http://192.168.219.105:5000")
.addConverterFactory(GsonConverterFactory.create())
.build()
// "http://ec2-13-124-250-65.ap-northeast-2.compute.amazonaws.com:5000/"
init{
networkService2 = retrofit2.create(NetworkService2::class.java)
networkServiceAuth = retrofitAuth.create(NetworkService::class.java)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import com.example.shape_up_2022.databinding.ActivityMyPageBinding
import com.google.android.youtube.player.internal.t
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.lang.Exception

class MyPageActivity : AppCompatActivity() {
Expand Down Expand Up @@ -43,6 +48,36 @@ class MyPageActivity : AppCompatActivity() {
}
}

// 로그아웃
val email = SaveSharedPreference.getUserEmail(this)!!.toString()
Log.d("mobileApp", "${email}")
binding.logoutBtn.setOnClickListener {
val call: Call<LogoutRes> = MyApplication.networkServiceAuth.logout(
)

call?.enqueue(object : Callback<LogoutRes> {
override fun onResponse(call: Call<LogoutRes>, response: Response<LogoutRes>) {
if(response.isSuccessful){
Log.d("mobileApp", "$response ${response.body()}")

// 저장했던 preference clear
SaveSharedPreference.clearUserEmail(baseContext)

// StartActivity로 이동
val intent = Intent(baseContext, TempMainActivity::class.java)
startActivity(intent)
finish()
}
}

override fun onFailure(call: Call<LogoutRes>, t: Throwable) {
Log.d("mobileApp", "onFailure $t")
Toast.makeText(baseContext, "토스트 메세지 띄우기 입니다.", Toast.LENGTH_SHORT).show()
}
})
}

// 프로필 사진 변경
binding.changeprofile.setOnClickListener{
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
intent.type="image/*"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.shape_up_2022

import retrofit2.Call
import retrofit2.http.*

interface NetworkService {
@POST("api/users/register")
fun register(
@Body user: RegisterReq,
): Call<RegisterRes>

@POST("api/users/login")
fun login(
@Body user: LoginReq,
): Call<LoginRes>

@GET("api/users/logout")
fun logout(

): Call<LogoutRes>

}

data class RegisterReq(val name: String, val email: String, val password: String)

data class RegisterRes(val success: String)

data class LoginReq(val email: String, val password: String)

data class LoginRes(val loginSuccess: String, val message: String)

data class LogoutReq(val email: String)

data class LogoutRes(val success: String, val err: String)
Loading

0 comments on commit 40ec89c

Please sign in to comment.