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

[Feat] #102 - 소셜로그인 회원가입 서버 구현 #111

Merged
merged 2 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Spark-iOS/Spark-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
F86C68B927958AA9009A5296 /* UIImageView+.swift in Sources */ = {isa = PBXBuildFile; fileRef = F86C68B827958AA9009A5296 /* UIImageView+.swift */; };
F8C14A9B2795094800498687 /* HomeEmptyCVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8C14A992795094800498687 /* HomeEmptyCVC.swift */; };
F8C14A9C2795094800498687 /* HomeEmptyCVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = F8C14A9A2795094800498687 /* HomeEmptyCVC.xib */; };
F8E3496827969B61001B67E7 /* AuthAPI.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8E3496727969B61001B67E7 /* AuthAPI.swift */; };
F8E3496A27969B67001B67E7 /* AuthService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8E3496927969B67001B67E7 /* AuthService.swift */; };
F8E3496D2796B7F6001B67E7 /* Signup.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8E3496C2796B7F6001B67E7 /* Signup.swift */; };
F8FAA9722790344D00C4190F /* HomeHabitCVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FAA9702790344D00C4190F /* HomeHabitCVC.swift */; };
F8FAA9732790344D00C4190F /* HomeHabitCVC.xib in Resources */ = {isa = PBXBuildFile; fileRef = F8FAA9712790344D00C4190F /* HomeHabitCVC.xib */; };
F8FAA9762790346000C4190F /* HomeWaitingCVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = F8FAA9742790346000C4190F /* HomeWaitingCVC.swift */; };
Expand Down Expand Up @@ -241,6 +244,9 @@
F86C68B827958AA9009A5296 /* UIImageView+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImageView+.swift"; sourceTree = "<group>"; };
F8C14A992795094800498687 /* HomeEmptyCVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeEmptyCVC.swift; sourceTree = "<group>"; };
F8C14A9A2795094800498687 /* HomeEmptyCVC.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomeEmptyCVC.xib; sourceTree = "<group>"; };
F8E3496727969B61001B67E7 /* AuthAPI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthAPI.swift; sourceTree = "<group>"; };
F8E3496927969B67001B67E7 /* AuthService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthService.swift; sourceTree = "<group>"; };
F8E3496C2796B7F6001B67E7 /* Signup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Signup.swift; sourceTree = "<group>"; };
F8FAA9702790344D00C4190F /* HomeHabitCVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeHabitCVC.swift; sourceTree = "<group>"; };
F8FAA9712790344D00C4190F /* HomeHabitCVC.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomeHabitCVC.xib; sourceTree = "<group>"; };
F8FAA9742790346000C4190F /* HomeWaitingCVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HomeWaitingCVC.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -718,6 +724,7 @@
isa = PBXGroup;
children = (
F82F58052792897B003E4174 /* Plugin */,
F8E3496627969B4F001B67E7 /* Auth */,
F82F5806279289AC003E4174 /* Home */,
2BF3AB42279475B9001A9042 /* Room */,
2BBED13127956D040052CA5C /* Feed */,
Expand All @@ -731,6 +738,7 @@
children = (
F82F57FA27928437003E4174 /* NetworkResult.swift */,
F82F57FC2792847D003E4174 /* GenericResponse.swift */,
F8E3496B2796B7DF001B67E7 /* Auth */,
F82F5800279287EC003E4174 /* Home */,
2BF3AB4A27947C3A001A9042 /* Room */,
2BBED12E27956C4F0052CA5C /* Feed */,
Expand Down Expand Up @@ -844,6 +852,23 @@
path = Home;
sourceTree = "<group>";
};
F8E3496627969B4F001B67E7 /* Auth */ = {
isa = PBXGroup;
children = (
F8E3496727969B61001B67E7 /* AuthAPI.swift */,
F8E3496927969B67001B67E7 /* AuthService.swift */,
);
path = Auth;
sourceTree = "<group>";
};
F8E3496B2796B7DF001B67E7 /* Auth */ = {
isa = PBXGroup;
children = (
F8E3496C2796B7F6001B67E7 /* Signup.swift */,
);
path = Auth;
sourceTree = "<group>";
};
F8FAA96B2790336E00C4190F /* Home */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -1043,6 +1068,7 @@
2BBED13527956EAB0052CA5C /* FeedAPI.swift in Sources */,
F80A3E55278C1C1F00728E07 /* StorageVC.swift in Sources */,
F82F580C27929F74003E4174 /* Header.swift in Sources */,
F8E3496827969B61001B67E7 /* AuthAPI.swift in Sources */,
F8096F3C2784211D00B71D38 /* UIColor+.swift in Sources */,
2BE5D818279330A6007A544D /* TimerAuthView.swift in Sources */,
2BBED12D279539B90052CA5C /* WaitingMember.swift in Sources */,
Expand Down Expand Up @@ -1089,6 +1115,8 @@
F82F580427928975003E4174 /* MoyaLoggerPlugin.swift in Sources */,
F8096F2927841F1E00B71D38 /* Const.swift in Sources */,
F86C68B927958AA9009A5296 /* UIImageView+.swift in Sources */,
F8E3496D2796B7F6001B67E7 /* Signup.swift in Sources */,
F8E3496A27969B67001B67E7 /* AuthService.swift in Sources */,
F8096F3027841F9200B71D38 /* HomeVC.swift in Sources */,
F82F5808279289B2003E4174 /* HomeAPI.swift in Sources */,
EBA93A7F279080E2009AE771 /* JoinCheckVC.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ extension Const {
static let isOnboarding = "isOnboarding"
static let userID = "userID"
static let accessToken = "accessToekn"
static let fcmToken = "fcmToken"
}
}
10 changes: 1 addition & 9 deletions Spark-iOS/Spark-iOS/Source/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,6 @@ extension AppDelegate: MessagingDelegate {
/// 현재 등록 토큰 가져오기.
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {

// TODO: - 디바이스 토큰을 보내는 서버통신 구현

sendDeviceTokenWithAPI(fcmToken: fcmToken ?? "")
}
}

extension AppDelegate {
private func sendDeviceTokenWithAPI(fcmToken: String) {

UserDefaults.standard.set(fcmToken, forKey: Const.UserDefaultsKey.fcmToken)
}
}
27 changes: 27 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkModels/Auth/Signup.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Signup.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/18.
//

import Foundation
import UIKit

// MARK: - DataClass
struct Signup: Codable {
let user: User
let accesstoken: String
}

// MARK: - User
struct User: Codable {
let userID: Int
let nickname: String
let profileImg: String

enum CodingKeys: String, CodingKey {
case userID = "userId"
case nickname, profileImg
}
}
53 changes: 53 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkServices/Auth/AuthAPI.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// AuthAPI.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/18.
//

import Foundation
import Moya

public class AuthAPI {

static let shared = AuthAPI()
var userProvider = MoyaProvider<AuthService>(plugins: [MoyaLoggerPlugin()])

public init() { }

func signup(socailID: String, profileImg: UIImage, nickname: String, fcmToken: String, completion: @escaping (NetworkResult<Any>) -> Void) {
userProvider.request(.signup(socialId: socailID, profileImg: profileImg, nickname: nickname, fcmToken: fcmToken)) { result in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

토큰도 스트링으로 보내면 되는거군뇨!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넹!

switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data

let networkResult = self.judgeSignupStatus(by: statusCode, data)
completion(networkResult)

case .failure(let err):
print(err)
}
}
}

private func judgeSignupStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {

let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<Signup>.self, from: data)
else {
return .pathErr
}

switch statusCode {
case 200:
return .success(decodedData.data ?? "None-Data")
case 400..<500:
return .requestErr(decodedData.message)
case 500:
return .serverErr
default:
return .networkFail
}
}
}
59 changes: 59 additions & 0 deletions Spark-iOS/Spark-iOS/Source/NetworkServices/Auth/AuthService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// AuthService.swift
// Spark-iOS
//
// Created by kimhyungyu on 2022/01/18.
//

import Foundation
import Moya

enum AuthService {
case signup(socialId: String, profileImg: UIImage, nickname: String, fcmToken: String)
}

extension AuthService: TargetType {
var baseURL: URL {
return URL(string: Const.URL.baseURL)!
}

var path: String {
switch self {
case .signup:
return "/auth/signup"
}
}

var method: Moya.Method {
switch self {
case .signup:
return .post
}
}

var task: Task {
switch self {
case .signup(let socialID, let profileImg, let nickname, let fcmToken):
var multiPartData: [Moya.MultipartFormData] = []

let socialIDData = socialID.data(using: .utf8) ?? Data()
multiPartData.append(MultipartFormData(provider: .data(socialIDData), name: "socialId"))
let nicknameData = nickname.data(using: .utf8) ?? Data()
multiPartData.append(MultipartFormData(provider: .data(nicknameData), name: "nickname"))
let fcmTokenData = fcmToken.data(using: .utf8) ?? Data()
multiPartData.append(MultipartFormData(provider: .data(fcmTokenData), name: "fcmToken"))

let profileImgData = MultipartFormData(provider: .data(profileImg.pngData() ?? Data()), name: "image", fileName: "image", mimeType: "image/png")
multiPartData.append(profileImgData)

return .uploadMultipart(multiPartData)
}
}

var headers: [String: String]? {
switch self {
case .signup:
return Const.Header.authrizationHeader
}
}
}