Skip to content

Commit

Permalink
fix: CrewVIew 수정까지
Browse files Browse the repository at this point in the history
  • Loading branch information
guesswb committed Aug 7, 2023
1 parent c0a600f commit a87933f
Show file tree
Hide file tree
Showing 88 changed files with 1,704 additions and 921 deletions.
184 changes: 110 additions & 74 deletions RunningCrew.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

File renamed without changes.
File renamed without changes.
20 changes: 20 additions & 0 deletions RunningCrew/Sources/Error/TokenError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// TokenError.swift
// RunningCrew
//
// Created by 김기훈 on 2023/07/26.
//

import Foundation

enum TokenError: Error {
case getToken
}

extension TokenError: LocalizedError {
public var errorDescription: String? {
switch self {
case .getToken: return "토큰을 받지 못했습니다."
}
}
}
9 changes: 9 additions & 0 deletions RunningCrew/Sources/Repository/KeyChainRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ final class KeyChainRepository {

return SecItemUpdate(updateQuery as CFDictionary, attributes as CFDictionary) == errSecSuccess
}

func deleteToken(key: String) -> Bool {
let deleteQuery: [CFString: Any] = [
kSecClass: kSecClassKey,
kSecAttrApplicationTag: key
]

return SecItemDelete(deleteQuery as CFDictionary) == errSecSuccess
}
}
8 changes: 8 additions & 0 deletions RunningCrew/Sources/Repository/UserDefaultRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// UserDefaultRepository.swift
// RunningCrew
//
// Created by 김기훈 on 2023/08/07.
//

import Foundation
78 changes: 28 additions & 50 deletions RunningCrew/Sources/Repository/UserRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,10 @@ import Foundation
import Moya
import RxSwift

enum UserAPI {
case logIn(body: SocialLogInRequestModel)
case signUp(token: String)
case isLogIn(accessToken: String)
}

extension UserAPI: TargetType {
var baseURL: URL {
switch self {
case .logIn, .signUp, .isLogIn:
return URL(string: "https://runningcrew-test.ddns.net")!
}
}

var path: String {
switch self {
case .logIn: return "/api/login/oauth"
case .signUp: return "/api/signup"
case .isLogIn: return "/api/me"
}
}

var method: Moya.Method {
switch self {
case .logIn: return .post
case .signUp: return .post
case .isLogIn: return .get
}
}

var task: Moya.Task {
switch self {
case .logIn(let body): return .requestJSONEncodable(body)
case .signUp: return .requestPlain
case .isLogIn: return .requestPlain
}
}

var headers: [String: String]? {
switch self {
case .logIn: return nil
case .signUp(let token): return ["Authorization": "Bearer \(token)"]
case .isLogIn(let accessToken): return ["Authorization": "Bearer \(accessToken)"]
}
}
}

