From c01d9a70214b21edd8b965427e11cf1702aa32a2 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Fri, 29 Jul 2022 12:06:07 -0700 Subject: [PATCH] server returns a pendingEmailVerification boolean in auth payload --- .../Registration/EmailAuth/EmailAuthView.swift | 3 +++ .../EmailAuth/EmailLoginFormView.swift | 8 +++++++- .../Authentication/AccountCreator.swift | 17 ++++++++++++----- .../Services/Authentication/AuthModels.swift | 5 +++++ .../DataService/Networking/ServerResource.swift | 4 +--- .../VerifyAuthProviderToken.swift | 6 +++--- packages/api/src/routers/auth/mobile/sign_in.ts | 4 ++-- 7 files changed, 33 insertions(+), 14 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift index 18b69768d..df8f5f10e 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift @@ -9,6 +9,7 @@ enum EmailAuthState { case signIn case signUp case loading + case pendingEmailVerification } @MainActor final class EmailAuthViewModel: ObservableObject { @@ -35,6 +36,8 @@ struct EmailAuthView: View { EmailSignupFormView(viewModel: viewModel) case .signIn: EmailLoginFormView(viewModel: viewModel) + case .pendingEmailVerification: + Text("Verify Your email") case .loading: VStack { Spacer() diff --git a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailLoginFormView.swift b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailLoginFormView.swift index 40756a017..3c8c0991d 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailLoginFormView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailLoginFormView.swift @@ -13,7 +13,13 @@ extension EmailAuthViewModel { do { try await authenticator.submitEmailLogin(email: email, password: password) } catch { - loginError = error as? LoginError + if let newLoginError = error as? LoginError { + if newLoginError == .pendingEmailVerification { + emailAuthState = .pendingEmailVerification + } else { + loginError = newLoginError + } + } } } } diff --git a/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift b/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift index e58b9b1e9..88f57583d 100644 --- a/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift +++ b/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift @@ -54,11 +54,18 @@ public extension Authenticator { ) async throws { do { let params = EmailSignInParams(email: email, password: password) - let authPayload = try await networker.submitEmailLogin(params: params) - try ValetKey.authCookieString.setValue(authPayload.commentedAuthCookieString) - try ValetKey.authToken.setValue(authPayload.authToken) - DispatchQueue.main.async { - self.isLoggedIn = true + let emailAuthPayload = try await networker.submitEmailLogin(params: params) + + if let authPayload = emailAuthPayload.authPayload { + try ValetKey.authCookieString.setValue(authPayload.commentedAuthCookieString) + try ValetKey.authToken.setValue(authPayload.authToken) + DispatchQueue.main.async { + self.isLoggedIn = true + } + } else if emailAuthPayload.errorCodes != nil { + throw ServerError.pendingEmailVerification + } else { + throw ServerError.unknown } } catch { let serverError = (error as? ServerError) ?? ServerError.unknown diff --git a/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift b/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift index 97e165415..ac4135f1f 100644 --- a/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift +++ b/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift @@ -33,6 +33,11 @@ struct AuthPayload: Decodable { let authToken: String } +struct EmailAuthPayload: Decodable { + let authPayload: AuthPayload? + let pendingEmailVerification: Bool? +} + struct CreateAccountParams: Encodable { let pendingUserToken: String let userProfile: UserProfile diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResource.swift b/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResource.swift index 331bac709..4bf7f11df 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResource.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResource.swift @@ -42,10 +42,9 @@ extension URLSession { do { let (data, response) = try await data(for: resource.urlRequest) let serverResponse = ServerResponse(data: data, response: response) + NetworkRequestLogger.log(request: resource.urlRequest, serverResponse: serverResponse) if let httpResponse = response as? HTTPURLResponse, 200 ..< 300 ~= httpResponse.statusCode { - NetworkRequestLogger.log(request: resource.urlRequest, serverResponse: serverResponse) - if let decodedValue = resource.decode(serverResponse) { return decodedValue } @@ -56,7 +55,6 @@ extension URLSession { } } catch { let serverResponse = ServerResponse(error: error) - NetworkRequestLogger.log(request: resource.urlRequest, serverResponse: serverResponse) throw ServerError(serverResponse: serverResponse) } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift b/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift index 3943d5807..382eb7b4a 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift @@ -37,7 +37,7 @@ extension Networker { } } - func submitEmailLogin(params: EmailSignInParams) async throws -> AuthPayload { + func submitEmailLogin(params: EmailSignInParams) async throws -> EmailAuthPayload { let encodedParams = (try? JSONEncoder().encode(params)) ?? Data() let urlRequest = URLRequest.create( @@ -46,9 +46,9 @@ extension Networker { requestMethod: .post(params: encodedParams) ) - let resource = ServerResource( + let resource = ServerResource( urlRequest: urlRequest, - decode: AuthPayload.decode + decode: EmailAuthPayload.decode ) do { diff --git a/packages/api/src/routers/auth/mobile/sign_in.ts b/packages/api/src/routers/auth/mobile/sign_in.ts index 8902ff482..699c38aa0 100644 --- a/packages/api/src/routers/auth/mobile/sign_in.ts +++ b/packages/api/src/routers/auth/mobile/sign_in.ts @@ -66,8 +66,8 @@ export async function createMobileEmailSignInResponse( name: user.name, }) return { - statusCode: 418, - json: { errorCodes: ['PENDING_VERIFICATION'] }, + statusCode: 200, + json: { pendingEmailVerification: true }, } }