Error display

auth_manage
Emre KARTAL 1 year ago
parent f59afdc193
commit e0954cba47

@ -15,6 +15,12 @@ struct Register: View {
@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 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
@ -42,91 +48,123 @@ 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: $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() .padding()
.background(Color.white.cornerRadius(9)) .background(Color.white.cornerRadius(9))
.frame(width: 300) .frame(width: 300)
.foregroundColor(.black)
.overlay( .overlay(
RoundedRectangle(cornerRadius: 9, style: .continuous) RoundedRectangle(cornerRadius: 9, style: .continuous)
.stroke(AllinColor.StrokeGrayColor, lineWidth: 1) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1)
) )
.foregroundColor(.black)
.padding(.bottom, 8) .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() .padding()
.background(Color.white.cornerRadius(9)) .background(Color.white.cornerRadius(9))
.frame(width: 300) .frame(width: 300)
.foregroundColor(.black)
.overlay( .overlay(
RoundedRectangle(cornerRadius: 9, style: .continuous) RoundedRectangle(cornerRadius: 9, style: .continuous)
.stroke(AllinColor.StrokeGrayColor, lineWidth: 1) .stroke(AllinColor.StrokeGrayColor, lineWidth: 1)
) )
.padding(.bottom, 8) .overlay(
HStack {
Group { Spacer()
if isPasswordVisible { Button(action: {
SecureField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) isPasswordVisible.toggle()
} else { }) {
TextField("", text: $password, prompt: Text("Mot de passe").foregroundColor(.gray)) Image(systemName: isPasswordVisible ? "eye.slash" : "eye")
.autocapitalization(.none) .foregroundColor(.gray)
} }
} .padding(.trailing, 8)
.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)
} }
.padding(.bottom, 8) )
.padding(.trailing, 8) .foregroundColor(.black)
} .padding(.bottom, 50)
)
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)
.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: { Button(action: {
register(email: email, username: username, password: password) 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)
@ -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() let api = AuthService()
api.register(email: email, password: password, username: username) { statusCode in api.register(email: email, password: password, username: username) { statusCode in
DispatchQueue.main.async { DispatchQueue.main.async {
if statusCode == 201 { if statusCode == 201 {
@ -175,4 +236,10 @@ struct Register: View {
} }
} }
func cleanError() {
errorPassword = false
errorMail = false
errorUsername = false
}
} }

Loading…
Cancel
Save