diff --git a/nirvana-ios.xcodeproj/project.pbxproj b/nirvana-ios.xcodeproj/project.pbxproj index dd5035e..6ba0b2b 100644 --- a/nirvana-ios.xcodeproj/project.pbxproj +++ b/nirvana-ios.xcodeproj/project.pbxproj @@ -30,6 +30,7 @@ 89288EC7276756DF00E962C4 /* FirebaseDatabase in Frameworks */ = {isa = PBXBuildFile; productRef = 89288EC6276756DF00E962C4 /* FirebaseDatabase */; }; 89288EC9276756DF00E962C4 /* FirebaseStorage in Frameworks */ = {isa = PBXBuildFile; productRef = 89288EC8276756DF00E962C4 /* FirebaseStorage */; }; 89288ECB27675B9F00E962C4 /* OnboardingTemplateView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89288ECA27675B9F00E962C4 /* OnboardingTemplateView.swift */; }; + 89B46E71276893E200B74255 /* AuthSessionStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89B46E70276893E200B74255 /* AuthSessionStore.swift */; }; 89D99D2E2766FD6B00237814 /* Liquid in Frameworks */ = {isa = PBXBuildFile; productRef = 89D99D2D2766FD6B00237814 /* Liquid */; }; 89F1386D2767EB19006680ED /* SignInView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 89F1386C2767EB19006680ED /* SignInView.swift */; }; 89F138702767F55F006680ED /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 89F1386F2767F55F006680ED /* GoogleSignIn */; }; @@ -57,6 +58,7 @@ 892179E52765FECD001E6C60 /* FooterControlsViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FooterControlsViewModel.swift; sourceTree = ""; }; 89288EBF276755DF00E962C4 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = ""; }; 89288ECA27675B9F00E962C4 /* OnboardingTemplateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingTemplateView.swift; sourceTree = ""; }; + 89B46E70276893E200B74255 /* AuthSessionStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSessionStore.swift; sourceTree = ""; }; 89F1386C2767EB19006680ED /* SignInView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInView.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -96,6 +98,7 @@ 891A15DF27653A7000B26659 /* nirvana-ios */ = { isa = PBXGroup; children = ( + 89B46E6F276893C900B74255 /* Auth */, 891A160C27657CD600B26659 /* Models */, 891A1609276572C600B26659 /* Globals */, 891A15F62765639B00B26659 /* Views */, @@ -129,7 +132,7 @@ 891A15F62765639B00B26659 /* Views */ = { isa = PBXGroup; children = ( - 89F1386B2767EB07006680ED /* SignInview */, + 89F1386B2767EB07006680ED /* SignIn */, 892179E22765FE93001E6C60 /* FooterControlsView */, 891A16052765726800B26659 /* Templates */, 891A16042765725D00B26659 /* WelcomeView */, @@ -201,12 +204,20 @@ path = FooterControlsView; sourceTree = ""; }; - 89F1386B2767EB07006680ED /* SignInview */ = { + 89B46E6F276893C900B74255 /* Auth */ = { + isa = PBXGroup; + children = ( + 89B46E70276893E200B74255 /* AuthSessionStore.swift */, + ); + path = Auth; + sourceTree = ""; + }; + 89F1386B2767EB07006680ED /* SignIn */ = { isa = PBXGroup; children = ( 89F1386C2767EB19006680ED /* SignInView.swift */, ); - path = SignInview; + path = SignIn; sourceTree = ""; }; /* End PBXGroup section */ @@ -300,6 +311,7 @@ 892179E62765FECD001E6C60 /* FooterControlsViewModel.swift in Sources */, 891A15F5276557AB00B26659 /* colors.swift in Sources */, 89288ECB27675B9F00E962C4 /* OnboardingTemplateView.swift in Sources */, + 89B46E71276893E200B74255 /* AuthSessionStore.swift in Sources */, 891A15F9276563CA00B26659 /* WelcomeView.swift in Sources */, 891A1611276590B000B26659 /* ChatsCarouselView.swift in Sources */, 891A16132765A44300B26659 /* ChatsCarouselViewModel.swift in Sources */, diff --git a/nirvana-ios/Auth/AuthSessionStore.swift b/nirvana-ios/Auth/AuthSessionStore.swift new file mode 100644 index 0000000..cae9db7 --- /dev/null +++ b/nirvana-ios/Auth/AuthSessionStore.swift @@ -0,0 +1,67 @@ +// +// AuthSessionStore.swift +// nirvana-ios +// +// Created by Arjun Patel on 12/14/21. +// + +import Foundation +import Firebase +import FirebaseAuth +import Combine + +enum SessionState { + case isAuthenticated + case isLoggedOut +} + +protocol SessionStore { + var sessionState:SessionState { get } + var user:User? { get } + + func signInOrCreateUser() + func logOut() + func unbind() +} + +final class AuthSessionStore: ObservableObject, SessionStore { + @Published var user : User? + @Published var sessionState: SessionState = SessionState.isLoggedOut + + private var handler: AuthStateDidChangeListenerHandle? + + init() { + self.setupAuthListen() + } + + func signInOrCreateUser() { + + } + + func logOut() { + + } + + func unbind () { + if let handle = self.handler { + Auth.auth().removeStateDidChangeListener(handle) + } + } +} + +private extension AuthSessionStore { + func setupAuthListen() { + // monitor authentication changes using firebase + self.handler = Auth.auth().addStateDidChangeListener { (auth, user) in + if let user = user { + // if we have a user, create a new user model + print("Got user: \(user)") + self.user = User( + _uid: user.uid, _email: user.email, _displayName: user.displayName, _profilePic: user.photoURL, _phoneNumber: user.phoneNumber) + } else { + // if we don't have a user, set our session to nil + self.user = nil + } + } + } +} diff --git a/nirvana-ios/ContentView.swift b/nirvana-ios/ContentView.swift index 4b6297e..4e8eff1 100644 --- a/nirvana-ios/ContentView.swift +++ b/nirvana-ios/ContentView.swift @@ -8,33 +8,27 @@ import SwiftUI struct ContentView: View { - @EnvironmentObject var authSession: AuthSessionStore + @EnvironmentObject var authSessionStore: AuthSessionStore var body: some View { NavigationView { - VStack { - NavigationLink(destination: WelcomeView()) { - Text("Welcome Page") + ZStack { + switch self.authSessionStore.sessionState { + case SessionState.isAuthenticated: + HomeView() + case SessionState.isLoggedOut: + WelcomeView() } +// NavigationLink("Welcome Page", isActive: self.authSessionStore.sessionState == SessionState.isLoggedOut, WelcomeView()) +// NavigationLink("HomePage", isActive: self.authSessionStore.sessionState == SessionState.isAuthenticated, destination: HomeView()) - NavigationLink(destination: HomeView()) { - Text("Home Page") - } - } - .navigationBarTitle("View All Pages", displayMode: .inline) - }.onAppear(perform: self.getUser) - -// .navigationViewStyle(StackNavigationViewStyle()) -// .environmentObject()//can pass anything to send to all views within navigation view - } - - func getUser () { - authSession.listen() + }.navigationBarHidden(true) + } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView() + ContentView().environmentObject(AuthSessionStore()) } } diff --git a/nirvana-ios/Views/SignInview/SignInView.swift b/nirvana-ios/Views/SignIn/SignInView.swift similarity index 94% rename from nirvana-ios/Views/SignInview/SignInView.swift rename to nirvana-ios/Views/SignIn/SignInView.swift index af9c37d..6b97257 100644 --- a/nirvana-ios/Views/SignInview/SignInView.swift +++ b/nirvana-ios/Views/SignIn/SignInView.swift @@ -69,8 +69,8 @@ struct SignInView: View { GIDSignIn.sharedInstance.signIn(with: config, presenting: getRootViewController()) {[self] user, err in - if let error = err { - print(err) + if err != nil { + print(err!.localizedDescription) return } @@ -85,8 +85,8 @@ struct SignInView: View { accessToken: authentication.accessToken) Auth.auth().signIn(with: credential) { result, error in - if let error = error { - print(error.localizedDescription) + if error != nil { + print(error!.localizedDescription) } guard let user = result?.user else { @@ -94,8 +94,7 @@ struct SignInView: View { } print(user.displayName ?? "Success!") - print(user.photoURL) - print(user.phoneNumber) + print(user) // User is signed in // ... } diff --git a/nirvana-ios/nirvana_iosApp.swift b/nirvana-ios/nirvana_iosApp.swift index 62dba93..06b4a23 100644 --- a/nirvana-ios/nirvana_iosApp.swift +++ b/nirvana-ios/nirvana_iosApp.swift @@ -11,10 +11,12 @@ import GoogleSignIn @main struct nirvana_iosApp: App { + @StateObject var authSessionStore: AuthSessionStore = AuthSessionStore() + @UIApplicationDelegateAdaptor private var appDelegate: AppDelegate var body: some Scene { WindowGroup { - ContentView() + ContentView().environmentObject(authSessionStore) } } }