having real time user listened to in authlistener
This commit is contained in:
@@ -18,6 +18,25 @@ class FirestoreService {
|
|||||||
|
|
||||||
private var db = Firestore.firestore()
|
private var db = Firestore.firestore()
|
||||||
|
|
||||||
|
func getUserRealtime(userId: String, completion: @escaping((_ user: User?) -> ())) {
|
||||||
|
db.collection(Collection.users.rawValue).document(userId).addSnapshotListener { documentSnapshot, error in
|
||||||
|
guard let document = documentSnapshot else {
|
||||||
|
print("Error fetching getting user realtime listener: \(error!)")
|
||||||
|
completion(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let updatedOrNewUser = try? document.data(as: User.self)
|
||||||
|
else {
|
||||||
|
completion(nil)
|
||||||
|
print("Document data was empty. tried fetching updated user document in firestore service")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// up to date user
|
||||||
|
completion(updatedOrNewUser)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getUser(userId: String, completion: @escaping((_ user: User?) -> ())) {
|
func getUser(userId: String, completion: @escaping((_ user: User?) -> ())) {
|
||||||
let docRef = db.collection(Collection.users.rawValue).document(userId)
|
let docRef = db.collection(Collection.users.rawValue).document(userId)
|
||||||
|
|
||||||
|
|||||||
@@ -116,7 +116,6 @@ final class AuthSessionStore: ObservableObject, SessionStore {
|
|||||||
|
|
||||||
func setupAuthListen() {
|
func setupAuthListen() {
|
||||||
// monitor authentication changes using firebase
|
// monitor authentication changes using firebase
|
||||||
print("let's see what user looks like in authsession at at the start: \(self.user)")
|
|
||||||
self.handler = Auth.auth().addStateDidChangeListener { [weak self] res, user in
|
self.handler = Auth.auth().addStateDidChangeListener { [weak self] res, user in
|
||||||
print("auth listener activated")
|
print("auth listener activated")
|
||||||
|
|
||||||
@@ -130,13 +129,11 @@ final class AuthSessionStore: ObservableObject, SessionStore {
|
|||||||
print("auth listener: Got user: \(uid)")
|
print("auth listener: Got user: \(uid)")
|
||||||
print("auth listener: phone number: \(user?.phoneNumber)")
|
print("auth listener: phone number: \(user?.phoneNumber)")
|
||||||
|
|
||||||
// get user from firestore and store in environment
|
// when we have a user get signed in, we can activate the listener for fetching the user's data to keep our auth session store always up to date for all of the ui
|
||||||
self.firestoreService.getUser(userId: uid) {[weak self] firestoreUser in
|
self.firestoreService.getUserRealtime(userId: uid) {[weak self] realtimeUpdatedUser in
|
||||||
// if we get a user back, then set this to our authsessionstore for our views
|
if realtimeUpdatedUser != nil {
|
||||||
print("user from firestore in auth listener: \(firestoreUser)")
|
print("up to date user: \(realtimeUpdatedUser)")
|
||||||
if firestoreUser != nil {
|
self?.user = realtimeUpdatedUser
|
||||||
self?.user = firestoreUser
|
|
||||||
print("lets see if authsessionstore user is being set: \(self?.user)")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -90,7 +90,7 @@ struct AddBasicInfoView: View {
|
|||||||
.font(.subheadline)
|
.font(.subheadline)
|
||||||
.multilineTextAlignment(.center)
|
.multilineTextAlignment(.center)
|
||||||
|
|
||||||
Text("What does your friends call you?")
|
Text("What do your friends call you?")
|
||||||
.font(.footnote)
|
.font(.footnote)
|
||||||
.foregroundColor(Color.black.opacity(0.7))
|
.foregroundColor(Color.black.opacity(0.7))
|
||||||
|
|
||||||
@@ -129,6 +129,13 @@ struct AddBasicInfoView: View {
|
|||||||
}
|
}
|
||||||
.frame(maxHeight: .infinity)
|
.frame(maxHeight: .infinity)
|
||||||
}
|
}
|
||||||
|
.onAppear {
|
||||||
|
// get current user nickname and avatar if they have one
|
||||||
|
self.nickname = self.authSessionStore.user?.nickname ?? ""
|
||||||
|
if self.authSessionStore.user?.avatar != nil { // if user has previously selected an avatar
|
||||||
|
self.selectedAvatarIndex = Avatars.avatarSystemNames.firstIndex(of: (self.authSessionStore.user?.avatar)!) ?? 0 // 0 in case the system names doesn't have it anymore
|
||||||
|
}
|
||||||
|
}
|
||||||
//TODO: hook up the alert with the view model
|
//TODO: hook up the alert with the view model
|
||||||
// .alert(isPresented: Binding<Bool>(
|
// .alert(isPresented: Binding<Bool>(
|
||||||
// get: { self.addInfoViewModel.state == ProfileRegistrationState.failed},
|
// get: { self.addInfoViewModel.state == ProfileRegistrationState.failed},
|
||||||
|
|||||||
@@ -41,6 +41,8 @@ class AddBasicInfoViewModel : ObservableObject {
|
|||||||
|
|
||||||
// save in firestore
|
// save in firestore
|
||||||
self.firestoreService.updateUser(user: currUser) {res in
|
self.firestoreService.updateUser(user: currUser) {res in
|
||||||
|
print(res)
|
||||||
|
|
||||||
switch res {
|
switch res {
|
||||||
case .success:
|
case .success:
|
||||||
self.state = .successful
|
self.state = .successful
|
||||||
|
|||||||
@@ -344,17 +344,21 @@ struct InnerCircleView: View {
|
|||||||
.font(.title2)
|
.font(.title2)
|
||||||
|
|
||||||
Menu {
|
Menu {
|
||||||
|
Button("Edit Profile") {
|
||||||
|
self.navigationStack.push(AddBasicInfoView())
|
||||||
|
}
|
||||||
|
|
||||||
|
Button("Friends") {
|
||||||
|
print("manage friends page")
|
||||||
|
}
|
||||||
|
|
||||||
Button("Log out") {
|
Button("Log out") {
|
||||||
print("log out button clicked")
|
print("log out button clicked")
|
||||||
|
|
||||||
// have this send it
|
// have this send it
|
||||||
self.authSessionStore.logOut()
|
self.authSessionStore.logOut()
|
||||||
|
|
||||||
// once logged out, then the listener will listen and change the page but we will go there first
|
// once logged out, then the listener will listen and router view will take care of us thereafter
|
||||||
self.navigationStack.push(ContentView())
|
|
||||||
}
|
|
||||||
Button("Friends") {
|
|
||||||
print("manage friends page")
|
|
||||||
}
|
}
|
||||||
} label: {
|
} label: {
|
||||||
Image("Artboards_Diversity_Avatars_by_Netguru-51")
|
Image("Artboards_Diversity_Avatars_by_Netguru-51")
|
||||||
|
|||||||
@@ -35,19 +35,24 @@ struct RouterView: View {
|
|||||||
if self.authSessionStore.user != nil {
|
if self.authSessionStore.user != nil {
|
||||||
if self.authSessionStore.user?.nickname == nil && self.authSessionStore.user?.avatar == nil {
|
if self.authSessionStore.user?.nickname == nil && self.authSessionStore.user?.avatar == nil {
|
||||||
OnboardingTrioView()
|
OnboardingTrioView()
|
||||||
|
.transition(.slide)
|
||||||
} else {// user is existing user/up and running user -> signs in is pushed nicely right to hub
|
} else {// user is existing user/up and running user -> signs in is pushed nicely right to hub
|
||||||
InnerCircleView()
|
InnerCircleView()
|
||||||
|
.transition(.slide)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
OnboardingTrioView()
|
OnboardingTrioView()
|
||||||
|
.transition(.slide)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if self.authSessionStore.sessionState == SessionState.isLoggedOut {
|
} else if self.authSessionStore.sessionState == SessionState.isLoggedOut {
|
||||||
// go to welcome page at which point they can go and follow the programmatic
|
// go to welcome page at which point they can go and follow the programmatic
|
||||||
// line to the sigin and so on and so forth
|
// line to the sigin and so on and so forth
|
||||||
WelcomeView()
|
WelcomeView()
|
||||||
|
.transition(.slide)
|
||||||
} else {
|
} else {
|
||||||
SplashView()
|
SplashView()
|
||||||
|
.transition(.slide)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user