adding logic for getting user details on log in

This commit is contained in:
talksik
2021-12-18 20:10:51 -08:00
parent 7933c46f38
commit 48c433a7a3
3 changed files with 58 additions and 15 deletions
+22 -3
View File
@@ -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 {
@@ -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..<Avatars.avatarSystemNames.count) { index in
Image(Avatars.avatarSystemNames[index])
.resizable()
.scaledToFit()
.frame(width: 100, height: 100, alignment: .center)
.blur(radius: self.selectedAvatarIndex == index ? 0 : 4)
.blur(radius: self.selectedAvatarIndex == index ? 0 : 1)
.scaleEffect(self.selectedAvatarIndex == index ? 1.3 : 1)
.onTapGesture {
print("selected another avatar: \(index)")
self.selectedAvatarIndex = index
}
.animation(.spring())
}
}
}.padding(.top, 20)
}
.padding()
// input first and last name
VStack {
@@ -88,10 +102,17 @@ struct AddBasicInfoView: View {
Spacer()
// button to save information
Button {
print("saving information")
// activate loading splashscreen
// let view model do work of saving to firestore
// note: I need these updated attributes to be listened to when loading the circle hub next
// but maybe just change the data in cache and no need to re-fetch
} label: {
Text("Save")
.fontWeight(.heavy)
@@ -102,6 +123,8 @@ struct AddBasicInfoView: View {
.clipShape(Capsule())
.shadow(radius:10)
}
.offset(x: 0, y: self.firstName.count == 0 || self.lastName.count == 0 ? 200 : 0)
.animation(.spring())
.padding()
}
.frame(maxHeight: .infinity)
@@ -10,7 +10,8 @@ import FirebaseAuth
final class PhoneVerificationViewModel : ObservableObject {
private var firestoreService = FirestoreService()
// TODO: add alert message handler here that publishes changes
// TODO: add alert message handler here that publishes changes to ui based on business logic errors
public func verifyPhoneAndSendSMS(phoneNumber: String) {
// TODO: do some string validation here