mirror of
https://github.com/omnivore-app/omnivore.git
synced 2026-03-11 08:54:26 +00:00
server returns a pendingEmailVerification boolean in auth payload
This commit is contained in:
parent
8b45095466
commit
c01d9a7021
7 changed files with 33 additions and 14 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<AuthPayload>(
|
||||
let resource = ServerResource<EmailAuthPayload>(
|
||||
urlRequest: urlRequest,
|
||||
decode: AuthPayload.decode
|
||||
decode: EmailAuthPayload.decode
|
||||
)
|
||||
|
||||
do {
|
||||
|
|
|
|||
|
|
@ -66,8 +66,8 @@ export async function createMobileEmailSignInResponse(
|
|||
name: user.name,
|
||||
})
|
||||
return {
|
||||
statusCode: 418,
|
||||
json: { errorCodes: ['PENDING_VERIFICATION'] },
|
||||
statusCode: 200,
|
||||
json: { pendingEmailVerification: true },
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue