Skip to content

Commit

Permalink
Merge pull request #1612 from Automattic/lantean/1611-updates-magic-l…
Browse files Browse the repository at this point in the history
…ink-endpoints

Magic Links: Wires new Endpoints
  • Loading branch information
jleandroperez committed Jul 15, 2024
2 parents 2614941 + 9507731 commit 94f66ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
9 changes: 4 additions & 5 deletions Simplenote/Classes/MagicLinkAuthenticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ private extension MagicLinkAuthenticator {

@discardableResult
func attemptLoginWithAuthCode(queryItems: [URLQueryItem]) -> Bool {
guard let authKey = queryItems.value(for: Constants.authKeyField),
guard let email = queryItems.base64DecodedValue(for: Constants.emailField),
let authCode = queryItems.value(for: Constants.authCodeField),
!authKey.isEmpty, !authCode.isEmpty
!email.isEmpty, !authCode.isEmpty
else {
return false
}

NSLog("[MagicLinkAuthenticator] Requesting SyncToken for \(authKey) and \(authCode)")
NSLog("[MagicLinkAuthenticator] Requesting SyncToken for \(email) and \(authCode)")
NotificationCenter.default.post(name: .magicLinkAuthWillStart, object: nil)

Task {
do {
let remote = LoginRemote()
let confirmation = try await remote.requestLoginConfirmation(authKey: authKey, authCode: authCode)
let confirmation = try await remote.requestLoginConfirmation(email: email, authCode: authCode)

Task { @MainActor in
NSLog("[MagicLinkAuthenticator] Should auth with token \(confirmation.syncToken)")
Expand Down Expand Up @@ -111,6 +111,5 @@ private struct AllowedHosts {
private struct Constants {
static let emailField = "email"
static let tokenField = "token"
static let authKeyField = "auth_key"
static let authCodeField = "auth_code"
}
14 changes: 7 additions & 7 deletions Simplenote/LoginRemote.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import Foundation
class LoginRemote: Remote {

func requestLoginEmail(email: String, completion: @escaping (_ result: Result<Data?, RemoteError>) -> Void) {
let request = requestForLoginRequest(with: email)
let request = requestForLoginRequest(email: email)
performDataTask(with: request, completion: completion)
}

func requestLoginConfirmation(authKey: String, authCode: String) async throws -> LoginConfirmationResponse {
let request = requestForLoginCompletion(authKey: authKey, authCode: authCode)
func requestLoginConfirmation(email: String, authCode: String) async throws -> LoginConfirmationResponse {
let request = requestForLoginCompletion(email: email, authCode: authCode)
return try await performDataTask(with: request, type: LoginConfirmationResponse.self)
}
}
Expand All @@ -28,19 +28,19 @@ struct LoginConfirmationResponse: Decodable {
//
private extension LoginRemote {

func requestForLoginRequest(with email: String) -> URLRequest {
func requestForLoginRequest(email: String) -> URLRequest {
let url = URL(string: SimplenoteConstants.loginRequestURL)!
return requestForURL(url, method: RemoteConstants.Method.POST, httpBody: [
"request_source": SimplenoteConstants.simplenotePlatformName,
"username": email.lowercased()
])
}

func requestForLoginCompletion(authKey: String, authCode: String) -> URLRequest {
func requestForLoginCompletion(email: String, authCode: String) -> URLRequest {
let url = URL(string: SimplenoteConstants.loginCompletionURL)!
return requestForURL(url, method: RemoteConstants.Method.POST, httpBody: [
"auth_key": authKey,
"auth_code": authCode
"auth_code": authCode,
"username": email
])
}
}

0 comments on commit 94f66ad

Please sign in to comment.