Skip to content

Commit

Permalink
[feat] #115 Auto Login
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-j0y committed Mar 27, 2022
1 parent 1e25141 commit 964caf2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/example/infraandroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import androidx.navigation.ui.NavigationUI
import com.example.infraandroid.databinding.ActivityMainBinding

import android.view.View
import android.widget.Toast
import com.google.android.material.snackbar.Snackbar
import com.kakao.sdk.common.util.Utility

class MainActivity : AppCompatActivity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.example.infraandroid.home.view.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import com.example.infraandroid.util.InfraApplication
import com.example.infraandroid.R
Expand Down Expand Up @@ -119,4 +114,6 @@ class HomeFragment : BaseFragment<FragmentHomeBinding>(R.layout.fragment_home) {
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.IntentSender
import android.util.Log
import android.widget.Toast
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.example.infraandroid.BuildConfig
import com.example.infraandroid.util.InfraApplication
import com.example.infraandroid.R
Expand Down Expand Up @@ -40,7 +41,7 @@ class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login
private var showOneTapUI = true

override fun FragmentLoginBinding.onCreateView(){
oneTapClient = Identity.getSignInClient(requireActivity())
// oneTapClient = Identity.getSignInClient(requireActivity())
// signInRequest = BeginSignInRequest.builder()
// .setPasswordRequestOptions(BeginSignInRequest.PasswordRequestOptions.builder()
// .setSupported(true)
Expand All @@ -56,6 +57,46 @@ class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login
// // Automatically sign in when exactly one credential is retrieved.
// .setAutoSelectEnabled(true)
// .build()
if(InfraApplication.prefs.getUserId().isNotEmpty()
and InfraApplication.prefs.getUserPW().isNotEmpty()){

val requestLoginData = RequestLoginData(
userId = InfraApplication.prefs.getUserId(),
userPw = InfraApplication.prefs.getUserPW(),
)

val call: Call<ResponseLoginData> = ServiceCreator.loginService
.postLogin(requestLoginData)

call.enqueue(object : Callback<ResponseLoginData>{
override fun onResponse(
call: Call<ResponseLoginData>,
response: Response<ResponseLoginData>
) {
if(response.isSuccessful){
val code = response.body()?.code
when(code){
1000 -> {
InfraApplication.prefs.setString("jwt", response.body()?.result?.jwt.toString())
InfraApplication.prefs.setString("refreshToken", response.body()?.result?.refreshToken.toString())
InfraApplication.prefs.setString("userId", response.body()?.result?.userId.toString())
InfraApplication.prefs.setString("userNickName", response.body()?.result?.userNickName.toString())
Toast.makeText(requireActivity(),"요청에 성공하셨습니다.", Toast.LENGTH_SHORT).show()
// 로그인 버튼을 누르면 home_fragment로 이동
findNavController().navigate(R.id.action_login_fragment_to_home_fragment)
}
2001 -> {Toast.makeText(requireActivity(),"id가 비어있습니다.", Toast.LENGTH_SHORT).show()}
3014 -> {Toast.makeText(requireActivity(),"없는 아이디거나 비밀번호가 틀렸습니다.", Toast.LENGTH_SHORT).show()}
4000 -> {Toast.makeText(requireActivity(),"데이터베이스 연결에 실패하였습니다.", Toast.LENGTH_SHORT).show()}
}
}
}

override fun onFailure(call: Call<ResponseLoginData>, t: Throwable) {
Log.e("login_server_test", "fail")
}
})
}
}

override fun FragmentLoginBinding.onViewCreated(){
Expand Down Expand Up @@ -87,6 +128,8 @@ class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login
InfraApplication.prefs.setString("refreshToken", response.body()?.result?.refreshToken.toString())
InfraApplication.prefs.setString("userId", response.body()?.result?.userId.toString())
InfraApplication.prefs.setString("userNickName", response.body()?.result?.userNickName.toString())
InfraApplication.prefs.setUserId(response.body()?.result?.userId.toString())
InfraApplication.prefs.setUserPW(inputPw)
Toast.makeText(requireActivity(),"요청에 성공하셨습니다.", Toast.LENGTH_SHORT).show()
// 로그인 버튼을 누르면 home_fragment로 이동
it.findNavController().navigate(R.id.action_login_fragment_to_home_fragment)
Expand Down
25 changes: 23 additions & 2 deletions app/src/main/java/com/example/infraandroid/id/viewmodel/Prefs.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
package com.example.infraandroid.id.viewmodel

import android.content.Context
import android.content.SharedPreferences

class Prefs(context: Context) {
private val prefs=context.getSharedPreferences("prefs_name", Context.MODE_PRIVATE)
private val prefs : SharedPreferences = context.getSharedPreferences("prefs_name", Context.MODE_PRIVATE)
private val editor : SharedPreferences.Editor = prefs.edit()

fun getString(key: String, defValue: String): String {
return prefs.getString(key, defValue).toString()
}
fun setString(key: String, str: String) {
prefs.edit().putString(key, str).apply()
val editor : SharedPreferences.Editor = prefs.edit()
editor.putString(key, str).apply()
}
fun setUserId(input: String){
editor.putString("MY_ID", input)
editor.commit()
}
fun setUserPW(input: String){
editor.putString("MY_PW", input)
editor.commit()
}
fun getUserId():String{
return prefs.getString("MY_ID", "").toString()
}
fun getUserPW():String{
return prefs.getString("MY_PW", "").toString()
}
fun clearUser(){
editor.clear()
editor.commit()
}
}

0 comments on commit 964caf2

Please sign in to comment.