From 810a3a7679bd1cbb882cc9a5478d17d510d9cc0c Mon Sep 17 00:00:00 2001 From: lucas delanier Date: Wed, 11 Oct 2023 17:19:31 +0200 Subject: [PATCH 1/4] 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) } } } -- 2.36.3 From f59afdc193f74eb4c195507bbfa2146b43d6ea20 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Wed, 11 Oct 2023 22:17:52 +0200 Subject: [PATCH 2/4] end authservice --- Sources/allin/allin.xcodeproj/project.pbxproj | 16 +++++++++ Sources/allin/allin/ContentView.swift | 2 +- Sources/allin/allin/Models/Singleton.swift | 19 ++++++++++ Sources/allin/allin/Models/User.swift | 21 +++++++++++ Sources/allin/allin/Screens/LoginScreen.swift | 19 ++++++++-- .../allin/allin/Screens/RegisterScreen.swift | 36 +++++++++++++++---- .../allin/allin/Services/AuthService.swift | 10 ++++-- 7 files changed, 110 insertions(+), 13 deletions(-) create mode 100644 Sources/allin/allin/Models/Singleton.swift create mode 100644 Sources/allin/allin/Models/User.swift diff --git a/Sources/allin/allin.xcodeproj/project.pbxproj b/Sources/allin/allin.xcodeproj/project.pbxproj index b6f2a31..30fc174 100644 --- a/Sources/allin/allin.xcodeproj/project.pbxproj +++ b/Sources/allin/allin.xcodeproj/project.pbxproj @@ -39,6 +39,8 @@ ECA7010B2AC4003400532444 /* RankingRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010A2AC4003400532444 /* RankingRowView.swift */; }; ECA7010D2AC4948600532444 /* FriendsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010C2AC4948600532444 /* FriendsScreen.swift */; }; ECA7010F2AC4949D00532444 /* FriendView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010E2AC4949D00532444 /* FriendView.swift */; }; + ECCD6DD92AD7228B00F947C4 /* Singleton.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECCD6DD82AD7228B00F947C4 /* Singleton.swift */; }; + ECCD6DDB2AD7233F00F947C4 /* User.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECCD6DDA2AD7233F00F947C4 /* User.swift */; }; ECDF624A2AC1CAFD00BA8213 /* RegisterScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECDF62492AC1CAFD00BA8213 /* RegisterScreen.swift */; }; ECFC54442AC0C39E00195760 /* LoginScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECFC54432AC0C39E00195760 /* LoginScreen.swift */; }; /* End PBXBuildFile section */ @@ -97,6 +99,8 @@ ECA7010A2AC4003400532444 /* RankingRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = RankingRowView.swift; path = allin/Views/RankingRowView.swift; sourceTree = SOURCE_ROOT; }; ECA7010C2AC4948600532444 /* FriendsScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FriendsScreen.swift; path = allin/Screens/FriendsScreen.swift; sourceTree = SOURCE_ROOT; }; ECA7010E2AC4949D00532444 /* FriendView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = FriendView.swift; path = allin/Views/FriendView.swift; sourceTree = SOURCE_ROOT; }; + ECCD6DD82AD7228B00F947C4 /* Singleton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Singleton.swift; path = allin/Models/Singleton.swift; sourceTree = SOURCE_ROOT; }; + ECCD6DDA2AD7233F00F947C4 /* User.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = User.swift; path = allin/Models/User.swift; sourceTree = SOURCE_ROOT; }; ECDF62492AC1CAFD00BA8213 /* RegisterScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = RegisterScreen.swift; path = allin/Screens/RegisterScreen.swift; sourceTree = SOURCE_ROOT; }; ECFC54432AC0C39E00195760 /* LoginScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LoginScreen.swift; path = allin/Screens/LoginScreen.swift; sourceTree = SOURCE_ROOT; }; /* End PBXFileReference section */ @@ -170,6 +174,7 @@ D98C4D622AB9D017007A6B4D /* AllIn */ = { isa = PBXGroup; children = ( + ECCD6DD72AD7227500F947C4 /* Models */, EC55565B2AD6E59C00E5CA3F /* Services */, EC9A45AE2ABDF47A00125D41 /* Screens */, EC0EA7AD2ABDAAAC006BA4A0 /* Fonts */, @@ -250,6 +255,15 @@ path = Screens; sourceTree = ""; }; + ECCD6DD72AD7227500F947C4 /* Models */ = { + isa = PBXGroup; + children = ( + ECCD6DD82AD7228B00F947C4 /* Singleton.swift */, + ECCD6DDA2AD7233F00F947C4 /* User.swift */, + ); + path = Models; + sourceTree = ""; + }; ECF816C72ABB51E300DE30A4 /* Extensions */ = { isa = PBXGroup; children = ( @@ -392,6 +406,7 @@ EC31955E2ACD3B8E00D0A4DC /* ConfidentialityView.swift in Sources */, ECA701092AC3FE3300532444 /* RankingScreen.swift in Sources */, ECFC54442AC0C39E00195760 /* LoginScreen.swift in Sources */, + ECCD6DDB2AD7233F00F947C4 /* User.swift in Sources */, EC87FCD62ABBA24000363986 /* Extensions.swift in Sources */, EC46D7DD2ABCCC270030AC04 /* MenuView.swift in Sources */, EC2C1EEB2AC08BE80091D57C /* BetCard.swift in Sources */, @@ -401,6 +416,7 @@ EC3737EA2AC5A58B00E6BDB5 /* RecapBetCard.swift in Sources */, EC55565D2AD6E5B900E5CA3F /* AuthService.swift in Sources */, D98C4D662AB9D017007A6B4D /* ContentView.swift in Sources */, + ECCD6DD92AD7228B00F947C4 /* Singleton.swift in Sources */, D92EC57C2ABADA2800CCD30E /* CoinCounterView.swift in Sources */, EC3737E42AC2F5FB00E6BDB5 /* ChoiceCapsule.swift in Sources */, EC3737E62AC41DB000E6BDB5 /* WinModal.swift in Sources */, diff --git a/Sources/allin/allin/ContentView.swift b/Sources/allin/allin/ContentView.swift index 30cfc89..f5b2673 100644 --- a/Sources/allin/allin/ContentView.swift +++ b/Sources/allin/allin/ContentView.swift @@ -11,7 +11,7 @@ struct ContentView: View { var body: some View { NavigationView { - Home(page: "CreationBet") + Welcome() } } } diff --git a/Sources/allin/allin/Models/Singleton.swift b/Sources/allin/allin/Models/Singleton.swift new file mode 100644 index 0000000..30b7674 --- /dev/null +++ b/Sources/allin/allin/Models/Singleton.swift @@ -0,0 +1,19 @@ +// +// Singleton.swift +// AllIn +// +// Created by étudiant on 11/10/2023. +// + +import Foundation + +class Singleton { + + static let singleton = Singleton() + + var user: User? + + var auth: AuthService = AuthService() + + private init() { } +} diff --git a/Sources/allin/allin/Models/User.swift b/Sources/allin/allin/Models/User.swift new file mode 100644 index 0000000..d848175 --- /dev/null +++ b/Sources/allin/allin/Models/User.swift @@ -0,0 +1,21 @@ +// +// User.swift +// AllIn +// +// Created by étudiant on 11/10/2023. +// + +import Foundation + +class User { + + public var Username: String + public var Email: String + public var NbCoins: Int + + public init(Username: String, andEmail Email: String, NbCoins: Int) { + self.Username = Username + self.Email = Email + self.NbCoins = NbCoins + } +} diff --git a/Sources/allin/allin/Screens/LoginScreen.swift b/Sources/allin/allin/Screens/LoginScreen.swift index ff003c9..0e5dcf8 100644 --- a/Sources/allin/allin/Screens/LoginScreen.swift +++ b/Sources/allin/allin/Screens/LoginScreen.swift @@ -12,6 +12,10 @@ struct Login: View { @State private var isPasswordVisible = true @State private var username: String = "" @State private var password: String = "" + @State private var showErrorAlert = false + @State private var errorAlertMessage = "" + @State private var isLoginSuccessful = false + var body: some View { GeometryReader { geometry in VStack(spacing: 15) { @@ -33,6 +37,7 @@ struct Login: View { RoundedRectangle(cornerRadius: 9, style: .continuous) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) ) + .autocapitalization(.none) .padding(.bottom, 8) Group { @@ -69,8 +74,9 @@ struct Login: View { .padding(.leading, 150) .betTextStyle(weight: .medium, color: AllinColor.StartTextColor, size: 14) - NavigationLink(destination: Home(page: "Bet").navigationBarBackButtonHidden(true)) - { + Button(action: { + login(email: username, password: password) + }) { Text("Se connecter") .betTextStyle(weight: .bold, color: .white, size: 17) .frame(width: 300, height: 60) @@ -79,6 +85,10 @@ struct Login: View { startPoint: .leading, endPoint: .trailing)) .cornerRadius(13) } + .background( + NavigationLink("", destination: Home(page: "Bet").navigationBarBackButtonHidden(true), isActive: $isLoginSuccessful) + .opacity(0) + ) Spacer() HStack(spacing: 0) { @@ -95,6 +105,9 @@ struct Login: View { .frame(width: geometry.size.width, height: geometry.size.height) .background(AllinColor.StartBackground) } + .alert(isPresented: $showErrorAlert) { + Alert(title: Text("Erreur de connexion"), message: Text(errorAlertMessage), dismissButton: .default(Text("OK"))) + } } func login(email: String, password: String) { @@ -103,7 +116,7 @@ struct Login: View { api.login(email: email, password: password) { statusCode in DispatchQueue.main.async { if statusCode == 200 { - + isLoginSuccessful = true } else { errorAlertMessage = "La connexion a échoué. Veuillez réessayer." showErrorAlert = true diff --git a/Sources/allin/allin/Screens/RegisterScreen.swift b/Sources/allin/allin/Screens/RegisterScreen.swift index ef88316..6197c5a 100644 --- a/Sources/allin/allin/Screens/RegisterScreen.swift +++ b/Sources/allin/allin/Screens/RegisterScreen.swift @@ -10,18 +10,20 @@ import SwiftUI struct Register: View { @State private var isPasswordVisible = true - @State private var pseudo: String = "" @State private var username: String = "" + @State private var email: String = "" @State private var password: String = "" @State private var passwordConfirm: String = "" + @State private var isRegisterSuccessful = false + var body: some View { GeometryReader { geometry in ScrollView(showsIndicators: false) { VStack(spacing: 15) { Spacer() VStack { - if (pseudo != "") { - Text("Bonjour "+pseudo+",") + if (username != "") { + Text("Bonjour "+username+",") .betTextStyle(weight: .semibold, color: AllinColor.StartTextColor, size: 40) .padding([.trailing, .leading], 30) } else { @@ -40,7 +42,7 @@ struct Register: View { .betTextStyle(weight: .regular, color: AllinColor.StartTextColor, size: 20) .padding(.bottom, 60) - TextField("", text: $pseudo, prompt: Text("Pseudo").foregroundColor(.gray)) + TextField("", text: $username, prompt: Text("Pseudo").foregroundColor(.gray)) .padding() .background(Color.white.cornerRadius(9)) .frame(width: 300) @@ -51,7 +53,7 @@ struct Register: View { ) .padding(.bottom, 8) - TextField("", text: $username, prompt: Text("Email").foregroundColor(.gray)) + TextField("", text: $email, prompt: Text("Email").foregroundColor(.gray)) .padding() .background(Color.white.cornerRadius(9)) .frame(width: 300) @@ -67,6 +69,7 @@ struct Register: View { SecureField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) } else { TextField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) + .autocapitalization(.none) } } .padding() @@ -97,6 +100,7 @@ struct Register: View { SecureField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) } else { TextField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) + .autocapitalization(.none) } } .padding() @@ -121,7 +125,9 @@ struct Register: View { .foregroundColor(.black) .padding(.bottom, 50) - Button(action: {}) { + Button(action: { + register(email: email, username: username, password: password) + }) { Text("S'inscrire") .betTextStyle(weight: .bold, color: .white, size: 17) } @@ -129,6 +135,10 @@ struct Register: View { .background(LinearGradient(gradient: Gradient(colors:[AllinColor.TopBarColorPink,AllinColor.TopBarColorPurple,AllinColor.TopBarColorBlue]), startPoint: .leading, endPoint: .trailing)) + .background( + NavigationLink("", destination: Home(page: "Bet").navigationBarBackButtonHidden(true), isActive: $isRegisterSuccessful) + .opacity(0) + ) .cornerRadius(13) Spacer() @@ -151,4 +161,18 @@ struct Register: View { } } + func register(email: String, username: String, password: String) { + let api = AuthService() + + api.register(email: email, password: password, username: username) { statusCode in + DispatchQueue.main.async { + if statusCode == 201 { + isRegisterSuccessful = true + } else { + + } + } + } + } + } diff --git a/Sources/allin/allin/Services/AuthService.swift b/Sources/allin/allin/Services/AuthService.swift index c639084..d02691d 100644 --- a/Sources/allin/allin/Services/AuthService.swift +++ b/Sources/allin/allin/Services/AuthService.swift @@ -6,17 +6,19 @@ // 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")! + let url = URL(string: baseURL+"users/login")! var request = URLRequest(url: url) request.httpMethod = "POST" + request.setValue("application/json", forHTTPHeaderField: "Content-Type") let json = [ - "email":email, + "username":email, "password":password, ] @@ -35,11 +37,13 @@ class AuthService { let url = URL(string: baseURL+"/users/register")! var request = URLRequest(url: url) request.httpMethod = "POST" - + request.setValue("application/json", forHTTPHeaderField: "Content-Type") + let json = [ "email":email, "username":username, "password":password, + "nbCoins": "0" ] if let jsonData = try? JSONSerialization.data(withJSONObject: json, options: []){ -- 2.36.3 From e0954cba471943ecddaea12d3dd1b574fb2320a9 Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Thu, 12 Oct 2023 08:30:32 +0200 Subject: [PATCH 3/4] Error display :white_check_mark: --- .../allin/allin/Screens/RegisterScreen.swift | 203 ++++++++++++------ 1 file changed, 135 insertions(+), 68 deletions(-) diff --git a/Sources/allin/allin/Screens/RegisterScreen.swift b/Sources/allin/allin/Screens/RegisterScreen.swift index 6197c5a..daebb85 100644 --- a/Sources/allin/allin/Screens/RegisterScreen.swift +++ b/Sources/allin/allin/Screens/RegisterScreen.swift @@ -15,6 +15,12 @@ struct Register: View { @State private var password: String = "" @State private var passwordConfirm: String = "" @State private var isRegisterSuccessful = false + @State private var errorPassword = false + @State private var errorMail = false + @State private var errorUsername = false + @State private var errorMailMessage: String = "" + @State private var errorUsernameMessage: String = "" + @State private var errorPasswordMessage: String = "" var body: some View { GeometryReader { geometry in @@ -42,91 +48,123 @@ struct Register: View { .betTextStyle(weight: .regular, color: AllinColor.StartTextColor, size: 20) .padding(.bottom, 60) - TextField("", text: $username, prompt: Text("Pseudo").foregroundColor(.gray)) + VStack { + if errorUsername { + Text(errorUsernameMessage) + .font(.system(size: 10)) + .foregroundColor(.red) + .fontWeight(.bold) + } + TextField("", text: $username, prompt: Text("Pseudo").foregroundColor(.gray)) + .padding() + .background(Color.white.cornerRadius(9)) + .frame(width: 300) + .foregroundColor(.black) + .overlay( + RoundedRectangle(cornerRadius: 9, style: .continuous) + .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) + ) + .padding(.bottom, 8) + } + + VStack { + if errorMail { + Text(errorMailMessage) + .font(.system(size: 10)) + .foregroundColor(.red) + .fontWeight(.bold) + } + TextField("", text: $email, prompt: Text("Email").foregroundColor(.gray)) + .padding() + .background(Color.white.cornerRadius(9)) + .frame(width: 300) + .foregroundColor(.black) + .overlay( + RoundedRectangle(cornerRadius: 9, style: .continuous) + .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) + ) + .padding(.bottom, 8) + } + + VStack { + if errorPassword { + Text(errorPasswordMessage) + .font(.system(size: 10)) + .foregroundColor(.red) + .fontWeight(.bold) + } + Group { + if isPasswordVisible { + SecureField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) + } else { + TextField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) + .autocapitalization(.none) + } + } .padding() .background(Color.white.cornerRadius(9)) .frame(width: 300) - .foregroundColor(.black) .overlay( RoundedRectangle(cornerRadius: 9, style: .continuous) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) ) + .foregroundColor(.black) .padding(.bottom, 8) + .overlay( + HStack { + Spacer() + Button(action: { + isPasswordVisible.toggle() + }) { + Image(systemName: isPasswordVisible ? "eye.slash" : "eye") + .foregroundColor(.gray) + } + .padding(.bottom, 8) + .padding(.trailing, 8) + } + ) + } - TextField("", text: $email, prompt: Text("Email").foregroundColor(.gray)) + VStack { + if errorPassword { + Text(errorPasswordMessage) + .font(.system(size: 10)) + .foregroundColor(.red) + .fontWeight(.bold) + } + + Group { + if isPasswordVisible { + SecureField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) + } else { + TextField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) + .autocapitalization(.none) + } + } .padding() .background(Color.white.cornerRadius(9)) .frame(width: 300) - .foregroundColor(.black) .overlay( RoundedRectangle(cornerRadius: 9, style: .continuous) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) ) - .padding(.bottom, 8) - - Group { - if isPasswordVisible { - SecureField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) - } else { - TextField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) - .autocapitalization(.none) - } - } - .padding() - .background(Color.white.cornerRadius(9)) - .frame(width: 300) - .overlay( - RoundedRectangle(cornerRadius: 9, style: .continuous) - .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) - ) - .foregroundColor(.black) - .padding(.bottom, 8) - .overlay( - HStack { - Spacer() - Button(action: { - isPasswordVisible.toggle() - }) { - Image(systemName: isPasswordVisible ? "eye.slash" : "eye") - .foregroundColor(.gray) + .overlay( + HStack { + Spacer() + Button(action: { + isPasswordVisible.toggle() + }) { + Image(systemName: isPasswordVisible ? "eye.slash" : "eye") + .foregroundColor(.gray) + } + .padding(.trailing, 8) } - .padding(.bottom, 8) - .padding(.trailing, 8) - } - ) - - Group { - if isPasswordVisible { - SecureField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) - } else { - TextField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) - .autocapitalization(.none) - } + ) + .foregroundColor(.black) + .padding(.bottom, 50) } - .padding() - .background(Color.white.cornerRadius(9)) - .frame(width: 300) - .overlay( - RoundedRectangle(cornerRadius: 9, style: .continuous) - .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) - ) - .overlay( - HStack { - Spacer() - Button(action: { - isPasswordVisible.toggle() - }) { - Image(systemName: isPasswordVisible ? "eye.slash" : "eye") - .foregroundColor(.gray) - } - .padding(.trailing, 8) - } - ) - .foregroundColor(.black) - .padding(.bottom, 50) - Button(action: { - register(email: email, username: username, password: password) + register(email: email, username: username, password: password, confirmPassword: passwordConfirm) }) { Text("S'inscrire") .betTextStyle(weight: .bold, color: .white, size: 17) @@ -161,9 +199,32 @@ struct Register: View { } } - func register(email: String, username: String, password: String) { + func register(email: String, username: String, password: String, confirmPassword: String) { + cleanError() + if (password != confirmPassword) { + errorPassword = true + errorPasswordMessage = "Les mots de passes doivent être identiques." + return + } + if (username.isEmpty) { + errorUsername = true + errorUsernameMessage = "Le pseudo ne peut pas être vide." + return + } + + if (email.isEmpty) { + errorMail = true + errorMailMessage = "Le mail ne peut pas être vide." + return + } + + if (password.isEmpty || confirmPassword.isEmpty) { + errorPassword = true + errorPasswordMessage = "Veuillez renseigner le mot de passe sur les deux champs." + return + } + let api = AuthService() - api.register(email: email, password: password, username: username) { statusCode in DispatchQueue.main.async { if statusCode == 201 { @@ -175,4 +236,10 @@ struct Register: View { } } + func cleanError() { + errorPassword = false + errorMail = false + errorUsername = false + } + } -- 2.36.3 From 5e41093f7e38029b3a46e9f7dda7f3ce0403799a Mon Sep 17 00:00:00 2001 From: "emre.kartal" Date: Thu, 12 Oct 2023 08:47:03 +0200 Subject: [PATCH 4/4] add more display errors --- .../allin/Screens/CreationBetScreen.swift | 5 +++ .../allin/allin/Screens/RegisterScreen.swift | 33 ++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/Sources/allin/allin/Screens/CreationBetScreen.swift b/Sources/allin/allin/Screens/CreationBetScreen.swift index f2f6b22..b7b85df 100644 --- a/Sources/allin/allin/Screens/CreationBetScreen.swift +++ b/Sources/allin/allin/Screens/CreationBetScreen.swift @@ -255,6 +255,11 @@ struct CreationBet: View { ) .frame(width: 250, height: 38) .foregroundColor(.black) + .onChange(of: response) { newValue in + if newValue.count > 20 { + response = String(newValue.prefix(20)) + } + } Button(action: { diff --git a/Sources/allin/allin/Screens/RegisterScreen.swift b/Sources/allin/allin/Screens/RegisterScreen.swift index daebb85..5628870 100644 --- a/Sources/allin/allin/Screens/RegisterScreen.swift +++ b/Sources/allin/allin/Screens/RegisterScreen.swift @@ -65,6 +65,11 @@ struct Register: View { .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) ) .padding(.bottom, 8) + .onChange(of: username) { newValue in + if newValue.count > 25 { + username = String(newValue.prefix(25)) + } + } } VStack { @@ -84,6 +89,11 @@ struct Register: View { .stroke(AllinColor.StrokeGrayColor, lineWidth: 1) ) .padding(.bottom, 8) + .onChange(of: email) { newValue in + if newValue.count > 50 { + email = String(newValue.prefix(50)) + } + } } VStack { @@ -201,29 +211,35 @@ struct Register: View { func register(email: String, username: String, password: String, confirmPassword: String) { cleanError() - if (password != confirmPassword) { + if password != confirmPassword { errorPassword = true errorPasswordMessage = "Les mots de passes doivent être identiques." return } - if (username.isEmpty) { + if username.isEmpty { errorUsername = true errorUsernameMessage = "Le pseudo ne peut pas être vide." return } - if (email.isEmpty) { + if email.isEmpty { errorMail = true errorMailMessage = "Le mail ne peut pas être vide." return } - if (password.isEmpty || confirmPassword.isEmpty) { + if password.isEmpty || confirmPassword.isEmpty { errorPassword = true errorPasswordMessage = "Veuillez renseigner le mot de passe sur les deux champs." return } + if isValidEmail(email: email) { + errorMail = true + errorMailMessage = "L'adresse e-mail n'est pas valide." + return + } + let api = AuthService() api.register(email: email, password: password, username: username) { statusCode in DispatchQueue.main.async { @@ -236,6 +252,15 @@ struct Register: View { } } + func isValidEmail(email: String) -> Bool + { + if(email.range(of:"^\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", options: .regularExpression) != nil) { + return false + } else { + return true + } + } + func cleanError() { errorPassword = false errorMail = false -- 2.36.3