Skip to content

Commit

Permalink
[#73] 로그인 API 연동준비
Browse files Browse the repository at this point in the history
  • Loading branch information
00yhsp committed Feb 11, 2024
1 parent 49c4f48 commit 1c5f03c
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 32 deletions.
8 changes: 8 additions & 0 deletions Spon-us.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
DF3AD3E12B61696000DBFCCC /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF3AD3E02B61696000DBFCCC /* LoginView.swift */; };
DF3AD3E32B621FFA00DBFCCC /* SearchPostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF3AD3E22B621FFA00DBFCCC /* SearchPostView.swift */; };
DF498F212B790BD000ADE078 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = DF498F202B790BCF00ADE078 /* GoogleService-Info.plist */; };
DF498F232B791EA800ADE078 /* LoginModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF498F222B791EA800ADE078 /* LoginModel.swift */; };
DF498F252B791EB400ADE078 /* LoginViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF498F242B791EB400ADE078 /* LoginViewModel.swift */; };
DF90A5AB2B64EB5500BC54D0 /* TermsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF90A5AA2B64EB5500BC54D0 /* TermsView.swift */; };
DF90A5AD2B664E5A00BC54D0 /* ProcessingPolicyView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF90A5AC2B664E5A00BC54D0 /* ProcessingPolicyView.swift */; };
DF90A5B02B664E9600BC54D0 /* GatherAndUsageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF90A5AF2B664E9600BC54D0 /* GatherAndUsageView.swift */; };
Expand Down Expand Up @@ -170,6 +172,8 @@
DF3AD3E02B61696000DBFCCC /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
DF3AD3E22B621FFA00DBFCCC /* SearchPostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchPostView.swift; sourceTree = "<group>"; };
DF498F202B790BCF00ADE078 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
DF498F222B791EA800ADE078 /* LoginModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginModel.swift; sourceTree = "<group>"; };
DF498F242B791EB400ADE078 /* LoginViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewModel.swift; sourceTree = "<group>"; };
DF90A5AA2B64EB5500BC54D0 /* TermsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TermsView.swift; sourceTree = "<group>"; };
DF90A5AC2B664E5A00BC54D0 /* ProcessingPolicyView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProcessingPolicyView.swift; sourceTree = "<group>"; };
DF90A5AF2B664E9600BC54D0 /* GatherAndUsageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GatherAndUsageView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -249,6 +253,8 @@
100A1E392B734DDE00AAC1E8 /* EmailViewModel.swift */,
DFB22ED62B766FF3007903DF /* JoinModel.swift */,
DFB22ED82B767003007903DF /* JoinViewModel.swift */,
DF498F222B791EA800ADE078 /* LoginModel.swift */,
DF498F242B791EB400ADE078 /* LoginViewModel.swift */,
);
path = Onboarding;
sourceTree = "<group>";
Expand Down Expand Up @@ -622,6 +628,7 @@
100D38B32B4EF15600498977 /* HomeView.swift in Sources */,
10C8AC632B5C34C400B40547 /* CompanyPostView.swift in Sources */,
DF3AD3DF2B61626300DBFCCC /* OnBoardingView.swift in Sources */,
DF498F232B791EA800ADE078 /* LoginModel.swift in Sources */,
DFB22ED92B767003007903DF /* JoinViewModel.swift in Sources */,
100A1E302B71637A00AAC1E8 /* MyProfileView.swift in Sources */,
3BFC8D1F2B5597C9000D6006 /* ProfileView.swift in Sources */,
Expand Down Expand Up @@ -652,6 +659,7 @@
DF3AD3E12B61696000DBFCCC /* LoginView.swift in Sources */,
3B81BCB82B622F080067E9CB /* MyNoticeListView.swift in Sources */,
3B81BCBA2B622F280067E9CB /* MyNoticeDetailView.swift in Sources */,
DF498F252B791EB400ADE078 /* LoginViewModel.swift in Sources */,
DF90A5B42B664EE700BC54D0 /* ThirdPartyView.swift in Sources */,
109F9E2A2B6E7536006B7D63 /* CouponView.swift in Sources */,
80B41FFE2B77CCA40021DD90 /* NotificationView.swift in Sources */,
Expand Down
16 changes: 16 additions & 0 deletions Spon-us/Model/Onboarding/JoinModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@

import Foundation

enum OrgType {
case student, company
}

enum SubOrgType {
case studentCouncil, studentClub
}

struct JoinRequestBody: Codable {
let name: String
let email: String
let password: String
let organizationType: String
let suborganizationType: String?
}

