Merge pull request #75 from writeas/add-https-prefix-on-login-if-missing

Add secure protocol to server string if it's missing
This commit is contained in:
Angelo Stavrow 2020-09-28 10:13:18 -04:00 committed by GitHub
commit c56c3c898b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 15 deletions

View File

@ -65,11 +65,10 @@ struct AccountLoginView: View {
.padding()
}
}
.alert(isPresented: $model.account.hasError) {
guard let accountError = model.account.currentError else { fatalError() }
return Alert(
.alert(isPresented: $model.isPresentingLoginErrorAlert) {
Alert(
title: Text("Error Logging In"),
message: Text(accountError.localizedDescription),
message: Text(model.loginErrorMessage ?? "An unknown error occurred while trying to login."),
dismissButton: .default(Text("OK"))
)
}

View File

@ -37,12 +37,7 @@ struct AccountModel {
var server: String = ""
var username: String = ""
var hasError: Bool = false
var currentError: AccountError? {
didSet {
hasError = true
}
}
private(set) var user: WFUser?
private(set) var isLoggedIn: Bool = false

View File

@ -26,11 +26,14 @@ class WriteFreelyModel: ObservableObject {
}
}
@Published var isPresentingDeleteAlert: Bool = false
@Published var isPresentingLoginErrorAlert: Bool = false
@Published var postToDelete: WFAPost?
#if os(iOS)
@Published var isPresentingSettingsView: Bool = false
#endif
var loginErrorMessage: String?
// swiftlint:disable line_length
let helpURL = URL(string: "https://discuss.write.as/c/help/5")!
let licensesURL = URL(string: "https://github.com/writeas/writefreely-swiftui-multiplatform/tree/main/Shared/Resources/Licenses")!
@ -79,9 +82,20 @@ class WriteFreelyModel: ObservableObject {
extension WriteFreelyModel {
func login(to server: URL, as username: String, password: String) {
let secureProtocolPrefix = "https://"
let insecureProtocolPrefix = "http://"
var serverString = server.absoluteString
// If there's neither an http or https prefix, prepend "https://" to the server string.
if !(serverString.hasPrefix(secureProtocolPrefix) || serverString.hasPrefix(insecureProtocolPrefix)) {
serverString = secureProtocolPrefix + serverString
}
// If the server string is prefixed with http, upgrade to https before attempting to login.
if serverString.hasPrefix(insecureProtocolPrefix) {
serverString = serverString.replacingOccurrences(of: insecureProtocolPrefix, with: secureProtocolPrefix)
}
isLoggingIn = true
account.server = server.absoluteString
client = WFClient(for: server)
account.server = serverString
client = WFClient(for: URL(string: serverString)!)
client?.login(username: username, password: password, completion: loginHandler)
}
@ -177,17 +191,25 @@ private extension WriteFreelyModel {
}
} catch WFError.notFound {
DispatchQueue.main.async {
self.account.currentError = AccountError.usernameNotFound
self.loginErrorMessage = AccountError.usernameNotFound.localizedDescription
self.isPresentingLoginErrorAlert = true
}
} catch WFError.unauthorized {
DispatchQueue.main.async {
self.account.currentError = AccountError.invalidPassword
self.loginErrorMessage = AccountError.invalidPassword.localizedDescription
self.isPresentingLoginErrorAlert = true
}
} catch {
if (error as NSError).domain == NSURLErrorDomain,
(error as NSError).code == -1003 {
DispatchQueue.main.async {
self.account.currentError = AccountError.serverNotFound
self.loginErrorMessage = AccountError.serverNotFound.localizedDescription
self.isPresentingLoginErrorAlert = true
}
} else {
DispatchQueue.main.async {
self.loginErrorMessage = error.localizedDescription
self.isPresentingLoginErrorAlert = true
}
}
}