mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
add ios network code to submit email sign up request
This commit is contained in:
parent
c01d9a7021
commit
0178e4a137
6 changed files with 74 additions and 6 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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<PendingEmailVerificationAuthPayload>(
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue