auth_manage #4

Merged
emre.kartal merged 4 commits from auth_manage into master 2 years ago

@ -29,6 +29,7 @@
EC46D7DF2ABCE0A20030AC04 /* ParameterMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC46D7DE2ABCE0A20030AC04 /* ParameterMenuView.swift */; }; EC46D7DF2ABCE0A20030AC04 /* ParameterMenuView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC46D7DE2ABCE0A20030AC04 /* ParameterMenuView.swift */; };
EC50BF962ABF4D3300197685 /* SplashScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC50BF952ABF4D3300197685 /* SplashScreen.swift */; }; EC50BF962ABF4D3300197685 /* SplashScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC50BF952ABF4D3300197685 /* SplashScreen.swift */; };
EC50BF982ABF541B00197685 /* WelcomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC50BF972ABF541B00197685 /* WelcomeScreen.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 */; }; EC8601F52AC37919004262C8 /* HomeScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC8601F42AC37919004262C8 /* HomeScreen.swift */; };
EC87FCD62ABBA24000363986 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC87FCD52ABBA24000363986 /* Extensions.swift */; }; EC87FCD62ABBA24000363986 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC87FCD52ABBA24000363986 /* Extensions.swift */; };
EC87FCD92ABBA60900363986 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC87FCD82ABBA60900363986 /* Colors.swift */; }; EC87FCD92ABBA60900363986 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = EC87FCD82ABBA60900363986 /* Colors.swift */; };
@ -38,6 +39,8 @@
ECA7010B2AC4003400532444 /* RankingRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010A2AC4003400532444 /* RankingRowView.swift */; }; ECA7010B2AC4003400532444 /* RankingRowView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010A2AC4003400532444 /* RankingRowView.swift */; };
ECA7010D2AC4948600532444 /* FriendsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010C2AC4948600532444 /* FriendsScreen.swift */; }; ECA7010D2AC4948600532444 /* FriendsScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010C2AC4948600532444 /* FriendsScreen.swift */; };
ECA7010F2AC4949D00532444 /* FriendView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECA7010E2AC4949D00532444 /* FriendView.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 */; }; ECDF624A2AC1CAFD00BA8213 /* RegisterScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECDF62492AC1CAFD00BA8213 /* RegisterScreen.swift */; };
ECFC54442AC0C39E00195760 /* LoginScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECFC54432AC0C39E00195760 /* LoginScreen.swift */; }; ECFC54442AC0C39E00195760 /* LoginScreen.swift in Sources */ = {isa = PBXBuildFile; fileRef = ECFC54432AC0C39E00195760 /* LoginScreen.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
@ -86,6 +89,7 @@
EC46D7DE2ABCE0A20030AC04 /* ParameterMenuView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = ParameterMenuView.swift; path = allin/Views/ParameterMenuView.swift; sourceTree = SOURCE_ROOT; }; 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; }; 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; }; 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; }; 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; }; 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; }; EC87FCD82ABBA60900363986 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Colors.swift; path = allin/Ressources/Colors.swift; sourceTree = SOURCE_ROOT; };
@ -95,6 +99,8 @@
ECA7010A2AC4003400532444 /* RankingRowView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = RankingRowView.swift; path = allin/Views/RankingRowView.swift; sourceTree = SOURCE_ROOT; }; 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; }; 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; }; 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; }; 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; }; ECFC54432AC0C39E00195760 /* LoginScreen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = LoginScreen.swift; path = allin/Screens/LoginScreen.swift; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */ /* End PBXFileReference section */
@ -168,6 +174,8 @@
D98C4D622AB9D017007A6B4D /* AllIn */ = { D98C4D622AB9D017007A6B4D /* AllIn */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
ECCD6DD72AD7227500F947C4 /* Models */,
EC55565B2AD6E59C00E5CA3F /* Services */,
EC9A45AE2ABDF47A00125D41 /* Screens */, EC9A45AE2ABDF47A00125D41 /* Screens */,
EC0EA7AD2ABDAAAC006BA4A0 /* Fonts */, EC0EA7AD2ABDAAAC006BA4A0 /* Fonts */,
EC87FCD72ABBA5F200363986 /* Ressources */, EC87FCD72ABBA5F200363986 /* Ressources */,
@ -214,6 +222,14 @@
path = Fonts; path = Fonts;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
EC55565B2AD6E59C00E5CA3F /* Services */ = {
isa = PBXGroup;
children = (
EC55565C2AD6E5B900E5CA3F /* AuthService.swift */,
);
path = Services;
sourceTree = "<group>";
};
EC87FCD72ABBA5F200363986 /* Ressources */ = { EC87FCD72ABBA5F200363986 /* Ressources */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -239,6 +255,15 @@
path = Screens; path = Screens;
sourceTree = "<group>"; sourceTree = "<group>";
}; };
ECCD6DD72AD7227500F947C4 /* Models */ = {
isa = PBXGroup;
children = (
ECCD6DD82AD7228B00F947C4 /* Singleton.swift */,
ECCD6DDA2AD7233F00F947C4 /* User.swift */,
);
path = Models;
sourceTree = "<group>";
};
ECF816C72ABB51E300DE30A4 /* Extensions */ = { ECF816C72ABB51E300DE30A4 /* Extensions */ = {
isa = PBXGroup; isa = PBXGroup;
children = ( children = (
@ -381,6 +406,7 @@
EC31955E2ACD3B8E00D0A4DC /* ConfidentialityView.swift in Sources */, EC31955E2ACD3B8E00D0A4DC /* ConfidentialityView.swift in Sources */,
ECA701092AC3FE3300532444 /* RankingScreen.swift in Sources */, ECA701092AC3FE3300532444 /* RankingScreen.swift in Sources */,
ECFC54442AC0C39E00195760 /* LoginScreen.swift in Sources */, ECFC54442AC0C39E00195760 /* LoginScreen.swift in Sources */,
ECCD6DDB2AD7233F00F947C4 /* User.swift in Sources */,
EC87FCD62ABBA24000363986 /* Extensions.swift in Sources */, EC87FCD62ABBA24000363986 /* Extensions.swift in Sources */,
EC46D7DD2ABCCC270030AC04 /* MenuView.swift in Sources */, EC46D7DD2ABCCC270030AC04 /* MenuView.swift in Sources */,
EC2C1EEB2AC08BE80091D57C /* BetCard.swift in Sources */, EC2C1EEB2AC08BE80091D57C /* BetCard.swift in Sources */,
@ -388,7 +414,9 @@
EC87FCD92ABBA60900363986 /* Colors.swift in Sources */, EC87FCD92ABBA60900363986 /* Colors.swift in Sources */,
EC3737E82AC5A53B00E6BDB5 /* AllcoinsCapsule.swift in Sources */, EC3737E82AC5A53B00E6BDB5 /* AllcoinsCapsule.swift in Sources */,
EC3737EA2AC5A58B00E6BDB5 /* RecapBetCard.swift in Sources */, EC3737EA2AC5A58B00E6BDB5 /* RecapBetCard.swift in Sources */,
EC55565D2AD6E5B900E5CA3F /* AuthService.swift in Sources */,
D98C4D662AB9D017007A6B4D /* ContentView.swift in Sources */, D98C4D662AB9D017007A6B4D /* ContentView.swift in Sources */,
ECCD6DD92AD7228B00F947C4 /* Singleton.swift in Sources */,
D92EC57C2ABADA2800CCD30E /* CoinCounterView.swift in Sources */, D92EC57C2ABADA2800CCD30E /* CoinCounterView.swift in Sources */,
EC3737E42AC2F5FB00E6BDB5 /* ChoiceCapsule.swift in Sources */, EC3737E42AC2F5FB00E6BDB5 /* ChoiceCapsule.swift in Sources */,
EC3737E62AC41DB000E6BDB5 /* WinModal.swift in Sources */, EC3737E62AC41DB000E6BDB5 /* WinModal.swift in Sources */,

@ -11,7 +11,7 @@ struct ContentView: View {
var body: some View { var body: some View {
NavigationView { NavigationView {
Home(page: "CreationBet") Welcome()
} }
} }
} }

@ -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() { }
}

@ -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
}
}

@ -255,6 +255,11 @@ struct CreationBet: View {
) )
.frame(width: 250, height: 38) .frame(width: 250, height: 38)
.foregroundColor(.black) .foregroundColor(.black)
.onChange(of: response) { newValue in
if newValue.count > 20 {
response = String(newValue.prefix(20))
}
}
Button(action: { Button(action: {

@ -12,6 +12,10 @@ struct Login: View {
@State private var isPasswordVisible = true @State private var isPasswordVisible = true
@State private var username: String = "" @State private var username: String = ""
@State private var password: String = "" @State private var password: String = ""
@State private var showErrorAlert = false
@State private var errorAlertMessage = ""
@State private var isLoginSuccessful = false
var body: some View { var body: some View {
GeometryReader { geometry in GeometryReader { geometry in
VStack(spacing: 15) { VStack(spacing: 15) {
@ -33,6 +37,7 @@ struct Login: View {
RoundedRectangle(cornerRadius: 9, style: .continuous) RoundedRectangle(cornerRadius: 9, style: .continuous)
.stroke(AllinColor.StrokeGrayColor, lineWidth: 1) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1)
) )
.autocapitalization(.none)
.padding(.bottom, 8) .padding(.bottom, 8)
Group { Group {
@ -69,8 +74,9 @@ struct Login: View {
.padding(.leading, 150) .padding(.leading, 150)
.betTextStyle(weight: .medium, color: AllinColor.StartTextColor, size: 14) .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") Text("Se connecter")
.betTextStyle(weight: .bold, color: .white, size: 17) .betTextStyle(weight: .bold, color: .white, size: 17)
.frame(width: 300, height: 60) .frame(width: 300, height: 60)
@ -79,6 +85,10 @@ struct Login: View {
startPoint: .leading, endPoint: .trailing)) startPoint: .leading, endPoint: .trailing))
.cornerRadius(13) .cornerRadius(13)
} }
.background(
NavigationLink("", destination: Home(page: "Bet").navigationBarBackButtonHidden(true), isActive: $isLoginSuccessful)
.opacity(0)
)
Spacer() Spacer()
HStack(spacing: 0) { HStack(spacing: 0) {
@ -95,7 +105,25 @@ struct Login: View {
.frame(width: geometry.size.width, height: geometry.size.height) .frame(width: geometry.size.width, height: geometry.size.height)
.background(AllinColor.StartBackground) .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) {
let api = AuthService()
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
}
}
} }
}
}

@ -10,18 +10,26 @@ import SwiftUI
struct Register: View { struct Register: View {
@State private var isPasswordVisible = true @State private var isPasswordVisible = true
@State private var pseudo: String = ""
@State private var username: String = "" @State private var username: String = ""
@State private var email: String = ""
@State private var password: String = "" @State private var password: String = ""
@State private var passwordConfirm: 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 { var body: some View {
GeometryReader { geometry in GeometryReader { geometry in
ScrollView(showsIndicators: false) { ScrollView(showsIndicators: false) {
VStack(spacing: 15) { VStack(spacing: 15) {
Spacer() Spacer()
VStack { VStack {
if (pseudo != "") { if (username != "") {
Text("Bonjour "+pseudo+",") Text("Bonjour "+username+",")
.betTextStyle(weight: .semibold, color: AllinColor.StartTextColor, size: 40) .betTextStyle(weight: .semibold, color: AllinColor.StartTextColor, size: 40)
.padding([.trailing, .leading], 30) .padding([.trailing, .leading], 30)
} else { } else {
@ -40,7 +48,14 @@ struct Register: View {
.betTextStyle(weight: .regular, color: AllinColor.StartTextColor, size: 20) .betTextStyle(weight: .regular, color: AllinColor.StartTextColor, size: 20)
.padding(.bottom, 60) .padding(.bottom, 60)
TextField("", text: $pseudo, 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() .padding()
.background(Color.white.cornerRadius(9)) .background(Color.white.cornerRadius(9))
.frame(width: 300) .frame(width: 300)
@ -50,8 +65,21 @@ struct Register: View {
.stroke(AllinColor.StrokeGrayColor, lineWidth: 1) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1)
) )
.padding(.bottom, 8) .padding(.bottom, 8)
.onChange(of: username) { newValue in
if newValue.count > 25 {
username = String(newValue.prefix(25))
}
}
}
TextField("", text: $username, prompt: Text("Email").foregroundColor(.gray)) VStack {
if errorMail {
Text(errorMailMessage)
.font(.system(size: 10))
.foregroundColor(.red)
.fontWeight(.bold)
}
TextField("", text: $email, prompt: Text("Email").foregroundColor(.gray))
.padding() .padding()
.background(Color.white.cornerRadius(9)) .background(Color.white.cornerRadius(9))
.frame(width: 300) .frame(width: 300)
@ -61,12 +89,26 @@ struct Register: View {
.stroke(AllinColor.StrokeGrayColor, lineWidth: 1) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1)
) )
.padding(.bottom, 8) .padding(.bottom, 8)
.onChange(of: email) { newValue in
if newValue.count > 50 {
email = String(newValue.prefix(50))
}
}
}
VStack {
if errorPassword {
Text(errorPasswordMessage)
.font(.system(size: 10))
.foregroundColor(.red)
.fontWeight(.bold)
}
Group { Group {
if isPasswordVisible { if isPasswordVisible {
SecureField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) SecureField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray))
} else { } else {
TextField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) TextField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray))
.autocapitalization(.none)
} }
} }
.padding() .padding()
@ -91,12 +133,22 @@ struct Register: View {
.padding(.trailing, 8) .padding(.trailing, 8)
} }
) )
}
VStack {
if errorPassword {
Text(errorPasswordMessage)
.font(.system(size: 10))
.foregroundColor(.red)
.fontWeight(.bold)
}
Group { Group {
if isPasswordVisible { if isPasswordVisible {
SecureField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) SecureField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray))
} else { } else {
TextField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray)) TextField("", text: $passwordConfirm, prompt: Text("Confirmation du Mot de passe").foregroundColor(.gray))
.autocapitalization(.none)
} }
} }
.padding() .padding()
@ -120,8 +172,10 @@ struct Register: View {
) )
.foregroundColor(.black) .foregroundColor(.black)
.padding(.bottom, 50) .padding(.bottom, 50)
}
Button(action: {}) { Button(action: {
register(email: email, username: username, password: password, confirmPassword: passwordConfirm)
}) {
Text("S'inscrire") Text("S'inscrire")
.betTextStyle(weight: .bold, color: .white, size: 17) .betTextStyle(weight: .bold, color: .white, size: 17)
} }
@ -129,6 +183,10 @@ struct Register: View {
.background(LinearGradient(gradient: .background(LinearGradient(gradient:
Gradient(colors:[AllinColor.TopBarColorPink,AllinColor.TopBarColorPurple,AllinColor.TopBarColorBlue]), Gradient(colors:[AllinColor.TopBarColorPink,AllinColor.TopBarColorPurple,AllinColor.TopBarColorBlue]),
startPoint: .leading, endPoint: .trailing)) startPoint: .leading, endPoint: .trailing))
.background(
NavigationLink("", destination: Home(page: "Bet").navigationBarBackButtonHidden(true), isActive: $isRegisterSuccessful)
.opacity(0)
)
.cornerRadius(13) .cornerRadius(13)
Spacer() Spacer()
@ -151,4 +209,62 @@ struct Register: View {
} }
} }
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
}
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 {
if statusCode == 201 {
isRegisterSuccessful = true
} else {
}
}
}
}
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
errorUsername = false
}
} }

@ -0,0 +1,59 @@
//
// 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"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
let json = [
"username":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"
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: []){
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()
}
}
}

@ -9,9 +9,11 @@ import SwiftUI
@main @main
struct AllInApp: App { struct AllInApp: App {
//@StateObject var auth = AuthService()
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
ContentView() ContentView()
// .environmentObject(auth)
} }
} }
} }

Loading…
Cancel
Save