struct JoinContent: Codable {
let id: Int
let email, name: String
Expand Down
31 changes: 31 additions & 0 deletions Spon-us/Model/Onboarding/LoginModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// LoginModel.swift
// Spon-us
//
// Created by 박현수 on 2/12/24.
//

import Foundation

struct LoginModelContent200: Codable {
let accessToken: String
let refreshToken: String
}

struct LoginModel200: Codable {
let statusCode: String
let message: String
let content: LoginModelContent200
}

struct LoginModel401: Codable {
let statusCode: String
let message: String
let content: String
}

struct LoginRequestBody: Codable {
let email: String
let password: String
let fcmToken: String
}
39 changes: 39 additions & 0 deletions Spon-us/Model/Onboarding/LoginViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// LoginViewModel.swift
// Spon-us
//
// Created by 박현수 on 2/12/24.
//

import Foundation
import Moya

class LoginViewModel: ObservableObject {
@Published var login200: LoginModel200?
@Published var login401: LoginModel401?

private let provider = MoyaProvider<SponusAPI>(plugins: [NetworkLoggerPlugin()])

func postLogin(email: String, password: String, fcmToken: String) {
provider.request(.postLogin(email: email, password: password, fcmToken: fcmToken)) { result in
switch result {
case let .success(response):
do {
if response.statusCode == 200 {
let loginResponse = try response.map(LoginModel200.self)
self.login200 = loginResponse
}
else {
let loginResponse = try response.map(LoginModel401.self)
self.login401 = loginResponse
}
} catch {
print("Error parsing response: \(error)")
}

case let .failure(error):
print("Network request failed: \(error)")
}
}
}
}
20 changes: 10 additions & 10 deletions Spon-us/Spon_usApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ extension AppDelegate: MessagingDelegate {

// 파이어베이스 MessagingDelegate 설정
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
print("Firebase registration token: \(String(describing: fcmToken))")

let dataDict: [String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: dataDict
)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
print("Firebase registration token: \(String(describing: fcmToken))")
let dataDict: [String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: dataDict
)
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
}

Expand Down
36 changes: 20 additions & 16 deletions Spon-us/SponusAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,10 @@ import Foundation
import Moya
import KeychainSwift

enum OrgType {
case student, company
}

enum SubOrgType {
case studentCouncil, studentClub
}

struct JoinRequestBody: Codable {
let name: String
let email: String
let password: String
let organizationType: String
let suborganizationType: String?
}

enum SponusAPI {
case postEmail(email: String)
case postJoin(name: String, email: String, password: String, orgType: OrgType, subOrgType: SubOrgType?)
case postLogin(email: String, password: String, fcmToken: String)
}

extension SponusAPI: TargetType {
Expand All @@ -41,6 +26,8 @@ extension SponusAPI: TargetType {
return "/api/v1/organizations/email"
case .postJoin:
return "/api/v1/organizations/join"
case .postLogin:
return "/api/v1/organizations/login"
}
}

Expand All @@ -50,6 +37,8 @@ extension SponusAPI: TargetType {
return .post
case .postJoin:
return .post
case .postLogin:
return .post
}
}

Expand All @@ -68,6 +57,16 @@ extension SponusAPI: TargetType {
]
]
return try! JSONSerialization.data(withJSONObject: response, options: .prettyPrinted)
case .postLogin:
let response: [String : Any] = [
"statusCode": "200",
"message": "OK",
"content": [
"accessToken": "sfiulghsdkh",
"refreshToken": "alskdjfhaslkjgh"
]
]
return try! JSONSerialization.data(withJSONObject: response, options: .prettyPrinted)
}
}

Expand All @@ -91,6 +90,9 @@ extension SponusAPI: TargetType {
let requestBody = JoinRequestBody(name: name, email: email, password: password, organizationType: "STUDENT", suborganizationType: nil)
return .requestJSONEncodable(requestBody)
}
case .postLogin(let email, let password, let fcmToken):
let requestBody = LoginRequestBody(email: email, password: password, fcmToken: fcmToken)
return .requestJSONEncodable(requestBody)
}
}

Expand All @@ -100,6 +102,8 @@ extension SponusAPI: TargetType {
return nil
case .postJoin:
return nil
case .postLogin:
return nil
/*
case .postLike:
return ["Content-Type": "application/json",
Expand Down
21 changes: 15 additions & 6 deletions Spon-us/View/Onboarding/LoginView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import SwiftUI
import Firebase

struct LoginView: View {
@State var userID = ""
Expand Down Expand Up @@ -92,17 +93,25 @@ struct LoginView: View {
Divider()
.background(.sponusPrimary)
}
// else if (((userID.wholeMatch(of: emailRegexPattern)?.output) != nil) || userID.isEmpty) {
// Divider()
// .background(.sponusGrey200)
// }
// else if (((userID.wholeMatch(of: emailRegexPattern)?.output) != nil) || userID.isEmpty) {
// Divider()
// .background(.sponusGrey200)
// }
else {
Divider().background(.sponusGrey200)
}
}.padding(.horizontal, 20)
.padding(.bottom, 48)
Button {
goToContentView = true
Messaging.messaging().token { token, error in
if let error = error {
print("Error fetching FCM registration token: \(error)")
} else if let token = token {
print("FCM registration token: \(token)")
// 여기서 토큰을 사용하거나 저장합니다.
}
}
//goToContentView = true
} label: {
Text("로그인")
.font(.Body04).foregroundStyle(.sponusWhite)
Expand All @@ -125,7 +134,7 @@ struct LoginView: View {
})
Spacer()
}
}
}
}
}

Expand Down

0 comments on commit 1c5f03c

Please sign in to comment.