tatsuyamoriguchi / Sleep-Tracer

1 stars 0 forks source link

Add User Logout function. #28

Closed tatsuyamoriguchi closed 7 months ago

tatsuyamoriguchi commented 7 months ago

Users can initiate a logout action from within the app. Upon logout, the app clears the user's authentication token or session data, effectively logging the user out.

tatsuyamoriguchi commented 7 months ago

Certainly! In SwiftUI, you can achieve this by using the @EnvironmentObject property wrapper to share data across different views. Here's a basic example of how you can implement a logout functionality that takes you back to the login view when tapping on a tab item:

First, create an ObservableObject class to hold the authentication state:

import SwiftUI

class AuthState: ObservableObject {
    @Published var isLoggedIn = false

    func logout() {
        // Perform any necessary cleanup tasks when logging out
        isLoggedIn = false
    }
}

Then, create your login view and main tab view:

import SwiftUI

struct LoginView: View {
    @EnvironmentObject var authState: AuthState

    var body: some View {
        Button(action: {
            self.authState.isLoggedIn = true
        }) {
            Text("Log In")
        }
    }
}

struct ContentView: View {
    @EnvironmentObject var authState: AuthState

    var body: some View {
        TabView {
            Text("Home View")
                .tabItem {
                    Image(systemName: "house")
                    Text("Home")
                }

            Text("Profile View")
                .tabItem {
                    Image(systemName: "person")
                    Text("Profile")
                }
                .onTapGesture {
                    self.authState.logout()
                }
        }
        .sheet(isPresented: $authState.isLoggedIn) {
            LoginView()
        }
    }
}

In this example:

Make sure to set up your SwiftUI app with the @main attribute and inject the AuthState object into the environment using .environmentObject(AuthState()). This will make the AuthState object available to all views in the app.

tatsuyamoriguchi commented 7 months ago
  1. Enter a user credential in Login view.
  2. Press Login button.
  3. Run time error: Crash "SwiftUI/EnvironmentObject.swift:90: Fatal error: No ObservableObject of type AuthenticationManager found. A View.environmentObject(_:) for AuthenticationManager may be missing as an ancestor of this view."
tatsuyamoriguchi commented 7 months ago

Login() if authManager.isLoggedIn == true { returns "SwiftUI/EnvironmentObject.swift:90: Fatal error: No ObservableObject of type AuthenticationManager found. A View.environmentObject(_:) for AuthenticationManager may be missing as an ancestor of this view."

Not working

tatsuyamoriguchi commented 7 months ago

Learn the login logic and code to go from button clicked to another view, Home, in Swift UI. Learn TabView how to use Tab item to logout and back to the login view.

tatsuyamoriguchi commented 7 months ago

Moved @ObservedObject var authManager = AuthenticationManager() outside of Login() view Use Text().onTapGesture on Logout tab item. Show a user a view to confirm "Logout"