From 0178e4a1376e5ec076700b49829f25f72d6c4558 Mon Sep 17 00:00:00 2001 From: Satindar Dhillon Date: Sat, 30 Jul 2022 09:10:41 -0700 Subject: [PATCH] add ios network code to submit email sign up request --- .../EmailAuth/EmailAuthView.swift | 8 +++++- .../EmailAuth/EmailSignupFormView.swift | 13 +++++++--- .../Authentication/AccountCreator.swift | 18 ++++++++++++- .../Services/Authentication/AuthModels.swift | 11 ++++++++ .../VerifyAuthProviderToken.swift | 25 +++++++++++++++++++ .../Sources/Services/Keychain/ValetKey.swift | 5 ++++ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift index df8f5f10e..8d9d01068 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailAuthView.swift @@ -22,7 +22,13 @@ enum EmailAuthState { func loadAuthState() { // check tokens here to determine pending/active/no user - emailAuthState = .signIn + if PublicValet.hasPendingEmailVerificationToken { + // TODO: make network request to check status + // if it's now active then log in the user\ + emailAuthState = .pendingEmailVerification + } else { + emailAuthState = .signIn + } } } diff --git a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailSignupFormView.swift b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailSignupFormView.swift index 53215af01..ff62bd52d 100644 --- a/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailSignupFormView.swift +++ b/apple/OmnivoreKit/Sources/App/Views/Registration/EmailAuth/EmailSignupFormView.swift @@ -9,13 +9,18 @@ extension EmailAuthViewModel { func signUp( email: String, password: String, - username _: String, - fullName _: String, + username: String, + fullName: String, authenticator: Authenticator ) async { do { - // TODO: add function to sign up - try await authenticator.submitEmailLogin(email: email, password: password) + try await authenticator.submitUserSignUp( + email: email, + password: password, + username: username, + name: fullName + ) + emailAuthState = .pendingEmailVerification } catch { loginError = error as? LoginError } diff --git a/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift b/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift index 88f57583d..140ff5a4c 100644 --- a/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift +++ b/apple/OmnivoreKit/Sources/Services/Authentication/AccountCreator.swift @@ -62,7 +62,7 @@ public extension Authenticator { DispatchQueue.main.async { self.isLoggedIn = true } - } else if emailAuthPayload.errorCodes != nil { + } else if emailAuthPayload.pendingEmailVerification == true { throw ServerError.pendingEmailVerification } else { throw ServerError.unknown @@ -72,4 +72,20 @@ public extension Authenticator { throw LoginError.make(serverError: serverError) } } + + func submitUserSignUp( + email: String, + password: String, + username: String, + name: String + ) async throws { + do { + let params = EmailSignUpParams(email: email, password: password, username: username, name: name) + let authPayload = try await networker.submitEmailSignUp(params: params) + try ValetKey.authTokenWithPendingEmail.setValue(authPayload.pendingEmailVerificationToken) + } catch { + let serverError = (error as? ServerError) ?? ServerError.unknown + throw LoginError.make(serverError: serverError) + } + } } diff --git a/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift b/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift index ac4135f1f..e20acb7f1 100644 --- a/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift +++ b/apple/OmnivoreKit/Sources/Services/Authentication/AuthModels.swift @@ -23,6 +23,13 @@ struct EmailSignInParams: Encodable { let password: String } +struct EmailSignUpParams: Encodable { + let email: String + let password: String + let username: String + let name: String +} + enum AuthProvider: String, Encodable { case apple = "APPLE" case google = "GOOGLE" @@ -48,6 +55,10 @@ struct PendingUserAuthPayload: Decodable { let pendingUserProfile: UserProfile } +struct PendingEmailVerificationAuthPayload: Decodable { + let pendingEmailVerificationToken: String +} + extension AuthPayload { var commentedAuthCookieString: String { authCookieString.replacingOccurrences(of: "HttpOnly", with: "comment=ios-webview-cookie; HttpOnly") diff --git a/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift b/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift index 382eb7b4a..c469dd955 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/Networking/ServerResources/VerifyAuthProviderToken.swift @@ -61,4 +61,29 @@ extension Networker { } } } + + func submitEmailSignUp(params: EmailSignUpParams) async throws -> PendingEmailVerificationAuthPayload { + let encodedParams = (try? JSONEncoder().encode(params)) ?? Data() + + let urlRequest = URLRequest.create( + baseURL: appEnvironment.serverBaseURL, + urlPath: "/api/mobile-auth/email-sign-up", + requestMethod: .post(params: encodedParams) + ) + + let resource = ServerResource( + urlRequest: urlRequest, + decode: PendingEmailVerificationAuthPayload.decode + ) + + do { + return try await urlSession.performRequest(resource: resource) + } catch { + if let error = error as? ServerError { + throw LoginError.make(serverError: error) + } else { + throw LoginError.unknown + } + } + } } diff --git a/apple/OmnivoreKit/Sources/Services/Keychain/ValetKey.swift b/apple/OmnivoreKit/Sources/Services/Keychain/ValetKey.swift index e4daea999..752279604 100644 --- a/apple/OmnivoreKit/Sources/Services/Keychain/ValetKey.swift +++ b/apple/OmnivoreKit/Sources/Services/Keychain/ValetKey.swift @@ -10,10 +10,15 @@ public enum PublicValet { public static var authToken: String? { ValetKey.authToken.value() } + + public static var hasPendingEmailVerificationToken: Bool { + ValetKey.authTokenWithPendingEmail.exists + } } enum ValetKey: String { case authToken = "app.omnivore.valet.auth-token" + case authTokenWithPendingEmail = "app.omnivore.valet.auth-token-with-pending-email" case authCookieString = "app.omnivore.valet.auth-cookie-raw-string" case appEnvironmentString = "app.omnivore.valet.app-environment" }