final class UserRepository {
private var userProvider = MoyaProvider<UserAPI>()

func logIn(body: SocialLogInRequestModel) -> Observable<Data> {
func logIn(body: SocialLogInRequest) -> Observable<Data> {
return userProvider.rx.request(.logIn(body: body))
.map { response -> Data in
if 400..<500 ~= response.statusCode {
Expand All @@ -73,8 +26,22 @@ final class UserRepository {
.asObservable()
}

func isLogIn(accessToken: String) -> Observable<Data> {
return userProvider.rx.request(.isLogIn(accessToken: accessToken))
func logInUserInformation(accessToken: String) -> Observable<Data> {
return userProvider.rx.request(.logInUserInformation(accessToken: accessToken))
.map { response -> Data in
if 400..<500 ~= response.statusCode {
throw NetworkError.client
} else if 500..<600 ~= response.statusCode {
throw NetworkError.server
}

return response.data
}
.asObservable()
}

func signUp(accessToken: String, name: String, nickName: String, dongId: Int, birthday: String, sex: String, height: Int, weight: Int) -> Observable<Data> {
return userProvider.rx.request(.signUp(accessToken: accessToken, name: name, nickName: nickName, dongId: dongId, birthday: birthday, sex: sex, height: height, weight: weight))
.map { response -> Data in
if 400..<500 ~= response.statusCode {
throw NetworkError.client
Expand All @@ -86,4 +53,15 @@ final class UserRepository {
}
.asObservable()
}

func deleteUser(accessToken: String, userID: String) -> Observable<Bool> {
return userProvider.rx.request(.deleteUser(accessToken: accessToken, userID: userID))
.map { response -> Bool in
if 400..<600 ~= response.statusCode {
return false
}
return true
}
.asObservable()
}
}
63 changes: 48 additions & 15 deletions RunningCrew/Sources/Service/LogInService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,78 @@ import Foundation
import RxSwift

final class LogInService {
private let logInRepository: UserRepository
private let userRepository: UserRepository
private let keyChainRepository: KeyChainRepository = KeyChainRepository.shared

init(logInRepository: UserRepository) {
self.logInRepository = logInRepository
init(userRepository: UserRepository) {
self.userRepository = userRepository
}
}

extension LogInService {
func logIn(accessToken: String = "", idToken: String = "", origin: String) -> Observable<SocialLogInResponseModel> {
func logIn(accessToken: String = "", idToken: String = "", origin: String) -> Observable<SocialLogInResponse> {
guard let fcmToken = try? keyChainRepository.readToken(key: "fcmToken") else {
return Observable.error(KeyChainError.readToken)
}
let model = SocialLogInRequestModel(fcmToken: fcmToken, accessToken: accessToken, idToken: idToken, origin: origin)
let model = SocialLogInRequest(fcmToken: fcmToken, accessToken: accessToken, idToken: idToken, origin: origin)

return logInRepository.logIn(body: model)
.map { try JSONDecoder().decode(SocialLogInResponseModel.self, from: $0) }
return userRepository.logIn(body: model)
.map { try JSONDecoder().decode(SocialLogInResponse.self, from: $0) }
.withUnretained(self)
.map { (owner, response) in
if owner.keyChainRepository.saveToken(key: "accessToken", value: response.accessToken) == false {
_ = owner.keyChainRepository.updateToken(key: "accessToken", newValue: response.accessToken)
}
if owner.keyChainRepository.saveToken(key: "refreshToken", value: response.refreshToken) == false {
_ = owner.keyChainRepository.updateToken(key: "refreshToken", newValue: response.refreshToken)
}

return response
}
}

func isLogIn() -> Bool {
guard let accessToken = try? KeyChainRepository.shared.readToken(key: "accessToken") else {
guard (try? keyChainRepository.readToken(key: "accessToken")) != nil else {
return false
}
//accessToken으로 인증
return true
}

func logOut() -> Bool {
if keyChainRepository.deleteToken(key: "accessToken") {
return keyChainRepository.deleteToken(key: "refreshToken")
}
return false
}

func getUserData() -> Observable<User> {
guard let accessToken = try? KeyChainRepository.shared.readToken(key: "accessToken") else {
func getUserData() -> Observable<Me> {
guard let accessToken = try? keyChainRepository.readToken(key: "accessToken") else {
return Observable.error(KeyChainError.readToken)
}

return logInRepository.isLogIn(accessToken: accessToken)
return userRepository.logInUserInformation(accessToken: accessToken)
.map { try JSONDecoder().decode(Me.self, from: $0) }
}

func signUp(accessToken: String, name: String, nickName: String, dongId: Int, birthday: String, sex: String, height: Int, weight: Int) -> Observable<User> {
return userRepository.signUp(accessToken: accessToken, name: name, nickName: nickName, dongId: dongId, birthday: birthday, sex: sex, height: height, weight: weight)
.map { try JSONDecoder().decode(User.self, from: $0) }
}

func storeToken(key: String, value: String) {
if keyChainRepository.saveToken(key: key, value: value) == false {
_ = keyChainRepository.updateToken(key: key, newValue: value)
func deleteUser() -> Observable<Bool> {
guard let accessToken = try? keyChainRepository.readToken(key: "accessToken") else {
return Observable.error(KeyChainError.readToken)
}

return getUserData()
.withUnretained(self)
.flatMap { (owner, me) in owner.userRepository.deleteUser(accessToken: accessToken, userID: "\(me.id)") }
.withUnretained(self)
.map { (owner, result) in
if result {
_ = owner.logOut()
}
return result
}
}
}
22 changes: 22 additions & 0 deletions RunningCrew/Sources/Service/Model/CrewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// CrewModel.swift
// RunningCrew
//
// Created by 김기훈 on 2023/08/07.
//

import Foundation

struct Crew: Codable, Hashable {
let id: Int
let createdDate, name, introduction, crewImgURL: String
let dong: String
let memberCount: Int
let joinApply, joinQuestion: Bool

enum CodingKeys: String, CodingKey {
case id, createdDate, name, introduction
case crewImgURL = "crewImgUrl"
case dong, memberCount, joinApply, joinQuestion
}
}
18 changes: 18 additions & 0 deletions RunningCrew/Sources/Service/Model/Me.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Me.swift
// RunningCrew
//
// Created by 김기훈 on 2023/08/07.
//

import Foundation

struct Me: Codable {
let id: Int
let email, name, nickname, imgURL: String

enum CodingKeys: String, CodingKey {
case id, email, name, nickname
case imgURL = "imgUrl"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// RunningNoticeModel.swift
// RunningNotice.swift
// RunningCrew
//
// Created by Kim TaeSoo on 2023/06/04.
Expand All @@ -24,7 +24,6 @@ struct Member: Codable {
let user: Profile
}

// MARK: - User
struct Profile: Codable {
let id: Int
let nickname, imgURL: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//
// LogInModel.swift
// SocialLogIn.swift
// RunningCrew
//
// Created by 김기훈 on 2023/07/24.
//

import Foundation

struct SocialLogInRequestModel: Encodable {
struct SocialLogInRequest: Encodable {
let fcmToken: String
let accessToken: String
let idToken: String
let origin: String
}

struct SocialLogInResponseModel: Decodable {
struct SocialLogInResponse: Decodable {
let accessToken: String
let refreshToken: String
let initData: Bool
Expand All @@ -25,11 +25,3 @@ struct SocialLogInResponseModel: Decodable {
case initData
}
}

struct User: Codable {
let id: Int
let email: String
let name: String
let nickname: String
let imgUrl: String
}
22 changes: 22 additions & 0 deletions RunningCrew/Sources/Service/Model/User.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// User.swift
// RunningCrew
//
// Created by 김기훈 on 2023/08/07.
//

import Foundation

struct User: Codable {
let id: Int
let email, name, nickname, imgURL: String
let loginType, phoneNumber, dongArea, sex: String
let birthday: String
let height, weight: Int

enum CodingKeys: String, CodingKey {
case id, email, name, nickname
case imgURL = "imgUrl"
case loginType, phoneNumber, dongArea, sex, birthday, height, weight
}
}
12 changes: 0 additions & 12 deletions RunningCrew/Sources/Service/SignUpService.swift

This file was deleted.

15 changes: 0 additions & 15 deletions RunningCrew/Sources/Utils/UIView+.swift

This file was deleted.

2 changes: 1 addition & 1 deletion RunningCrew/Sources/View/Alarm/AlarmViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class AlarmViewController: BaseViewController {

override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
// view.backgroundColor = .white
setNavigationBar()
addView()
setConstraint()
Expand Down
Loading

0 comments on commit a87933f

Please sign in to comment.