getting phase one of organizing top level auth

This commit is contained in:
talksik
2021-12-14 02:10:55 -08:00
parent ef2d859dd2
commit 62968fbbd2
5 changed files with 102 additions and 28 deletions
+15 -3
View File
@@ -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 = "<group>"; };
89288EBF276755DF00E962C4 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
89288ECA27675B9F00E962C4 /* OnboardingTemplateView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingTemplateView.swift; sourceTree = "<group>"; };
89B46E70276893E200B74255 /* AuthSessionStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthSessionStore.swift; sourceTree = "<group>"; };
89F1386C2767EB19006680ED /* SignInView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SignInView.swift; sourceTree = "<group>"; };
/* 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 = "<group>";
};
89F1386B2767EB07006680ED /* SignInview */ = {
89B46E6F276893C900B74255 /* Auth */ = {
isa = PBXGroup;
children = (
89B46E70276893E200B74255 /* AuthSessionStore.swift */,
);
path = Auth;
sourceTree = "<group>";
};
89F1386B2767EB07006680ED /* SignIn */ = {
isa = PBXGroup;
children = (
89F1386C2767EB19006680ED /* SignInView.swift */,
);
path = SignInview;
path = SignIn;
sourceTree = "<group>";
};
/* 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 */,
+67
View File
@@ -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
}
}
}
}
+12 -18
View File
@@ -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())
}
}
@@ -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
// ...
}
+3 -1
View File
@@ -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)
}
}
}