Skip to content

Commit

Permalink
[Add] AuthAPI 연결 (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
ReeseSR committed Apr 30, 2022
1 parent affef46 commit 4d7568d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
2 changes: 0 additions & 2 deletions Hyangyu/Hyangyu/Sources/APIModels/Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,3 @@ struct CodeData: Codable {
struct EmailCheckData: Codable {
let message: String
}


44 changes: 39 additions & 5 deletions Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ public class PasswordAPI {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeGetEmailCodeStatus(by: statusCode, data)
let networkResult = self.judgeStatus(by: statusCode, data)
completion(networkResult)

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

func checkCode(completion: @escaping (NetworkResult<Any>) -> Void, email: String, authNum: String) {
courseProvider.request(.checkCode(email: email, authNum: authNum)) { (result) in
switch result {
case .success(let response):
let statusCode = response.statusCode
let data = response.data
let networkResult = self.judgeStatus(by: statusCode, data)
completion(networkResult)

case .failure(let err):
Expand Down Expand Up @@ -67,16 +82,16 @@ public class PasswordAPI {
}
}

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

private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
let decoder = JSONDecoder()
guard let decodedData = try? decoder.decode(GenericResponse<EmailCheckData>.self, from: data) else {
guard let decodedData = try? decoder.decode(GenericResponse<String>.self, from: data)
else {
return .pathErr
}

switch statusCode {
case 200:
return .success(decodedData.data)
return .success(decodedData.message)
case 400..<500:
return .requestErr(decodedData.message)
case 500:
Expand All @@ -85,5 +100,24 @@ public class PasswordAPI {
return .networkFail
}
}

// private func judgeGetEmailCodeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
//
// let decoder = JSONDecoder()
// guard let decodedData = try? decoder.decode(GenericResponse<EmailCheckData>.self, from: data) else {
// return .pathErr
// }
//
// switch statusCode {
// case 200:
// return .success(decodedData.data)
// case 400..<500:
// return .requestErr(decodedData.message)
// case 500:
// return .serverErr
// default:
// return .networkFail
// }
// }
}

10 changes: 10 additions & 0 deletions Hyangyu/Hyangyu/Sources/APIServices/AuthAPI/PasswordService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Moya
enum PasswordService {
case putChangedPassword(email: String, password: String)
case postEmailCode(email: String)
case checkCode(email: String, authNum: String)

}

Expand All @@ -25,6 +26,8 @@ extension PasswordService: TargetType {
return Const.URL.passwordURL
case .postEmailCode(let email):
return Const.URL.findPasswordURL + "/\(email)"
case .checkCode(let email, let authNum):
return Const.URL.checkCodeURL
}
}

Expand All @@ -34,6 +37,8 @@ extension PasswordService: TargetType {
return .put
case .postEmailCode(_):
return .post
case .checkCode(_, _):
return .delete
}
}

Expand All @@ -51,6 +56,11 @@ extension PasswordService: TargetType {
], encoding: JSONEncoding.default)
case .postEmailCode(_):
return .requestPlain
case .checkCode(let email, let authNum):
return .requestParameters(parameters: [
"email": email,
"authNum": authNum
], encoding: JSONEncoding.default)
}
}

Expand Down

0 comments on commit 4d7568d

Please sign in to comment.