diff --git a/Runnect-iOS/Runnect-iOS.xcodeproj/project.pbxproj b/Runnect-iOS/Runnect-iOS.xcodeproj/project.pbxproj index 52b9a2f1..4da9bef8 100644 --- a/Runnect-iOS/Runnect-iOS.xcodeproj/project.pbxproj +++ b/Runnect-iOS/Runnect-iOS.xcodeproj/project.pbxproj @@ -18,6 +18,9 @@ A3BC2F3F2964706100198261 /* UploadedCourseInfoCVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3BC2F3E2964706100198261 /* UploadedCourseInfoCVC.swift */; }; A3BC2F4129667A0D00198261 /* NicknameEditorVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3BC2F4029667A0D00198261 /* NicknameEditorVC.swift */; }; A3BC2F432966A93100198261 /* CourseDetailVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3BC2F422966A93100198261 /* CourseDetailVC.swift */; }; + A3D1A77929CF03D200DD54EC /* AuthRouter.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3D1A77829CF03D200DD54EC /* AuthRouter.swift */; }; + A3D1A77E29CF09B600DD54EC /* SignInResponseDto.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3D1A77D29CF09B600DD54EC /* SignInResponseDto.swift */; }; + A3D1A78029CF142E00DD54EC /* UserManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3D1A77F29CF142E00DD54EC /* UserManager.swift */; }; A3E55BA029C815B10000D85D /* SignInSocialLoginVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3E55B9F29C815B10000D85D /* SignInSocialLoginVC.swift */; }; A3F67AE2296D33AC001598A2 /* MyPageDto.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3F67AE1296D33AC001598A2 /* MyPageDto.swift */; }; A3F67AEA296E4936001598A2 /* ActivityRecordInfoDto.swift in Sources */ = {isa = PBXBuildFile; fileRef = A3F67AE9296E4936001598A2 /* ActivityRecordInfoDto.swift */; }; @@ -119,7 +122,7 @@ CEB8416E2962C45300BF8080 /* LocationSearchResultTVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEB8416D2962C45300BF8080 /* LocationSearchResultTVC.swift */; }; CEB841702963360800BF8080 /* CountDownVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEB8416F2963360800BF8080 /* CountDownVC.swift */; }; CEC2A6852961F92C00160BF7 /* CustomButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC2A6842961F92C00160BF7 /* CustomButton.swift */; }; - CEC2A68729629B9B00160BF7 /* SignInVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC2A68629629B9B00160BF7 /* SignInVC.swift */; }; + CEC2A68729629B9B00160BF7 /* NickNameSetUpVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC2A68629629B9B00160BF7 /* NickNameSetUpVC.swift */; }; CEC2A68A2962ADCD00160BF7 /* RNMapView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC2A6892962ADCD00160BF7 /* RNMapView.swift */; }; CEC2A68C2962AE1B00160BF7 /* RNStartMarker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC2A68B2962AE1B00160BF7 /* RNStartMarker.swift */; }; CEC2A68E2962AF2C00160BF7 /* RNMarker.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC2A68D2962AF2C00160BF7 /* RNMarker.swift */; }; @@ -156,6 +159,9 @@ A3BC2F3E2964706100198261 /* UploadedCourseInfoCVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UploadedCourseInfoCVC.swift; sourceTree = ""; }; A3BC2F4029667A0D00198261 /* NicknameEditorVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NicknameEditorVC.swift; sourceTree = ""; }; A3BC2F422966A93100198261 /* CourseDetailVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CourseDetailVC.swift; sourceTree = ""; }; + A3D1A77829CF03D200DD54EC /* AuthRouter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthRouter.swift; sourceTree = ""; }; + A3D1A77D29CF09B600DD54EC /* SignInResponseDto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInResponseDto.swift; sourceTree = ""; }; + A3D1A77F29CF142E00DD54EC /* UserManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserManager.swift; sourceTree = ""; }; A3E55B9F29C815B10000D85D /* SignInSocialLoginVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInSocialLoginVC.swift; sourceTree = ""; }; A3E55BA529C8AB0A0000D85D /* Runnect-iOS.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = "Runnect-iOS.entitlements"; sourceTree = ""; }; A3F67AE1296D33AC001598A2 /* MyPageDto.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyPageDto.swift; sourceTree = ""; }; @@ -268,7 +274,7 @@ CEB8416D2962C45300BF8080 /* LocationSearchResultTVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocationSearchResultTVC.swift; sourceTree = ""; }; CEB8416F2963360800BF8080 /* CountDownVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CountDownVC.swift; sourceTree = ""; }; CEC2A6842961F92C00160BF7 /* CustomButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomButton.swift; sourceTree = ""; }; - CEC2A68629629B9B00160BF7 /* SignInVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInVC.swift; sourceTree = ""; }; + CEC2A68629629B9B00160BF7 /* NickNameSetUpVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NickNameSetUpVC.swift; sourceTree = ""; }; CEC2A6892962ADCD00160BF7 /* RNMapView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RNMapView.swift; sourceTree = ""; }; CEC2A68B2962AE1B00160BF7 /* RNStartMarker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RNStartMarker.swift; sourceTree = ""; }; CEC2A68D2962AF2C00160BF7 /* RNMarker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RNMarker.swift; sourceTree = ""; }; @@ -361,6 +367,14 @@ path = UploadedCourseInfoCollectionView; sourceTree = ""; }; + A3D1A77C29CF099400DD54EC /* ResponseDto */ = { + isa = PBXGroup; + children = ( + A3D1A77D29CF09B600DD54EC /* SignInResponseDto.swift */, + ); + path = ResponseDto; + sourceTree = ""; + }; CE0C23712966D5FF00B45063 /* ViewPager */ = { isa = PBXGroup; children = ( @@ -444,6 +458,7 @@ CE10063829680C2A00FD31FB /* SignInDto */ = { isa = PBXGroup; children = ( + A3D1A77C29CF099400DD54EC /* ResponseDto */, CE10063929680C5700FD31FB /* .gitkeep */, ); path = SignInDto; @@ -758,6 +773,7 @@ CE14677729658C7200DCEA1B /* Stopwatch.swift */, CE9291282965E01D0010959C /* RNTimeFormatter.swift */, CE3A53C4296C6017003D518C /* KeychainManager.swift */, + A3D1A77F29CF142E00DD54EC /* UserManager.swift */, ); path = RNUtils; sourceTree = ""; @@ -857,6 +873,7 @@ CE6655AD295D7FEA00C64E12 /* Router */ = { isa = PBXGroup; children = ( + A3D1A77829CF03D200DD54EC /* AuthRouter.swift */, CE21C023299E5FE500F62AF5 /* UserRouter.swift */, CE21C025299E5FF300F62AF5 /* CourseRouter.swift */, CE21C027299E5FFC00F62AF5 /* PublicCourseRouter.swift */, @@ -996,7 +1013,7 @@ CE6655C3295D837800C64E12 /* VC */ = { isa = PBXGroup; children = ( - CEC2A68629629B9B00160BF7 /* SignInVC.swift */, + CEC2A68629629B9B00160BF7 /* NickNameSetUpVC.swift */, A3E55B9F29C815B10000D85D /* SignInSocialLoginVC.swift */, ); path = VC; @@ -1255,7 +1272,7 @@ A3BC2F2F2962C40A00198261 /* UploadedCourseInfoVC.swift in Sources */, CE6655EA295D88B200C64E12 /* UITabBar+.swift in Sources */, CE9291272965D0ED0010959C /* StatsInfoView.swift in Sources */, - CEC2A68729629B9B00160BF7 /* SignInVC.swift in Sources */, + CEC2A68729629B9B00160BF7 /* NickNameSetUpVC.swift in Sources */, CE665602295D918000C64E12 /* JsonCoder.swift in Sources */, DA97A033296E65D80086760E /* CourseUploadingRequestDto.swift in Sources */, CE4545CD295D7AF4003201E1 /* TaBarController.swift in Sources */, @@ -1319,10 +1336,13 @@ CE18E890296C76C100FEB569 /* RNLocationModel.swift in Sources */, CE6655DC295D873500C64E12 /* UIButton+.swift in Sources */, CE21C02C299E601000F62AF5 /* ScrapRouter.swift in Sources */, + A3D1A78029CF142E00DD54EC /* UserManager.swift in Sources */, + A3D1A77929CF03D200DD54EC /* AuthRouter.swift in Sources */, CE6655D4295D865B00C64E12 /* Publisher+UIControl.swift in Sources */, CE0D9FD329648DA300CEB5CD /* CustomAlertVC.swift in Sources */, CEB841702963360800BF8080 /* CountDownVC.swift in Sources */, CE320F38296C8FAB009F89A7 /* CourseDrawingResponseData.swift in Sources */, + A3D1A77E29CF09B600DD54EC /* SignInResponseDto.swift in Sources */, CE21C028299E5FFC00F62AF5 /* PublicCourseRouter.swift in Sources */, CE18E894296C79B900FEB569 /* CourseDrawingRequestDto.swift in Sources */, DA20D84E2966A9B300F1581F /* CourseSearchVC.swift in Sources */, diff --git a/Runnect-iOS/Runnect-iOS/Global/Supports/SceneDelegate.swift b/Runnect-iOS/Runnect-iOS/Global/Supports/SceneDelegate.swift index 3080cf84..f2baba80 100644 --- a/Runnect-iOS/Runnect-iOS/Global/Supports/SceneDelegate.swift +++ b/Runnect-iOS/Runnect-iOS/Global/Supports/SceneDelegate.swift @@ -16,7 +16,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { guard let windowScene = (scene as? UIWindowScene) else { return } let window = UIWindow(windowScene: windowScene) - let nav = UINavigationController(rootViewController: SignInSocialLoginVC()) + let nav = UINavigationController(rootViewController: SplashVC()) window.rootViewController = nav self.window = window window.makeKeyAndVisible() diff --git a/Runnect-iOS/Runnect-iOS/Global/Utils/RNUtils/UserManager.swift b/Runnect-iOS/Runnect-iOS/Global/Utils/RNUtils/UserManager.swift new file mode 100644 index 00000000..304d071c --- /dev/null +++ b/Runnect-iOS/Runnect-iOS/Global/Utils/RNUtils/UserManager.swift @@ -0,0 +1,99 @@ +// +// UserManager.swift +// Runnect-iOS +// +// Created by 몽이 누나 on 2023/03/25. +// + +import Foundation + +import Moya + +enum RNError: Error { + case networkFail + case etc +} + +final class UserManager { + static let shared = UserManager() + + private var signInProvider = MoyaProvider( + plugins: [NetworkLoggerPlugin(verbose: true)] + ) + + @UserDefaultWrapper(key: "accessToken") public var accessToken + @UserDefaultWrapper(key: "refreshToken") public var refreshToken + @UserDefaultWrapper(key: "isKakao") public var isKakao + var hasAccessToken: Bool { return self.accessToken != nil } + + private init() {} + + func updateToken(accessToken: String, refreshToken: String, isKakao: Bool) { + self.accessToken = accessToken + self.refreshToken = refreshToken + self.isKakao = isKakao + } + + func signIn(token: String, provider: String, completion: @escaping(Result) -> Void) { + signInProvider.request(.signIn(token: token, provider: provider)) { [weak self] response in + guard let self = self else { return } + switch response { + case .success(let result): + let status = result.statusCode + if 200..<300 ~= status { + do { + let responseDto = try result.map(BaseResponse.self) + guard let data = responseDto.data else { return } + self.accessToken = data.data.accessToken + self.refreshToken = data.data.refreshToken + self.isKakao = provider == "KAKAO" ? true : false + completion(.success(data.data.nickname)) + } catch { + print(error.localizedDescription) + completion(.failure(.networkFail)) + } + } + if status >= 400 { + print("400 error") + completion(.failure(.networkFail)) + } + case .failure(let error): + print(error.localizedDescription) + completion(.failure(.networkFail)) + } + } + } + + func autoSignIn(completion: @escaping(Result) -> Void) { + guard let accessToken = self.accessToken else { return } + guard let isKakao = self.isKakao else { return } + let provider = isKakao ? "KAKAO" : "APPLE" + signInProvider.request(.signIn(token: accessToken, provider: provider)) { [weak self] response in + guard let self = self else { return } + switch response { + case .success(let result): + let status = result.statusCode + if 200..<300 ~= status { + do { + let responseDto = try result.map(BaseResponse.self) + guard let data = responseDto.data else { return } + self.accessToken = data.data.accessToken + self.refreshToken = data.data.refreshToken + self.isKakao = provider == "KAKAO" ? true : false + completion(.success(data.data.nickname)) + } catch { + print(error.localizedDescription) + completion(.failure(.networkFail)) + } + } + if status >= 400 { + print("400 error") + completion(.failure(.networkFail)) + } + case .failure(let error): + print(error.localizedDescription) + completion(.failure(.networkFail)) + } + } + } +} diff --git a/Runnect-iOS/Runnect-iOS/Network/Dto/SignInDto/ResponseDto/SignInResponseDto.swift b/Runnect-iOS/Runnect-iOS/Network/Dto/SignInDto/ResponseDto/SignInResponseDto.swift new file mode 100644 index 00000000..1e36747d --- /dev/null +++ b/Runnect-iOS/Runnect-iOS/Network/Dto/SignInDto/ResponseDto/SignInResponseDto.swift @@ -0,0 +1,25 @@ +// +// SignInResponseDto.swift +// Runnect-iOS +// +// Created by 몽이 누나 on 2023/03/25. +// + +import Foundation + +// MARK: - SignInResponseDto + +struct SignInResponseDto: Codable { + let status: Int + let success: Bool + let message: String + let data: SignInResponseData +} + +// MARK: - SignInResponseData + +struct SignInResponseData: Codable { + let type, nickname, accessToken: String + let email: String? + let refreshToken: String +} diff --git a/Runnect-iOS/Runnect-iOS/Network/Dto/SignInDto/SignInDto.swift b/Runnect-iOS/Runnect-iOS/Network/Dto/SignInDto/SignInDto.swift new file mode 100644 index 00000000..cf62cc15 --- /dev/null +++ b/Runnect-iOS/Runnect-iOS/Network/Dto/SignInDto/SignInDto.swift @@ -0,0 +1,14 @@ +// +// SignInDto.swift +// Runnect-iOS +// +// Created by 몽이 누나 on 2023/03/25. +// + +import Foundation + +// MARK: - SignInDto + +struct SignInDto: Codable { + +} diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/AuthRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/AuthRouter.swift new file mode 100644 index 00000000..e6f3ad40 --- /dev/null +++ b/Runnect-iOS/Runnect-iOS/Network/Router/AuthRouter.swift @@ -0,0 +1,52 @@ +// +// AuthRouter.swift +// Runnect-iOS +// +// Created by 몽이 누나 on 2023/03/25. +// + +import Foundation + +import Moya + +enum AuthRouter { + case signIn(token: String, provider: String) +} + +extension AuthRouter: TargetType { + var baseURL: URL { + guard let url = URL(string: Config.baseURL) else { + fatalError("baseURL could not be configured") + } + + return url + } + + var path: String { + switch self { + case .signIn: + return "/auth" + } + } + + var method: Moya.Method { + switch self { + case .signIn: + return .post + } + } + + var task: Moya.Task { + switch self { + case .signIn(let token, let provider): + return .requestParameters(parameters: ["token": token, "social": provider], encoding: JSONEncoding.default) + } + } + + var headers: [String: String]? { + switch self { + case .signIn: + return Config.headerWithAccessToken + } + } +} diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/CourseRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/CourseRouter.swift index 16712503..2c779635 100644 --- a/Runnect-iOS/Runnect-iOS/Network/Router/CourseRouter.swift +++ b/Runnect-iOS/Runnect-iOS/Network/Router/CourseRouter.swift @@ -92,7 +92,7 @@ extension CourseRouter: TargetType { return ["Content-Type": "multipart/form-data", "machineId": Config.deviceId] default: - return Config.headerWithDeviceId + return Config.headerWithAccessToken } } } diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift index 6e70575d..2bfa41df 100644 --- a/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift +++ b/Runnect-iOS/Runnect-iOS/Network/Router/PublicCourseRouter.swift @@ -59,7 +59,7 @@ extension PublicCourseRouter: TargetType { var headers: [String: String]? { switch self { default: - return Config.headerWithDeviceId + return Config.headerWithAccessToken } } } diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/RecordRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/RecordRouter.swift index a5388fa7..3f896268 100644 --- a/Runnect-iOS/Runnect-iOS/Network/Router/RecordRouter.swift +++ b/Runnect-iOS/Runnect-iOS/Network/Router/RecordRouter.swift @@ -57,7 +57,7 @@ extension RecordRouter: TargetType { var headers: [String: String]? { switch self { case .recordRunning, .getActivityRecordInfo: - return Config.headerWithDeviceId + return Config.headerWithAccessToken } } } diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/ScrapRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/ScrapRouter.swift index f3e30cc4..fc23a636 100644 --- a/Runnect-iOS/Runnect-iOS/Network/Router/ScrapRouter.swift +++ b/Runnect-iOS/Runnect-iOS/Network/Router/ScrapRouter.swift @@ -53,7 +53,7 @@ extension ScrapRouter: TargetType { var headers: [String: String]? { switch self { case .createAndDeleteScrap, .getScrapCourse: - return Config.headerWithDeviceId + return Config.headerWithAccessToken } } } diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/StampRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/StampRouter.swift index f4cbdf30..36b09646 100644 --- a/Runnect-iOS/Runnect-iOS/Network/Router/StampRouter.swift +++ b/Runnect-iOS/Runnect-iOS/Network/Router/StampRouter.swift @@ -46,7 +46,7 @@ extension StampRouter: TargetType { var headers: [String: String]? { switch self { case .getGoalRewardInfo: - return Config.headerWithDeviceId + return Config.headerWithAccessToken } } } diff --git a/Runnect-iOS/Runnect-iOS/Network/Router/UserRouter.swift b/Runnect-iOS/Runnect-iOS/Network/Router/UserRouter.swift index 3d330af3..c820973d 100644 --- a/Runnect-iOS/Runnect-iOS/Network/Router/UserRouter.swift +++ b/Runnect-iOS/Runnect-iOS/Network/Router/UserRouter.swift @@ -54,6 +54,6 @@ extension UserRouter: TargetType { } var headers: [String: String]? { - return Config.headerWithDeviceId + return Config.headerWithAccessToken } } diff --git a/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/SignInVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/NickNameSetUpVC.swift similarity index 96% rename from Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/SignInVC.swift rename to Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/NickNameSetUpVC.swift index ffb4da29..4f51f24f 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/SignInVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/NickNameSetUpVC.swift @@ -1,5 +1,5 @@ // -// SignInVC.swift +// NickNameSetUpVC.swift // Runnect-iOS // // Created by sejin on 2023/01/02. @@ -9,7 +9,7 @@ import UIKit import Moya -final class SignInVC: UIViewController { +final class NickNameSetUpVC: UIViewController { // MARK: - Properties @@ -62,7 +62,7 @@ final class SignInVC: UIViewController { // MARK: - Methods -extension SignInVC { +extension NickNameSetUpVC { private func setDelegate() { self.nicknameTextField.delegate = self } @@ -84,7 +84,7 @@ extension SignInVC { // MARK: - @objc Function -extension SignInVC { +extension NickNameSetUpVC { @objc private func textFieldTextDidChange() { guard let text = nicknameTextField.text else { return } @@ -106,7 +106,7 @@ extension SignInVC { // MARK: - UI & Layout -extension SignInVC { +extension NickNameSetUpVC { private func setUI() { view.backgroundColor = .w1 } @@ -140,7 +140,7 @@ extension SignInVC { // MARK: - UITextFieldDelegate -extension SignInVC: UITextFieldDelegate { +extension NickNameSetUpVC: UITextFieldDelegate { func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true @@ -149,7 +149,7 @@ extension SignInVC: UITextFieldDelegate { // MARK: - Network -extension SignInVC { +extension NickNameSetUpVC { func signIn(nickname: String) { LoadingIndicator.showLoading() userProvider.request(.signUp(nickname: nickname)) { [weak self] response in diff --git a/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/SignInSocialLoginVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/SignInSocialLoginVC.swift index aa2ff75e..aa0afcc4 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/SignInSocialLoginVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/SignIn/VC/SignInSocialLoginVC.swift @@ -9,6 +9,7 @@ import UIKit import SnapKit import Then +import Moya import AuthenticationServices import KakaoSDKAuth import KakaoSDKUser @@ -43,16 +44,7 @@ final class SignInSocialLoginVC: UIViewController { $0.tintColor = .black } - private lazy var appleLoginButton = UIButton(type: .system).then { - $0.setTitle("Apple로 로그인", for: .normal) - $0.titleLabel?.font = .b3 - $0.setTitleColor(.white, for: .normal) - $0.setBackgroundColor(.black, for: .normal) - $0.layer.cornerRadius = 7 - $0.setImage(ImageLiterals.icApple, for: .normal) - $0.imageEdgeInsets = .init(top: 0, left: 0, bottom: 0, right: self.screenWidth * 0.5) - $0.tintColor = .white - } + private let appleLoginButton = ASAuthorizationAppleIDButton() override func viewDidLoad() { super.viewDidLoad() @@ -82,30 +74,44 @@ extension SignInSocialLoginVC { } @objc func kakaoLoginButtonDidTap(_ sender: Any) { - // isKakaoTalkLoginAvailable() : 카톡 설치 되어있으면 true - if (UserApi.isKakaoTalkLoginAvailable()) { //카톡 설치되어있으면 -> 카톡으로 로그인 + // isKakaoTalkLoginAvailable(): 카톡 설치 되어있으면 true + if (UserApi.isKakaoTalkLoginAvailable()) { // 카톡 설치되어있으면 -> 카톡으로 로그인 UserApi.shared.loginWithKakaoTalk {(oauthToken, error) in if let error = error { print(error) } else { print("카카오 톡으로 로그인 성공") - - _ = oauthToken - /// 로그인 관련 메소드 추가 - + guard let oauthToken = oauthToken else { return } + UserManager.shared.signIn(token: oauthToken.accessToken, provider: "KAKAO") { [weak self] result in + switch result { + case .success(let nickname): + print(nickname) + self?.pushToNickNameSetUpVC() + case .failure(let error): + print(error) + self?.showNetworkFailureToast() + } + } } } } else { - // 카톡 없으면 -> 계정으로 로그인 UserApi.shared.loginWithKakaoAccount { (oauthToken, error) in if let error = error { print(error) } else { print("카카오 계정으로 로그인 성공") - - _ = oauthToken - // 관련 메소드 추가 + guard let oauthToken = oauthToken else { return } + UserManager.shared.signIn(token: oauthToken.accessToken, provider: "KAKAO") { [weak self] result in + switch result { + case .success(let nickname): + print(nickname) + self?.pushToNickNameSetUpVC() + case .failure(let error): + print(error) + self?.showNetworkFailureToast() + } + } } } } @@ -119,6 +125,11 @@ extension SignInSocialLoginVC { self.appleLoginButton.addTarget(self, action: #selector(touchUpAppleLoginButton), for: .touchUpInside) self.kakaoLoginButton.addTarget(self, action: #selector(kakaoLoginButtonDidTap), for: .touchUpInside) } + + private func pushToNickNameSetUpVC() { + let nicknameSetUpVC = NickNameSetUpVC() + self.navigationController?.pushViewController(nicknameSetUpVC, animated: true) + } } // MARK: - UI & Layout @@ -173,11 +184,21 @@ extension SignInSocialLoginVC: ASAuthorizationControllerPresentationContextProvi /// 계정 정보 가져오기 let userIdentifier = appleIDCredential.user let idToken = appleIDCredential.identityToken! - let tokeStr = String(data: idToken, encoding: .utf8) + guard let tokeStr = String(data: idToken, encoding: .utf8) else { return } print("User ID : \(userIdentifier)") print("token : \(String(describing: tokeStr))") + UserManager.shared.signIn(token: tokeStr, provider: "APPLE") { [weak self] result in + switch result { + case .success(let nickname): + print(nickname) + self?.pushToNickNameSetUpVC() + case .failure(let error): + print(error) + self?.showNetworkFailureToast() + } + } default: break } diff --git a/Runnect-iOS/Runnect-iOS/Presentation/Splash/VC/SplashVC.swift b/Runnect-iOS/Runnect-iOS/Presentation/Splash/VC/SplashVC.swift index 3230a3b1..9b6c2cd6 100644 --- a/Runnect-iOS/Runnect-iOS/Presentation/Splash/VC/SplashVC.swift +++ b/Runnect-iOS/Runnect-iOS/Presentation/Splash/VC/SplashVC.swift @@ -40,20 +40,25 @@ final class SplashVC: UIViewController { extension SplashVC { private func checkDidSignIn() { DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { - let deviceId = KeychainManager.shared.getDeviceId() - if deviceId.isEmpty { - let deviceIdStoreSuccess = KeychainManager.shared.storeDeviceId() - guard deviceIdStoreSuccess else { return } + if UserManager.shared.hasAccessToken { + UserManager.shared.autoSignIn { [weak self] result in + switch result { + case .success(let nickname): + print(nickname) + self?.pushToTabBarController() + case .failure(let error): + print(error) + self?.pushToSignInView() + } + } + } else { self.pushToSignInView() - return } - - self.pushToTabBarController() } } private func pushToSignInView() { - let signInVC = SignInVC() + let signInVC = SignInSocialLoginVC() self.navigationController?.pushViewController(signInVC, animated: true) }