This commit is contained in:
Trey t
2025-11-05 20:31:01 -06:00
parent fe99f67f81
commit bc289d6c88
17 changed files with 819 additions and 203 deletions

View File

@@ -4,6 +4,7 @@ struct LoginView: View {
@StateObject private var viewModel = LoginViewModel()
@FocusState private var focusedField: Field?
@State private var showingRegister = false
@State private var showingVerifyEmail = false
enum Field {
case username, password
@@ -11,97 +12,107 @@ struct LoginView: View {
var body: some View {
NavigationView {
ZStack {
// Background
Color(.systemGroupedBackground)
.ignoresSafeArea()
Form {
Section {
VStack(spacing: 16) {
Image(systemName: "house.fill")
.font(.system(size: 60))
.foregroundStyle(.blue.gradient)
ScrollView {
VStack(spacing: 24) {
// Logo or App Name
LoginHeader()
Text("MyCrib")
.font(.largeTitle)
.fontWeight(.bold)
// Login Form
VStack(spacing: 16) {
// Username Field
VStack(alignment: .leading, spacing: 8) {
Text("Username")
.font(.subheadline)
.foregroundColor(.secondary)
Text("Manage your properties with ease")
.font(.subheadline)
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity)
.padding(.vertical)
}
.listRowBackground(Color.clear)
TextField("Enter your username", text: $viewModel.username)
.textFieldStyle(.roundedBorder)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.focused($focusedField, equals: .username)
.submitLabel(.next)
.onSubmit {
focusedField = .password
}
}
// Password Field
VStack(alignment: .leading, spacing: 8) {
Text("Password")
.font(.subheadline)
.foregroundColor(.secondary)
SecureField("Enter your password", text: $viewModel.password)
.textFieldStyle(.roundedBorder)
.focused($focusedField, equals: .password)
.submitLabel(.go)
.onSubmit {
viewModel.login()
}
}
// Error Message
if let errorMessage = viewModel.errorMessage {
ErrorMessageView(message: errorMessage, onDismiss: viewModel.clearError)
}
// Login Button
Button(action: viewModel.login) {
HStack {
if viewModel.isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
} else {
Text("Login")
.fontWeight(.semibold)
}
}
.frame(maxWidth: .infinity)
.padding()
.background(viewModel.isLoading ? Color.gray : Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.disabled(viewModel.isLoading)
// Forgot Password / Sign Up Links
HStack(spacing: 4) {
Text("Don't have an account?")
.font(.caption)
.foregroundColor(.secondary)
Button("Sign Up") {
showingRegister = true
}
.font(.caption)
.fontWeight(.semibold)
}
Section {
TextField("Username", text: $viewModel.username)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.focused($focusedField, equals: .username)
.submitLabel(.next)
.onSubmit {
focusedField = .password
}
.padding(.horizontal, 24)
SecureField("Password", text: $viewModel.password)
.focused($focusedField, equals: .password)
.submitLabel(.go)
.onSubmit {
viewModel.login()
}
} header: {
Text("Account Information")
}
if let errorMessage = viewModel.errorMessage {
Section {
HStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.red)
Text(errorMessage)
.foregroundColor(.red)
.font(.subheadline)
}
}
}
Section {
Button(action: viewModel.login) {
HStack {
Spacer()
if viewModel.isLoading {
ProgressView()
} else {
Text("Login")
.fontWeight(.semibold)
}
Spacer()
}
}
.disabled(viewModel.isLoading)
}
Section {
HStack {
Spacer()
Text("Don't have an account?")
.font(.subheadline)
.foregroundColor(.secondary)
Button("Sign Up") {
showingRegister = true
}
.font(.subheadline)
.fontWeight(.semibold)
Spacer()
}
}
.listRowBackground(Color.clear)
}
.navigationTitle("Welcome Back")
.navigationBarTitleDisplayMode(.inline)
.navigationBarTitleDisplayMode(.large)
.fullScreenCover(isPresented: $viewModel.isAuthenticated) {
MainTabView()
if viewModel.isVerified {
MainTabView()
} else {
VerifyEmailView(
onVerifySuccess: {
// After verification, show main tab view
viewModel.isVerified = true
},
onLogout: {
// Logout and dismiss verification screen
viewModel.logout()
}
)
}
}
.sheet(isPresented: $showingRegister) {
RegisterView()

View File

@@ -10,7 +10,9 @@ class LoginViewModel: ObservableObject {
@Published var isLoading: Bool = false
@Published var errorMessage: String?
@Published var isAuthenticated: Bool = false
@Published var isVerified: Bool = false
@Published var currentUser: User?
// MARK: - Private Properties
private let authApi: AuthApi
private let tokenStorage: TokenStorage
@@ -77,6 +79,10 @@ class LoginViewModel: ObservableObject {
let user = results.data?.user {
self.tokenStorage.saveToken(token: token)
// Store user data and verification status
self.currentUser = user
self.isVerified = user.verified
// Initialize lookups repository after successful login
LookupsManager.shared.initialize()
@@ -85,7 +91,7 @@ class LoginViewModel: ObservableObject {
self.isLoading = false
print("Login successful! Token: token")
print("User: \(user.username)")
print("User: \(user.username), Verified: \(user.verified)")
}
}

View File

@@ -1,9 +1,11 @@
import SwiftUI
import ComposeApp
struct RegisterView: View {
@StateObject private var viewModel = RegisterViewModel()
@Environment(\.dismiss) var dismiss
@FocusState private var focusedField: Field?
@State private var showVerifyEmail = false
enum Field {
case username, email, password, confirmPassword
@@ -11,114 +13,99 @@ struct RegisterView: View {
var body: some View {
NavigationView {
ZStack {
Color(.systemGroupedBackground)
.ignoresSafeArea()
Form {
Section {
VStack(spacing: 16) {
Image(systemName: "person.badge.plus")
.font(.system(size: 60))
.foregroundStyle(.blue.gradient)
ScrollView {
VStack(spacing: 24) {
// Icon and Title
RegisterHeader()
Text("Join MyCrib")
.font(.largeTitle)
.fontWeight(.bold)
// Registration Form
VStack(spacing: 16) {
// Username Field
VStack(alignment: .leading, spacing: 8) {
Text("Username")
.font(.subheadline)
.foregroundColor(.secondary)
TextField("Enter your username", text: $viewModel.username)
.textFieldStyle(.roundedBorder)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.focused($focusedField, equals: .username)
.submitLabel(.next)
.onSubmit {
focusedField = .email
}
}
// Email Field
VStack(alignment: .leading, spacing: 8) {
Text("Email")
.font(.subheadline)
.foregroundColor(.secondary)
TextField("Enter your email", text: $viewModel.email)
.textFieldStyle(.roundedBorder)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.keyboardType(.emailAddress)
.focused($focusedField, equals: .email)
.submitLabel(.next)
.onSubmit {
focusedField = .password
}
}
// Password Field
VStack(alignment: .leading, spacing: 8) {
Text("Password")
.font(.subheadline)
.foregroundColor(.secondary)
SecureField("Enter your password", text: $viewModel.password)
.textFieldStyle(.roundedBorder)
.focused($focusedField, equals: .password)
.submitLabel(.next)
.onSubmit {
focusedField = .confirmPassword
}
}
// Confirm Password Field
VStack(alignment: .leading, spacing: 8) {
Text("Confirm Password")
.font(.subheadline)
.foregroundColor(.secondary)
SecureField("Confirm your password", text: $viewModel.confirmPassword)
.textFieldStyle(.roundedBorder)
.focused($focusedField, equals: .confirmPassword)
.submitLabel(.go)
.onSubmit {
viewModel.register()
}
}
// Error Message
if let errorMessage = viewModel.errorMessage {
ErrorMessageView(message: errorMessage, onDismiss: viewModel.clearError)
}
// Register Button
Button(action: viewModel.register) {
HStack {
if viewModel.isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
} else {
Text("Create Account")
.fontWeight(.semibold)
}
}
.frame(maxWidth: .infinity)
.padding()
.background(viewModel.isLoading ? Color.gray : Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
.disabled(viewModel.isLoading)
}
.padding(.horizontal, 24)
Spacer()
Text("Start managing your properties today")
.font(.subheadline)
.foregroundColor(.secondary)
}
.frame(maxWidth: .infinity)
.padding(.vertical)
}
.listRowBackground(Color.clear)
Section {
TextField("Username", text: $viewModel.username)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.focused($focusedField, equals: .username)
.submitLabel(.next)
.onSubmit {
focusedField = .email
}
TextField("Email", text: $viewModel.email)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
.keyboardType(.emailAddress)
.focused($focusedField, equals: .email)
.submitLabel(.next)
.onSubmit {
focusedField = .password
}
} header: {
Text("Account Information")
}
Section {
SecureField("Password", text: $viewModel.password)
.focused($focusedField, equals: .password)
.submitLabel(.next)
.onSubmit {
focusedField = .confirmPassword
}
SecureField("Confirm Password", text: $viewModel.confirmPassword)
.focused($focusedField, equals: .confirmPassword)
.submitLabel(.go)
.onSubmit {
viewModel.register()
}
} header: {
Text("Security")
} footer: {
Text("Password must be secure")
}
if let errorMessage = viewModel.errorMessage {
Section {
HStack {
Image(systemName: "exclamationmark.triangle.fill")
.foregroundColor(.red)
Text(errorMessage)
.foregroundColor(.red)
.font(.subheadline)
}
}
}
Section {
Button(action: viewModel.register) {
HStack {
Spacer()
if viewModel.isLoading {
ProgressView()
} else {
Text("Create Account")
.fontWeight(.semibold)
}
Spacer()
}
}
.disabled(viewModel.isLoading)
}
}
.navigationTitle("Create Account")
.navigationBarTitleDisplayMode(.inline)
.navigationBarTitleDisplayMode(.large)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button("Cancel") {
@@ -127,7 +114,18 @@ struct RegisterView: View {
}
}
.fullScreenCover(isPresented: $viewModel.isRegistered) {
MainTabView()
VerifyEmailView(
onVerifySuccess: {
dismiss()
showVerifyEmail = false
},
onLogout: {
// Logout and return to login screen
TokenManager().clearToken()
LookupsManager.shared.clear()
dismiss()
}
)
}
}
}

View File

@@ -0,0 +1,158 @@
import SwiftUI
struct VerifyEmailView: View {
@StateObject private var viewModel = VerifyEmailViewModel()
@FocusState private var isFocused: Bool
var onVerifySuccess: () -> Void
var onLogout: () -> Void
var body: some View {
NavigationView {
ZStack {
Color(.systemGroupedBackground)
.ignoresSafeArea()
ScrollView {
VStack(spacing: 24) {
Spacer().frame(height: 20)
// Header
VStack(spacing: 12) {
Image(systemName: "envelope.badge.shield.half.filled")
.font(.system(size: 60))
.foregroundStyle(.blue.gradient)
.padding(.bottom, 8)
Text("Verify Your Email")
.font(.title)
.fontWeight(.bold)
Text("You must verify your email address to continue")
.font(.subheadline)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal)
}
// Info Card
GroupBox {
HStack(spacing: 12) {
Image(systemName: "exclamationmark.shield.fill")
.foregroundColor(.orange)
.font(.title2)
Text("Email verification is required. Check your inbox for a 6-digit code.")
.font(.subheadline)
.foregroundColor(.primary)
.fontWeight(.semibold)
}
.padding(.vertical, 4)
}
.padding(.horizontal)
// Code Input
VStack(alignment: .leading, spacing: 12) {
Text("Verification Code")
.font(.headline)
.padding(.horizontal)
TextField("000000", text: $viewModel.code)
.font(.system(size: 32, weight: .semibold, design: .rounded))
.multilineTextAlignment(.center)
.keyboardType(.numberPad)
.textFieldStyle(.roundedBorder)
.frame(height: 60)
.padding(.horizontal)
.focused($isFocused)
.onChange(of: viewModel.code) { _, newValue in
// Limit to 6 digits
if newValue.count > 6 {
viewModel.code = String(newValue.prefix(6))
}
// Only allow numbers
viewModel.code = newValue.filter { $0.isNumber }
}
Text("Code must be 6 digits")
.font(.caption)
.foregroundColor(.secondary)
.padding(.horizontal)
}
// Error Message
if let errorMessage = viewModel.errorMessage {
ErrorMessageView(message: errorMessage, onDismiss: viewModel.clearError)
.padding(.horizontal)
}
// Verify Button
Button(action: {
viewModel.verifyEmail()
}) {
HStack {
if viewModel.isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
} else {
Image(systemName: "checkmark.shield.fill")
Text("Verify Email")
.fontWeight(.semibold)
}
}
.frame(maxWidth: .infinity)
.frame(height: 50)
.background(
viewModel.code.count == 6 && !viewModel.isLoading
? Color.blue
: Color.gray.opacity(0.3)
)
.foregroundColor(.white)
.cornerRadius(12)
}
.disabled(viewModel.code.count != 6 || viewModel.isLoading)
.padding(.horizontal)
Spacer().frame(height: 20)
// Help Text
Text("Didn't receive the code? Check your spam folder or contact support.")
.font(.caption)
.foregroundColor(.secondary)
.multilineTextAlignment(.center)
.padding(.horizontal, 32)
}
}
}
.navigationBarTitleDisplayMode(.inline)
.navigationBarBackButtonHidden(true)
.interactiveDismissDisabled(true)
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: onLogout) {
HStack(spacing: 4) {
Image(systemName: "rectangle.portrait.and.arrow.right")
.font(.system(size: 16))
Text("Logout")
.font(.subheadline)
}
}
}
}
.onAppear {
isFocused = true
}
.onChange(of: viewModel.isVerified) { _, isVerified in
if isVerified {
onVerifySuccess()
}
}
}
}
}
#Preview {
VerifyEmailView(
onVerifySuccess: { print("Verified!") },
onLogout: { print("Logout") }
)
}

View File

@@ -0,0 +1,89 @@
import Foundation
import ComposeApp
import Combine
@MainActor
class VerifyEmailViewModel: ObservableObject {
// MARK: - Published Properties
@Published var code: String = ""
@Published var isLoading: Bool = false
@Published var errorMessage: String?
@Published var isVerified: Bool = false
// MARK: - Private Properties
private let authApi: AuthApi
private let tokenStorage: TokenStorage
// MARK: - Initialization
init() {
self.authApi = AuthApi(client: ApiClient_iosKt.createHttpClient())
self.tokenStorage = TokenStorage()
self.tokenStorage.initialize(manager: TokenManager.init())
}
// MARK: - Public Methods
func verifyEmail() {
// Validation
guard code.count == 6 else {
errorMessage = "Please enter a valid 6-digit code"
return
}
guard code.allSatisfy({ $0.isNumber }) else {
errorMessage = "Code must contain only numbers"
return
}
guard let token = tokenStorage.getToken() else {
errorMessage = "Not authenticated"
return
}
isLoading = true
errorMessage = nil
let request = VerifyEmailRequest(code: code)
authApi.verifyEmail(token: token, request: request) { result, error in
if let successResult = result as? ApiResultSuccess<VerifyEmailResponse> {
self.handleSuccess(results: successResult)
return
}
if let errorResult = result as? ApiResultError {
self.handleError(message: errorResult.message)
return
}
if let error = error {
self.handleError(message: error.localizedDescription)
return
}
self.isLoading = false
print("Unknown error during email verification")
}
}
@MainActor
func handleError(message: String) {
self.isLoading = false
self.errorMessage = message
print("Verification error: \(message)")
}
@MainActor
func handleSuccess(results: ApiResultSuccess<VerifyEmailResponse>) {
if let verified = results.data?.verified, verified {
self.isVerified = true
self.isLoading = false
print("Email verification successful!")
} else {
self.handleError(message: "Verification failed")
}
}
func clearError() {
errorMessage = nil
}
}