From 810a3a7679bd1cbb882cc9a5478d17d510d9cc0c Mon Sep 17 00:00:00 2001 From: lucas delanier Date: Wed, 11 Oct 2023 17:19:31 +0200 Subject: [PATCH] start authservice --- Sources/allin/allin.xcodeproj/project.pbxproj | 12 ++++ Sources/allin/allin/Screens/LoginScreen.swift | 15 +++++ .../allin/allin/Services/AuthService.swift | 55 +++++++++++++++++++ Sources/allin/allin/allinApp.swift | 2 + 4 files changed, 84 insertions(+) create mode 100644 Sources/allin/allin/Services/AuthService.swift diff --git a/Sources/allin/allin.xcodeproj/project.pbxproj b/Sources/allin/allin.xcodeproj/project.pbxproj index f526a57..b6f2a31 100644 --- a/Sources/allin/allin.xcodeproj/project.pbxproj +++ b/Sources/allin/allin.xcodeproj/project.pbxproj @@ -29,6 +29,7 @@ EC46D7DF2ABCE0A20030AC04 /* ParameterMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC46D7DE2ABCE0A20030AC04 /* ParameterMenuView.swift */; }; EC50BF962ABF4D3300197685 /* SplashScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC50BF952ABF4D3300197685 /* SplashScreen.swift */; }; EC50BF982ABF541B00197685 /* WelcomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC50BF972ABF541B00197685 /* WelcomeScreen.swift */; }; + EC55565D2AD6E5B900E5CA3F /* AuthService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC55565C2AD6E5B900E5CA3F /* AuthService.swift */; }; EC8601F52AC37919004262C8 /* HomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC8601F42AC37919004262C8 /* HomeScreen.swift */; }; EC87FCD62ABBA24000363986 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC87FCD52ABBA24000363986 /* Extensions.swift */; }; EC87FCD92ABBA60900363986 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC87FCD82ABBA60900363986 /* Colors.swift */; }; @@ -86,6 +87,7 @@ EC46D7DE2ABCE0A20030AC04 /* ParameterMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ParameterMenuView.swift; path = allin/Views/ParameterMenuView.swift; sourceTree = SOURCE_ROOT; }; EC50BF952ABF4D3300197685 /* SplashScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = SplashScreen.swift; path = allin/Screens/SplashScreen.swift; sourceTree = SOURCE_ROOT; }; EC50BF972ABF541B00197685 /* WelcomeScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = WelcomeScreen.swift; path = allin/Screens/WelcomeScreen.swift; sourceTree = SOURCE_ROOT; }; + EC55565C2AD6E5B900E5CA3F /* AuthService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = AuthService.swift; path = allin/Services/AuthService.swift; sourceTree = SOURCE_ROOT; }; EC8601F42AC37919004262C8 /* HomeScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = HomeScreen.swift; path = allin/Screens/HomeScreen.swift; sourceTree = SOURCE_ROOT; }; EC87FCD52ABBA24000363986 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Extensions.swift; path = allin/Extensions/Extensions.swift; sourceTree = SOURCE_ROOT; }; EC87FCD82ABBA60900363986 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Colors.swift; path = allin/Ressources/Colors.swift; sourceTree = SOURCE_ROOT; }; @@ -168,6 +170,7 @@ D98C4D622AB9D017007A6B4D /* AllIn */ = { isa = PBXGroup; children = ( + EC55565B2AD6E59C00E5CA3F /* Services */, EC9A45AE2ABDF47A00125D41 /* Screens */, EC0EA7AD2ABDAAAC006BA4A0 /* Fonts */, EC87FCD72ABBA5F200363986 /* Ressources */, @@ -214,6 +217,14 @@ path = Fonts; sourceTree = ""; }; + EC55565B2AD6E59C00E5CA3F /* Services */ = { + isa = PBXGroup; + children = ( + EC55565C2AD6E5B900E5CA3F /* AuthService.swift */, + ); + path = Services; + sourceTree = ""; + }; EC87FCD72ABBA5F200363986 /* Ressources */ = { isa = PBXGroup; children = ( @@ -388,6 +399,7 @@ EC87FCD92ABBA60900363986 /* Colors.swift in Sources */, EC3737E82AC5A53B00E6BDB5 /* AllcoinsCapsule.swift in Sources */, EC3737EA2AC5A58B00E6BDB5 /* RecapBetCard.swift in Sources */, + EC55565D2AD6E5B900E5CA3F /* AuthService.swift in Sources */, D98C4D662AB9D017007A6B4D /* ContentView.swift in Sources */, D92EC57C2ABADA2800CCD30E /* CoinCounterView.swift in Sources */, EC3737E42AC2F5FB00E6BDB5 /* ChoiceCapsule.swift in Sources */, diff --git a/Sources/allin/allin/Screens/LoginScreen.swift b/Sources/allin/allin/Screens/LoginScreen.swift index de07e4d..ff003c9 100644 --- a/Sources/allin/allin/Screens/LoginScreen.swift +++ b/Sources/allin/allin/Screens/LoginScreen.swift @@ -97,5 +97,20 @@ struct Login: View { } } + func login(email: String, password: String) { + let api = AuthService() + + api.login(email: email, password: password) { statusCode in + DispatchQueue.main.async { + if statusCode == 200 { + + } else { + errorAlertMessage = "La connexion a échoué. Veuillez réessayer." + showErrorAlert = true + } + } + } + } } + diff --git a/Sources/allin/allin/Services/AuthService.swift b/Sources/allin/allin/Services/AuthService.swift new file mode 100644 index 0000000..c639084 --- /dev/null +++ b/Sources/allin/allin/Services/AuthService.swift @@ -0,0 +1,55 @@ +// +// AuthService.swift +// AllIn +// +// Created by étudiant on 11/10/2023. +// + +import Foundation +class AuthService { + + let baseURL : String = "https://codefirst.iut.uca.fr/containers/AllDev-api/" + + public func login(email : String, password : String, completion : @escaping (Int)-> ()){ + let url = URL(string: baseURL+"/users/login")! + var request = URLRequest(url: url) + request.httpMethod = "POST" + + let json = [ + "email":email, + "password":password, + ] + + if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){ + URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in + print ("ALLIN : Process LOGIN") + if let httpResponse = response as? HTTPURLResponse { + completion(httpResponse.statusCode) + } + }.resume() + } + } + + + public func register(email : String, password : String, username : String, completion : @escaping (Int)-> ()){ + let url = URL(string: baseURL+"/users/register")! + var request = URLRequest(url: url) + request.httpMethod = "POST" + + let json = [ + "email":email, + "username":username, + "password":password, + ] + + if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){ + URLSession.shared.uploadTask(with: request, from: jsonData) { data, response, error in + print ("ALLIN : Process REGISTER") + if let httpResponse = response as? HTTPURLResponse { + completion(httpResponse.statusCode) + } + }.resume() + } + } + +} diff --git a/Sources/allin/allin/allinApp.swift b/Sources/allin/allin/allinApp.swift index 0b5242b..c09bd14 100644 --- a/Sources/allin/allin/allinApp.swift +++ b/Sources/allin/allin/allinApp.swift @@ -9,9 +9,11 @@ import SwiftUI @main struct AllInApp: App { + //@StateObject var auth = AuthService() var body: some Scene { WindowGroup { ContentView() + // .environmentObject(auth) } } }