Skip to content

Commit

Permalink
[cookie/week10] 로그인 API 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
7b7hom committed Jun 30, 2024
1 parent ed6ba2b commit 8952820
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 28 deletions.
4 changes: 4 additions & 0 deletions cookie/cookie_week10/cookie_week10/HomeView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import SwiftUI

struct HomeView : View {

@State var showAlert: Bool = false
@EnvironmentObject var authViewModel: AuthViewModel

var body: some View {
Text("로그인 성공!")
}
Expand Down
13 changes: 6 additions & 7 deletions cookie/cookie_week10/cookie_week10/LogInAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Moya
import Foundation

enum LoginAPI {
case login(id: String, pw: String)
case signup(id: String, pw: String)
case login(id: String, password: String)
case signup(id: String, password: String)
}

extension LoginAPI: TargetType {
Expand Down Expand Up @@ -31,16 +31,16 @@ extension LoginAPI: TargetType {

var task: Task {
switch self {
case let .login(id, pw):
case let .login(id, password):
let parameters: [String: Any] = [
"username": id,
"password": pw
"password": password
]
return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
case .signup(id: let id, pw: let pw):
case .signup(id: let id, password: let password):
let parameters: [String: Any] = [
"username": id,
"password": pw
"password": password
]
return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
}
Expand All @@ -50,4 +50,3 @@ extension LoginAPI: TargetType {
return ["Content-Type": "application/json"]
}
}

124 changes: 103 additions & 21 deletions cookie/cookie_week10/cookie_week10/LogInView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,146 @@ import Moya
struct LogInView: View {

@State var id: String = ""
@State var pw: String = ""

@State var password: String = ""
@State var showAlert: Bool = false
@State var showAutoLoginAlert: Bool = false
@State var alertMessage: String = ""

@EnvironmentObject var authViewModel: AuthViewModel

var body: some View {
VStack (spacing: 0) {
Text("로그인")
.padding(.top, 230)
.padding(.bottom, 20)

TextField("ID", text: $id)
.frame(maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/)
TextField("아이디를 입력하세요.", text: $id)
.frame(maxWidth: .infinity)
.textFieldStyle(.roundedBorder)
.padding(.horizontal, 20)
.padding(.bottom, 20)


SecureField("PW", text: $pw)
.frame(maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/)

SecureField("비밀번호를 입력하세요.", text: $password)
.frame(maxWidth: .infinity)
.textFieldStyle(.roundedBorder)
.padding(.horizontal,20)
.padding(.horizontal, 20)
.padding(.bottom, 20)

Button (action: {

Button(action: {
loginAPI(id: id, password: password, completion: { loginSuccess in
if loginSuccess {
authViewModel.loginSuccess = true
showAlert = true
showAutoLoginAlert = true
} else {
showAlert = true
alertMessage = "로그인 실패"
}
})
}, label: {
Text("Sign Up")
Text("Log In")
.foregroundColor(.white)
.frame(height: 50)
.frame(maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/)
.background(.mint)
.frame(maxWidth: .infinity)
.background(Color.mint)
.cornerRadius(20)
})
.padding(.horizontal, 20)
.padding(.top, 10)


Button (action: {


Button(action: {
signupAPI(id: id, password: password, completion: { statusCode in
if statusCode == 200 {
alertMessage = "회원가입 성공"
} else if statusCode == 400 {
alertMessage = "이미 존재하는 id입니다."
} else if statusCode == 404 {
alertMessage = "네트워크 통신에 실패하였습니다."
}
showAlert = true
})
}, label: {
Text("Log In")
Text("Sign Up")
.foregroundColor(.white)
.frame(height: 50)
.frame(maxWidth: /*@START_MENU_TOKEN@*/.infinity/*@END_MENU_TOKEN@*/)
.background(.mint)
.frame(maxWidth: .infinity)
.background(Color.mint)
.cornerRadius(20)
})
.padding(.horizontal, 20)
.padding(.top, 10)

}
.padding()

Spacer()
.alert(isPresented: $showAlert) {
if showAutoLoginAlert {
return Alert(
title: Text("자동 로그인 활성화"),
message: Text("자동 로그인을 활성화 하시겠습니까?"),
primaryButton: .default(Text("취소").foregroundColor(.blue), action: {
TokenManager.shared.isAutoLogin = false
}),
secondaryButton: .default(Text("확인").foregroundColor(.red), action: {
TokenManager.shared.isAutoLogin = true
})
)
} else {
return Alert(
title: Text(""),
message: Text(alertMessage),
dismissButton: .default(Text("확인"))
)
}
}
}
}

// MARK: Function
let provider = MoyaProvider<LoginAPI>()
func loginAPI(id: String, password: String, completion: @escaping (Bool) -> Void) {
provider.request(.login(id: id, password: password)) { result in
DispatchQueue.main.async {
switch result {
case let .success(response):
if let tokenResponse = try? response.map(TokenResponse.self) {
TokenManager.shared.accessToken = tokenResponse.access_token
TokenManager.shared.refreshToken = tokenResponse.refresh_token
completion(true)
} else {
completion(false)
}
case .failure:
completion(false)
}
}
}
}

func signupAPI(id: String, password: String, completion: @escaping (Int) -> Void) {
provider.request(.signup(id: id, password: password)) { result in
DispatchQueue.main.async {
switch result {
case let .success(response):
if response.statusCode == 200 {
completion(200)
} else {
completion(400)
}
case .failure:
completion(404)
}
}
}
}

struct TokenResponse: Decodable {
var access_token: String
var refresh_token: String
}

#Preview {
LogInView()
}

0 comments on commit 8952820

Please sign in to comment.