From a41126b0e73d2bf1f0854769059ef5ff526a94ed Mon Sep 17 00:00:00 2001 From: Eric Andrews Date: Wed, 17 Jan 2024 02:44:46 -0500 Subject: [PATCH] Eric/sopuli login fix (#861) --- Mlem/API/APIClient/APIClient.swift | 21 +++++++++++---------- Mlem/Views/Tabs/Profile/UserView.swift | 10 ++++++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Mlem/API/APIClient/APIClient.swift b/Mlem/API/APIClient/APIClient.swift index 1572ae4bd..d3fd5b270 100644 --- a/Mlem/API/APIClient/APIClient.swift +++ b/Mlem/API/APIClient/APIClient.swift @@ -90,8 +90,8 @@ class APIClient { } @discardableResult - func perform(request: Request) async throws -> Request.Response { - let urlRequest = try urlRequest(from: request) + func perform(request: Request, overrideToken: String? = nil) async throws -> Request.Response { + let urlRequest = try urlRequest(from: request, overrideToken: overrideToken) let (data, response) = try await execute(urlRequest) @@ -153,13 +153,15 @@ class APIClient { } } - private func urlRequest(from defintion: any APIRequest) throws -> URLRequest { + private func urlRequest(from defintion: any APIRequest, overrideToken: String?) throws -> URLRequest { var urlRequest = URLRequest(url: defintion.endpoint) defintion.headers.forEach { header in urlRequest.setValue(header.value, forHTTPHeaderField: header.key) } - if case let .authenticated(_, token) = session { + if let overrideToken { + urlRequest.setValue("Bearer \(overrideToken)", forHTTPHeaderField: "Authorization") + } else if case let .authenticated(_, token) = session { urlRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization") } @@ -210,7 +212,7 @@ extension APIClient { // so an external session is required, as the expectation is the client will not yet have a session or // the session will be for another account. let request = try GetPersonDetailsRequest(session: session, username: username) - return try await perform(request: request) + return try await perform(request: request, overrideToken: session.token) } func getPersonDetails(for personId: Int, limit: Int?, savedOnly: Bool) async throws -> GetPersonDetailsResponse { @@ -292,8 +294,7 @@ extension APIClient { func saveUserSettings( myUserInfo info: APIMyUserInfo ) async throws -> SuccessResponse { - - // Despite all values being optional, we actually have to provide all values + // Despite all values being optional, we actually have to provide all values // here otherwise Lemmy returns 'user_already_exists'. Possibly fixed >0.19.0 // https://github.com/LemmyNet/lemmy/issues/4076 @@ -324,10 +325,10 @@ extension APIClient { showReadPosts: localUser.showReadPosts, showScores: localUser.showScores, theme: localUser.theme, - auth: try session.token + auth: session.token ) ) - return SuccessResponse(from: try await perform(request: request)) + return try await SuccessResponse(from: perform(request: request)) } @discardableResult @@ -423,7 +424,7 @@ extension APIClient { totpToken: totpToken ) - return try await perform(request: request) + return try await perform(request: request, overrideToken: "") } @discardableResult diff --git a/Mlem/Views/Tabs/Profile/UserView.swift b/Mlem/Views/Tabs/Profile/UserView.swift index 2af52885b..889364f42 100644 --- a/Mlem/Views/Tabs/Profile/UserView.swift +++ b/Mlem/Views/Tabs/Profile/UserView.swift @@ -5,8 +5,8 @@ // Created by Sjmarf on 27/12/2023. // -import SwiftUI import Dependencies +import SwiftUI // swiftlint:disable type_body_length struct UserView: View { @@ -90,7 +90,6 @@ struct UserView: View { Divider() .padding(.bottom, AppConstants.postAndCommentSpacing) MarkdownView(text: bio, isNsfw: false, alignment: bioAlignment).padding(AppConstants.postAndCommentSpacing) - } HStack { Label(cakeDayFormatter.string(from: user.creationDate), systemImage: Icons.cakeDay) @@ -174,7 +173,9 @@ struct UserView: View { } .task(priority: .userInitiated) { if isLoadingContent { - await tryReloadUser() + Task { + await tryReloadUser() + } } } .onChange(of: user.userId) { _ in @@ -272,7 +273,7 @@ struct UserView: View { } @ViewBuilder - func flairBackground(color: Color, @ViewBuilder content: () -> Content) -> some View { + func flairBackground(color: Color, @ViewBuilder content: () -> some View) -> some View { content() .foregroundStyle(color) .padding(.vertical, 8) @@ -283,4 +284,5 @@ struct UserView: View { .padding(.horizontal, AppConstants.postAndCommentSpacing) } } + // swiftlint:enable type_body_length