at a place where I just have to debug slowly the process

This commit is contained in:
talksik
2021-12-17 19:10:01 -08:00
parent f4eef85175
commit e240d73b1f
3 changed files with 84 additions and 22 deletions
+1 -1
View File
@@ -15,7 +15,7 @@ struct ContentView: View {
NavigationStackView { NavigationStackView {
switch self.authSessionStore.sessionState { switch self.authSessionStore.sessionState {
case SessionState.isAuthenticated: case SessionState.isAuthenticated:
HomeView() InnerCircleView()
case SessionState.isLoggedOut: case SessionState.isLoggedOut:
WelcomeView() WelcomeView()
} }
@@ -10,11 +10,13 @@ import NavigationStack
import Combine import Combine
// TODO: maybe this is useless and just do my own toasts and alerts // TODO: maybe this is useless and just do my own toasts and alerts
import PopupView import PopupView
import Firebase
import FirebaseAuth
// user can type in a phone number and then receive a text // user can type in a phone number and then receive a text
struct PhoneVerificationView: View { struct PhoneVerificationView: View {
@State var phoneNumber = "" @State var phoneNumber = ""
@State var ccode = "+1" @State var ccode = "1"
@EnvironmentObject private var navigationStack: NavigationStack @EnvironmentObject private var navigationStack: NavigationStack
// Alert settings // Alert settings
@@ -29,7 +31,7 @@ struct PhoneVerificationView: View {
OnboardingTemplateView(hdrText: "Let's get you verified", imgName: "undraw_my_password_d-6-kg", bottomActArea: AnyView( OnboardingTemplateView(hdrText: "Let's get you verified", imgName: "undraw_my_password_d-6-kg", bottomActArea: AnyView(
VStack { VStack {
HStack { HStack {
Text(ccode) Text("+\(ccode)")
.padding() .padding()
.background(.ultraThinMaterial) .background(.ultraThinMaterial)
.clipShape(Circle()) .clipShape(Circle())
@@ -61,21 +63,43 @@ struct PhoneVerificationView: View {
.padding(.vertical, 20) .padding(.vertical, 20)
Button { Button {
print("send verification") print("send sms")
// do auth stuff let concatPhoneNumber = "+"+self.ccode+self.phoneNumber
// then async send to next page // do auth stuff from firebase
self.navigationStack.push(PhoneVerificationCodeView()) // verify code page
PhoneAuthProvider.provider()
.verifyPhoneNumber(concatPhoneNumber, uiDelegate: nil) { verificationID, error in
// problem verifying number showing alert...maybe fake number or something from user
if error != nil {
self.toastText = (error?.localizedDescription)!
self.showToast.toggle()
return
}
// Sign in using the verificationID and the code sent to the user
// ...
UserDefaults.standard.set(verificationID, forKey: "authVerificationID")
// then async send to next page
self.navigationStack.push(PhoneVerificationCodeView()) // verify code page
}
} label: { } label: {
Text("Send Verification") VStack {
.bold() Text("Send Verification")
.foregroundColor(NirvanaColor.white) .fontWeight(.heavy)
.frame(maxWidth: .infinity) .foregroundColor(NirvanaColor.white)
.padding(.vertical, 20) .frame(maxWidth: .infinity)
.background(NirvanaColor.teal) .padding(.vertical, 20)
.clipShape(Capsule()) .background(self.phoneNumber.count == 10 ? NirvanaColor.teal : Color.white.opacity(0.2)) // show dull button if didn't enter full phone number
.shadow(radius:10) .clipShape(Capsule())
.shadow(radius:10)
.animation(.default)
Text("You might receive an SMS message for verification and standard rates apply.")
.font(.footnote)
.foregroundColor(Color.black.opacity(0.7))
}
} }
} }
)) ))
@@ -118,6 +142,10 @@ struct PhoneVerificationCodeView: View {
@State var verificationCode = "" @State var verificationCode = ""
// Alert settings
@State var showToast = false
@State var toastText = "There was an error in verifying your account"
var body: some View { var body: some View {
ZStack(alignment: .topLeading) { ZStack(alignment: .topLeading) {
Color.clear Color.clear
@@ -133,21 +161,47 @@ struct PhoneVerificationCodeView: View {
.font(.subheadline) .font(.subheadline)
.multilineTextAlignment(.center) .multilineTextAlignment(.center)
.padding(.vertical) .padding(.vertical)
// .onReceive(Just(self.$verificationCode)) {newValue in // .onReceive(Just(self.$verificationCode)) { newValue in
// let filtered = newValue.filter { // let filtered = newValue.filter { "0123456789".contains($0) }
// "0123456789".contains($0) //
// }
// if filtered != newValue { // if filtered != newValue {
// self.$verificationCode = filtered // self.verificationCode = filtered
// } // }
// } // }
Button { Button {
print("Verify") print("verify code")
// do auth stuff // do auth stuff
// get it from the previous screen but from storage
let verificationID = UserDefaults.standard.string(forKey: "authVerificationID")
if verificationID == nil {
self.toastText = "There was an error validating your code."
self.showToast.toggle()
}
// if we got a value, then continue with verifying their code
let credential = PhoneAuthProvider.provider().credential(
withVerificationID: verificationID!,
verificationCode: self.verificationCode
)
// firebase will now verify the put together credential
Auth.auth().signIn(with: credential) { (res, err) in
if err != nil {
self.toastText = (err?.localizedDescription)!
self.showToast.toggle()
return
}
UserDefaults.standard.set(true, forKey: "authVerificationID")
}
// then async send to next page // then async send to next page
self.navigationStack.push(PhoneVerificationCodeView()) // verify code page self.navigationStack.push(PhoneVerificationCodeView()) // verify code page
} label: { } label: {
Text("Send Verification") Text("Send Verification")
@@ -174,6 +228,13 @@ struct PhoneVerificationCodeView: View {
.padding() .padding()
} }
} }
.alert(isPresented: self.$showToast) {
Alert(
title: Text(self.toastText),
message: Text("Go back and try again or re-enter phone number."),
dismissButton: .default(Text("Got it!"))
)
}
} }
} }
@@ -12,6 +12,7 @@ final class PhoneVerificationViewModel : ObservableObject {
} }
extension String { extension String {
// custom function to give (949)920-0392 and get out the right thing
func applyPatternOnNumbers(pattern: String, replacementCharacter: Character) -> String { func applyPatternOnNumbers(pattern: String, replacementCharacter: Character) -> String {
var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression) var pureNumber = self.replacingOccurrences( of: "[^0-9]", with: "", options: .regularExpression)
for index in 0 ..< pattern.count { for index in 0 ..< pattern.count {