From 48c433a7a3f8c1bb8944d72d03f9555ec20f7985 Mon Sep 17 00:00:00 2001 From: talksik Date: Sat, 18 Dec 2021 20:10:51 -0800 Subject: [PATCH] adding logic for getting user details on log in --- nirvana-ios/Services/AuthSessionStore.swift | 25 +++++++++-- .../Views/AddBasicInfo/AddBasicInfoView.swift | 45 ++++++++++++++----- .../PhoneVerificationViewModel.swift | 3 +- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/nirvana-ios/Services/AuthSessionStore.swift b/nirvana-ios/Services/AuthSessionStore.swift index 001f210..ec9b3c0 100644 --- a/nirvana-ios/Services/AuthSessionStore.swift +++ b/nirvana-ios/Services/AuthSessionStore.swift @@ -34,6 +34,8 @@ final class AuthSessionStore: ObservableObject, SessionStore { private var handler: AuthStateDidChangeListenerHandle? + private var firestoreService: FirestoreService = FirestoreService() + init() { // create google sign in configuration object let clientID = FirebaseApp.app()?.options.clientID @@ -41,6 +43,8 @@ final class AuthSessionStore: ObservableObject, SessionStore { print("the google client id: prolly shouldn't be printing this: \(clientID)") + + self.setupAuthListen() } @@ -112,17 +116,23 @@ final class AuthSessionStore: ObservableObject, SessionStore { func setupAuthListen() { // monitor authentication changes using firebase - self.handler = Auth.auth().addStateDidChangeListener { (auth, user) in + self.handler = Auth.auth().addStateDidChangeListener { [weak self] res, user in print("auth listener activated") + guard let self = self else { return } + + // get the user from the auth table in firebase auth if let user = user { // if we have a user, create a new user model - print("Got user: \(user.uid)") - print("phone number: \(user.phoneNumber!)") + print("auth listener: Got user: \(user.uid)") + print("auth listener: phone number: \(user.phoneNumber!)") // TODO: go to firestore and get full user document for the current authenticated user // and have this in environment for all pages to access without having to fetch all the time + + self.getAndSetEnvironmentUserDetails(userId: user.uid) + self.sessionState = .isAuthenticated } else { // if we don't have a user, set our session to nil @@ -131,6 +141,15 @@ final class AuthSessionStore: ObservableObject, SessionStore { } } } + + private func getAndSetEnvironmentUserDetails(userId: String) { + let firestoreUser: User? = self.firestoreService.getUser(userId: userId) + + // if we get a user back, then set this to our instance to allow UI to get published with all user details + if firestoreUser != nil { + self.user = firestoreUser + } + } } extension UIApplication { diff --git a/nirvana-ios/Views/AddBasicInfo/AddBasicInfoView.swift b/nirvana-ios/Views/AddBasicInfo/AddBasicInfoView.swift index b7fad7c..19e30f8 100644 --- a/nirvana-ios/Views/AddBasicInfo/AddBasicInfoView.swift +++ b/nirvana-ios/Views/AddBasicInfo/AddBasicInfoView.swift @@ -16,6 +16,11 @@ struct AddBasicInfoView: View { @State var selectedAvatarIndex: Int = 0 + var columns: [GridItem] = + Array( + repeating: GridItem(.fixed(100), spacing: 0, alignment: .center), + count: 5) + var body: some View { ZStack { // bg @@ -40,30 +45,39 @@ struct AddBasicInfoView: View { // header logo area LogoHeaderView() - Text("Let's Build Your Profile") - .font(.title) - .fontWeight(.medium) - .foregroundColor(NirvanaColor.teal) - .multilineTextAlignment(.center) + // action text + VStack(alignment: .leading) { + Text("Let's Build A Profile") + .font(.title) + .fontWeight(.medium) + .foregroundColor(NirvanaColor.teal) + .multilineTextAlignment(.center) + + Text("Select an avatar you like, then input your first and last name! You can always change this later.") + .font(.subheadline) + .foregroundColor(Color.black.opacity(0.7)) + } + .padding() // Horizontal scroll view to allow user to select the avatar he or she wants - ScrollView(.horizontal, showsIndicators: false) { - LazyHStack { + ScrollView([.horizontal, .vertical], showsIndicators: false) { + LazyVGrid(columns: columns) { ForEach(0..