Skip to content

Commit

Permalink
Merge pull request #23 from tukcomCD2024/RINGO-61-login-service
Browse files Browse the repository at this point in the history
Ringo-61 login service
  • Loading branch information
hhyukjin authored Feb 7, 2024
2 parents 33553fe + 87317fb commit 014a7dd
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 3 deletions.
Binary file modified iOS/Ringo/.DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions iOS/Ringo/Ringo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
94470A8A2B71050700F0A942 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 94470A892B71050700F0A942 /* Alamofire */; };
94470A8D2B710A3100F0A942 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = 94470A8C2B710A3100F0A942 /* Starscream */; };
94470A902B710CE500F0A942 /* WebRTC in Frameworks */ = {isa = PBXBuildFile; productRef = 94470A8F2B710CE500F0A942 /* WebRTC */; };
94470A932B7163E900F0A942 /* Model.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94470A922B7163E900F0A942 /* Model.swift */; };
94470A952B71680100F0A942 /* SigninService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94470A942B71680100F0A942 /* SigninService.swift */; };
94470A972B717DC300F0A942 /* ConnectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 94470A962B717DC300F0A942 /* ConnectionViewController.swift */; };
945603132B6AC07D002F4B33 /* TabBarViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945603122B6AC07D002F4B33 /* TabBarViewController.swift */; };
945603152B6AC22A002F4B33 /* ContactsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 945603142B6AC22A002F4B33 /* ContactsViewController.swift */; };
Expand All @@ -28,6 +30,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
94470A922B7163E900F0A942 /* Model.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Model.swift; sourceTree = "<group>"; };
94470A942B71680100F0A942 /* SigninService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SigninService.swift; sourceTree = "<group>"; };
940C25632B73D20900E069D0 /* ConnectionCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionCollectionViewCell.swift; sourceTree = "<group>"; };
94470A962B717DC300F0A942 /* ConnectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectionViewController.swift; sourceTree = "<group>"; };
945603122B6AC07D002F4B33 /* TabBarViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -98,6 +102,8 @@
945CF97C2B67E1CD00396E4E /* SceneDelegate.swift */,
940C25632B73D20900E069D0 /* ConnectionCollectionViewCell.swift */,
945603222B6D31D9002F4B33 /* ContactsTableViewCell.swift */,
94470A922B7163E900F0A942 /* Model.swift */,
94470A942B71680100F0A942 /* SigninService.swift */,
94470A962B717DC300F0A942 /* ConnectionViewController.swift */,
945603202B6D2F06002F4B33 /* Canvas.swift */,
945CF9832B67E1CE00396E4E /* Assets.xcassets */,
Expand Down Expand Up @@ -199,8 +205,10 @@
945603172B6AC2D3002F4B33 /* RecentsViewController.swift in Sources */,
945603232B6D31D9002F4B33 /* ContactsTableViewCell.swift in Sources */,
945CF97D2B67E1CD00396E4E /* SceneDelegate.swift in Sources */,
94470A932B7163E900F0A942 /* Model.swift in Sources */,
945603152B6AC22A002F4B33 /* ContactsViewController.swift in Sources */,
9456031D2B6BF44A002F4B33 /* FriendRequestViewController.swift in Sources */,
94470A952B71680100F0A942 /* SigninService.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
29 changes: 29 additions & 0 deletions iOS/Ringo/Ringo/Model.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// Model.swift
// Ringo
//
// Created by 강진혁 on 2/6/24.
//

import Foundation

struct SigninData: Codable {
let id: Int
let eamil: String
let jwtToken: String
let refreshToken: String
}

struct SigninDataResponse: Codable {
let data: SigninData?
let message: String
let result: String
}

enum NetworkResult<T> {
case success(T)
case requestErr(T)
case pathErr
case serverErr
case networkFail
}
2 changes: 1 addition & 1 deletion iOS/Ringo/Ringo/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {

guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
let mainViewController = ConnectionViewController()
let mainViewController = ViewController()

window?.rootViewController = mainViewController
window?.makeKeyAndVisible()
Expand Down
4 changes: 4 additions & 0 deletions iOS/Ringo/Ringo/Screens/TabBarViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class TabBarViewController: UITabBarController{
override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = .systemBackground

tabBar.backgroundColor = .init(white: 1, alpha: 0.8)

let contactsVC = UINavigationController(rootViewController: ContactsViewController())
Expand All @@ -22,6 +24,8 @@ class TabBarViewController: UITabBarController{
contactsVC.tabBarItem = UITabBarItem(title: "Contacts", image: UIImage(systemName: "person.3"), tag: 1)
recentsVC.tabBarItem = UITabBarItem(title: "Recents", image: UIImage(systemName: "clock"), tag: 2)
accountVC.tabBarItem = UITabBarItem(title: "Account", image: UIImage(systemName: "person.text.rectangle"), tag: 3)

contactsVC.navigationBar.backgroundColor = .systemBackground

setViewControllers([contactsVC,recentsVC,accountVC], animated: false)
}
Expand Down
60 changes: 58 additions & 2 deletions iOS/Ringo/Ringo/Screens/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ViewController: UIViewController {
input_email.layer.borderColor = UIColor(hexCode: "E2E8F0").cgColor
input_email.layer.cornerRadius = 5
input_email.keyboardType = .emailAddress
input_email.autocapitalizationType = .none

stackView.axis = .vertical
stackView.alignment = .fill
Expand Down Expand Up @@ -114,8 +115,9 @@ class ViewController: UIViewController {
input_passwd.isSecureTextEntry = true
input_passwd.rightView = showBtn
input_passwd.rightViewMode = .always
input_passwd.autocapitalizationType = .none

// error.layer.isHidden = true
error.layer.isHidden = true
error.setTitle(" Incorrect password. Please check your password.", for: .normal)
error.setTitleColor(.red, for: .normal)
error.titleLabel?.font = .systemFont(ofSize: 13)
Expand All @@ -136,7 +138,9 @@ class ViewController: UIViewController {
signinBtn.layer.shadowOpacity = 0.5
signinBtn.layer.shadowOffset = CGSize(width: 0, height: 0)
signinBtn.layer.masksToBounds = false

signinBtn.addTarget(self, action: #selector(onPressSignin(_:)), for: .touchUpInside)
// signinBtn.addAction(UIAction{_ in self.login()}, for: .touchUpInside)

Text4.text = "or continue with"
Text4.font = .preferredFont(forTextStyle: .body)
Text4.textColor = .lightGray
Expand Down Expand Up @@ -215,6 +219,9 @@ class ViewController: UIViewController {
make.trailing.equalTo(apple.snp.leading).offset(-20)
}
}
@objc func onPressSignin(_ sender: UIButton) {
login()
}
}

// MARK: - canvas 이용하기
Expand Down Expand Up @@ -246,3 +253,52 @@ extension UIColor {
alpha: alpha)
}
}
// MARK: - Sign in
extension ViewController {

func login() {

guard let email = input_email.text else { return }
guard let passward = input_passwd.text else { return }

SigninSercive.shared.login(email: email, password: passward) { response in
switch response {
case .success(let data):

guard let data = data as? String else { return }
if data == "success"{
// UserDefaults.standard.set(data.data?.jwtToken, forKey: "jwtToken")
let nav = UINavigationController()
nav.modalPresentationStyle = .fullScreen
nav.navigationBar.barTintColor = .white
// nav.navigationBar.tintColor = UIColor(w: 42)

//네비게이션 중복 수정 1/31
nav.navigationBar.isHidden = true

let controller = TabBarViewController()
nav.viewControllers = [controller]
self.present(nav, animated: true, completion: nil)
print(data)
} else {
let alert = UIAlertController(title: data, message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "확인", style: .cancel, handler: nil))
// alert.addAction(UIAlertAction(title: "DEFAULT", style: .default, handler: nil))
// alert.addAction(UIAlertAction(title: "DESTRUCTIVE", style: .destructive, handler: nil))

self.present(alert, animated: true, completion: nil)
print(data)
}

case .requestErr(let err):
print(err)
case .pathErr:
print("pathErr")
case .serverErr:
print("serverErr")
case .networkFail:
print("networkFail")
}
}
}
}
77 changes: 77 additions & 0 deletions iOS/Ringo/Ringo/SigninService.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//
// SigninService.swift
// Ringo
//
// Created by 강진혁 on 2/6/24.
//

import Foundation
import Alamofire
//(1) 라이브러리 추가

class SigninSercive {

static let shared = SigninSercive()
//(2)싱글통 객체를 선언해서 앱 어디에서든지 접근가능하도록 한다
private init() {}

func login(email: String, password: String, completion: @escaping(NetworkResult<Any>) -> Void)
{
let url = "http://192.168.0.7:7080/member/login" //통신할 API 주소

//HTTP Headers : 요청 헤더
let header : HTTPHeaders = ["Content-Type" : "application/json"]

//요청 바디
let body : Parameters = [
"loginEmail" : email,
"password" : password
]

//요청서 //Request 생성
//통신할 주소, HTTP메소드, 요청방식, 인코딩방식, 요청헤더
let dataRequest = AF.request(url,
method: .post,
parameters: body,
encoding: JSONEncoding.default,
headers: header)

//request 시작 ,responseData를 호출하면서 데이터 통신 시작
dataRequest.responseData{
response in //데이터 통신의 결과가 response에 담기게 된다
switch response.result {
case .success: //데이터 통신이 성공한 경우에
guard let statusCode = response.response?.statusCode else {return}
guard let value = response.value else {return}

let networkResult = self.judgeStatus(by: statusCode, value)
completion(networkResult)


case .failure:
completion(.networkFail)
}
}
}
private func judgeStatus(by statusCode: Int, _ data: Data) -> NetworkResult<Any> {
print(statusCode)
switch statusCode {
case ..<300 : return isVaildData(data: data)
case 400..<500 : return .pathErr
case 500..<600 : return .serverErr
default : return .networkFail
}
}
//통신이 성공하고 원하는 데이터가 올바르게 들어왔을때 처리하는 함수
private func isVaildData(data: Data) -> NetworkResult<Any> {
// response가 string일 경우
let decodedata = String(data: data, encoding: .utf8)
print(decodedata! as Any)
// let decoder = JSONDecoder() //서버에서 준 데이터를 Codable을 채택, response가 json일 경우
guard let decodedData = /*try? decoder.decode(SigninDataResponse.self, from: data)*/ decodedata
//데이터가 변환이 되게끔 Response 모델 구조체로 데이터를 변환해서 넣고, 그 데이터를 NetworkResult Success 파라미터로 전달
else { return .pathErr }

return .success(decodedData as Any)
}
}

0 comments on commit 014a7dd

Please sign in to comment.