You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.8 KiB
122 lines
2.8 KiB
//
|
|
// LoginView.swift
|
|
// PadelClub
|
|
//
|
|
// Created by Laurent Morvillier on 19/02/2024.
|
|
//
|
|
|
|
import SwiftUI
|
|
import LeStorage
|
|
|
|
struct LoginView: View {
|
|
|
|
@EnvironmentObject var dataStore: DataStore
|
|
|
|
@State var username: String = "razmig"
|
|
@State var password: String = "StaxKikoo12"
|
|
|
|
@State var showEmailPopup: Bool = false
|
|
|
|
@State var error: Error? = nil
|
|
|
|
var handler: (User) -> ()
|
|
|
|
var body: some View {
|
|
|
|
Form {
|
|
|
|
TextField("Username", text: self.$username)
|
|
.autocorrectionDisabled()
|
|
.textInputAutocapitalization(.never)
|
|
SecureField("Password", text: self.$password)
|
|
|
|
Section {
|
|
Button(action: {
|
|
self._login()
|
|
}, label: {
|
|
Text("Login")
|
|
})
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
|
|
Section {
|
|
NavigationLink("Sign up") {
|
|
UserCreationView()
|
|
}
|
|
Button(action: {
|
|
self.showEmailPopup = true
|
|
}, label: {
|
|
Text("Forgotten password")
|
|
})
|
|
.alert(
|
|
Text("Password reset"),
|
|
isPresented: self.$showEmailPopup
|
|
) {
|
|
EmailConfirmationView()
|
|
} message: {
|
|
Text("Please enter your email")
|
|
}
|
|
}
|
|
|
|
}
|
|
.navigationTitle("Login")
|
|
}
|
|
|
|
fileprivate func _login() {
|
|
guard let service = Store.main.service else {
|
|
return
|
|
}
|
|
Task {
|
|
do {
|
|
let user: User = try await service.login(
|
|
username: self.username,
|
|
password: self.password)
|
|
self.dataStore.setUser(user)
|
|
self.handler(user)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
struct EmailConfirmationView: View {
|
|
|
|
@State var email: String = ""
|
|
|
|
var body: some View {
|
|
VStack {
|
|
TextField("Email", text: self.$email)
|
|
.keyboardType(.emailAddress)
|
|
Button {
|
|
self._forgottenPassword()
|
|
} label: {
|
|
Text("Send email")
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
fileprivate func _forgottenPassword() {
|
|
|
|
Task {
|
|
do {
|
|
try await Store.main.service?.forgotPassword(email: self.email)
|
|
} catch {
|
|
Logger.error(error)
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
#Preview {
|
|
NavigationStack {
|
|
LoginView(handler: { _ in })
|
|
}
|
|
}
|
|
|
|
#Preview {
|
|
EmailConfirmationView()
|
|
}
|
|
|