Skip to content

Commit

Permalink
Merge pull request #67 from KCY-Fit-a-Pet/feat/12
Browse files Browse the repository at this point in the history
🛠 oauth 로그인 계정 존재 여부 판단/기타 수정 사항
  • Loading branch information
psychology50 authored Jan 16, 2024
2 parents 612a461 + c4adbe6 commit 9af48cb
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 67 deletions.
Binary file not shown.
1 change: 0 additions & 1 deletion fit-a-pet-client/fit-a-pet-client/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ enum FindIdPwSwitch{

enum OauthInfo{
static var oauthId = ""
// static var phoneNum = ""
static var nonce = ""
static var provider = ""
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
struct PetCareRegistrationManager {
static var shared = PetCareRegistrationManager()

var category: (categoryId: Int, categoryName: String)? {
didSet {
if let category = category {
categoryDictionary = ["categoryId": category.categoryId, "categoryName": category.categoryName]
} else {
categoryDictionary = nil
}
}
}

var careName: String? {
didSet { updateCareDictionary() }
Expand All @@ -21,16 +31,6 @@ struct PetCareRegistrationManager {
private var categoryDictionary: [String: Any]?
private var careDictionary: [String: Any]?

var category: (categoryId: Int, categoryName: String)? {
didSet {
if let category = category {
categoryDictionary = ["categoryId": category.categoryId, "categoryName": category.categoryName]
} else {
categoryDictionary = nil
}
}
}

var categoryDictionaryRepresentation: [String: Any]? {
return categoryDictionary
}
Expand Down
39 changes: 29 additions & 10 deletions fit-a-pet-client/fit-a-pet-client/ViewControllers/FirstVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ extension FirstVC{

@objc func kakaoLoginBtnTapped(_ sender: UIButton){

let mainVC = TabBarController()
mainVC.modalPresentationStyle = .fullScreen

let nextVC = InputPhoneNumVC()

let nonce = CryptoHelpers.randomNonceString()
Expand Down Expand Up @@ -212,8 +215,16 @@ extension FirstVC{
let object = try?JSONSerialization.jsonObject(with: responseData, options: []) as? NSDictionary
guard let jsonObject = object else {return}
print("respose jsonData: \(jsonObject)")
RegistDivision.oauth = true
self.navigationController?.pushViewController(nextVC, animated: false)
if let userData = jsonObject["data"] as? [String: Any], let userId = userData["userId"] as? Int {
print("User ID: \(userId)")
UserDefaults.standard.set(userId, forKey: "id")

self.present(mainVC, animated: false, completion: nil)
} else {

RegistDivision.oauth = true
self.navigationController?.pushViewController(nextVC, animated: false)
}
}
case .failure(let error):
// Handle failure
Expand All @@ -226,10 +237,11 @@ extension FirstVC{
}
}
@objc func googleLoginBtnTapped(_ sender: UIButton){
let nonce = CryptoHelpers.randomNonceString()
print("nonce 값: \(nonce)")
let hashedString = CryptoHelpers.sha256(nonce)
print("hashedString 값: \(hashedString)")

let mainVC = TabBarController()
mainVC.modalPresentationStyle = .fullScreen

let nextVC = InputPhoneNumVC()

GIDSignIn.sharedInstance.signIn(
withPresenting: self) { signInResult, error in
Expand Down Expand Up @@ -258,11 +270,8 @@ extension FirstVC{
print("idToken: \(idToken)")

OauthInfo.provider = "google"
OauthInfo.nonce = hashedString
OauthInfo.oauthId = userId!

//KeychainHelper.saveTempToken(tempToken: idToken)

AnonymousAlamofire.shared.oauthLogin(){
result in
switch result {
Expand All @@ -273,7 +282,17 @@ extension FirstVC{
let object = try?JSONSerialization.jsonObject(with: responseData, options: []) as? NSDictionary
guard let jsonObject = object else {return}
print("respose jsonData: \(jsonObject)")
RegistDivision.oauth = true

if let userData = jsonObject["data"] as? [String: Any], let userId = userData["userId"] as? Int {
print("User ID: \(userId)")
UserDefaults.standard.set(userId, forKey: "id")

self.present(mainVC, animated: false, completion: nil)
} else {

RegistDivision.oauth = true
self.navigationController?.pushViewController(nextVC, animated: false)
}
}
case .failure(let error):
// Handle failure
Expand Down
36 changes: 1 addition & 35 deletions fit-a-pet-client/fit-a-pet-client/ViewControllers/LoginVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ class LoginVC: UIViewController{
print("userId: \(userId)")
UserDefaults.standard.set(userId, forKey: "id")

self?.fetchUserProfileInfo(completion: {
self?.present(mainVC, animated: false, completion: nil)
})
self?.present(mainVC, animated: false, completion: nil)
}
}
} catch {
Expand All @@ -192,38 +190,6 @@ class LoginVC: UIViewController{
}
}
}

func fetchUserProfileInfo(completion: @escaping () -> Void) {

AuthorizationAlamofire.shared.userProfileInfo { userProfileResult in
switch userProfileResult {
case .success(let data):
if let responseData = data {
do {
let jsonObject = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any] ?? [:]

if let dataDict = jsonObject["data"] as? [String: Any],
let memberDict = dataDict["member"] as? [String: Any] {

for (key, value) in memberDict {
UserDefaults.standard.set(value, forKey: key)
}

UserDefaults.standard.synchronize()
}
print("Response JSON Data: \(jsonObject)")
} catch {
print("Error parsing user profile JSON: \(error)")
}
}

case .failure(let profileError):
print("Error fetching user profile info: \(profileError)")
}

completion()
}
}

@objc func changeFindIdVC(_ sender: UIButton){
FindIdPwSwitch.findAuth = "아이디 찾기"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,14 @@ class MainVC: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

// self.navigationItem.title = "반려동물 등록하기"

petCollectionView.delegate = self
petCollectionView.dataSource = self

layoutScrollView.delegate = self

initView()

// if let cookies = HTTPCookieStorage.shared.cookies {
// for cookie in cookies {
// print("Cookie description: \(cookie.description)")
// // You can access other properties of the cookie here
// }
// }

fetchUserProfileInfo()

}
private func initView(){
Expand Down Expand Up @@ -140,8 +133,37 @@ class MainVC: UIViewController {
let nextVC = InputSpeciesVC(title: "반려동물 등록하기")
nextVC.hidesBottomBarWhenPushed = true
self.navigationController?.pushViewController(nextVC, animated: false)


}

func fetchUserProfileInfo() {

AuthorizationAlamofire.shared.userProfileInfo { userProfileResult in
switch userProfileResult {
case .success(let data):
if let responseData = data {
do {
let jsonObject = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any] ?? [:]

if let dataDict = jsonObject["data"] as? [String: Any],
let memberDict = dataDict["member"] as? [String: Any] {

for (key, value) in memberDict {
UserDefaults.standard.set(value, forKey: key)
}

UserDefaults.standard.synchronize()
}
print("Response JSON Data: \(jsonObject)")
} catch {
print("Error parsing user profile JSON: \(error)")
}
}

case .failure(let profileError):
print("Error fetching user profile info: \(profileError)")
}

}
}
}

Expand Down

0 comments on commit 9af48cb

Please sign in to comment.