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

๐Ÿ›  oauth ๋กœ๊ทธ์ธ ๊ณ„์ • ์กด์žฌ ์—ฌ๋ถ€ ํŒ๋‹จ/๊ธฐํƒ€ ์ˆ˜์ • ์‚ฌํ•ญ #67

Merged
merged 4 commits into from
Jan 16, 2024
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