Skip to content

some initial code for showing authentication with new customer accounts #212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
2147F3E42B502AA9005546F3 /* ShopifyCheckoutSheetKit in Frameworks */ = {isa = PBXBuildFile; productRef = 2147F3E32B502AA9005546F3 /* ShopifyCheckoutSheetKit */; };
2147F3E62B502AFD005546F3 /* checkout-sheet-kit-swift in Resources */ = {isa = PBXBuildFile; fileRef = 2147F3E52B502AFD005546F3 /* checkout-sheet-kit-swift */; };
2722118C2CF77C3600A4D881 /* LoginView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2722118B2CF77C3600A4D881 /* LoginView.swift */; };
27F6B1BF2CF9E30500AA2A59 /* AuthenticationWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F6B1BE2CF9E30200AA2A59 /* AuthenticationWebView.swift */; };
4EA7F9B62A9D2B9D003276A1 /* SettingsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EA7F9B42A9D2B9D003276A1 /* SettingsViewController.swift */; };
4EBBA76B2A5F0CE200193E19 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EBBA76A2A5F0CE200193E19 /* AppDelegate.swift */; };
4EBBA76D2A5F0CE200193E19 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4EBBA76C2A5F0CE200193E19 /* SceneDelegate.swift */; };
Expand All @@ -33,10 +35,18 @@
6A3D7ADC2B8E01460010EB27 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 6A3D7ADB2B8E01460010EB27 /* Localizable.xcstrings */; };
6A774DD12B58023400C8EF7E /* CountryCode+inferRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6A774DD02B58023400C8EF7E /* CountryCode+inferRegion.swift */; };
86250DE42AD5521C002E45C2 /* AppConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86250DE32AD5521C002E45C2 /* AppConfiguration.swift */; };
C0408BA32C82217F00F263EC /* LoginWebView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0408BA22C82217F00F263EC /* LoginWebView.swift */; };
C0408BA52C82706F00F263EC /* LoginWebViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0408BA42C82706F00F263EC /* LoginWebViewController.swift */; };
C0408BA72C85B69A00F263EC /* JwtDecoder.swift in Sources */ = {isa = PBXBuildFile; fileRef = C0408BA62C85B69A00F263EC /* JwtDecoder.swift */; };
C053D5902C81EAF7003E30D4 /* CustomerAccountClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = C053D58F2C81EAF7003E30D4 /* CustomerAccountClient.swift */; };
C053D5952C82110E003E30D4 /* LoginViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = C053D5942C82110E003E30D4 /* LoginViewController.swift */; };
C053D5992C821435003E30D4 /* LoginViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = C053D5982C821435003E30D4 /* LoginViewController.xib */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
2147F3E52B502AFD005546F3 /* checkout-sheet-kit-swift */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "checkout-sheet-kit-swift"; path = ../..; sourceTree = "<group>"; };
2722118B2CF77C3600A4D881 /* LoginView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginView.swift; sourceTree = "<group>"; };
27F6B1BE2CF9E30200AA2A59 /* AuthenticationWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationWebView.swift; sourceTree = "<group>"; };
4EA7F9B42A9D2B9D003276A1 /* SettingsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsViewController.swift; sourceTree = "<group>"; };
4EBBA7672A5F0CE200193E19 /* MobileBuyIntegration.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MobileBuyIntegration.app; sourceTree = BUILT_PRODUCTS_DIR; };
4EBBA76A2A5F0CE200193E19 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -63,6 +73,12 @@
6A774DD02B58023400C8EF7E /* CountryCode+inferRegion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CountryCode+inferRegion.swift"; sourceTree = "<group>"; };
6AE865492CE3BB6500A4971C /* MobileBuyIntegration.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = MobileBuyIntegration.entitlements; sourceTree = "<group>"; };
86250DE32AD5521C002E45C2 /* AppConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppConfiguration.swift; sourceTree = "<group>"; };
C0408BA22C82217F00F263EC /* LoginWebView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginWebView.swift; sourceTree = "<group>"; };
C0408BA42C82706F00F263EC /* LoginWebViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginWebViewController.swift; sourceTree = "<group>"; };
C0408BA62C85B69A00F263EC /* JwtDecoder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JwtDecoder.swift; sourceTree = "<group>"; };
C053D58F2C81EAF7003E30D4 /* CustomerAccountClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomerAccountClient.swift; sourceTree = "<group>"; };
C053D5942C82110E003E30D4 /* LoginViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoginViewController.swift; sourceTree = "<group>"; };
C053D5982C821435003E30D4 /* LoginViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = LoginViewController.xib; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -125,9 +141,11 @@
6A3393622CEF742200E89FAA /* CheckoutController.swift */,
6A2E77BF2CE6186F0067062D /* Cart.swift */,
6A2E77BD2CE606400067062D /* Catalog.swift */,
C053D5932C820913003E30D4 /* Login */,
4EBBA76A2A5F0CE200193E19 /* AppDelegate.swift */,
86250DE32AD5521C002E45C2 /* AppConfiguration.swift */,
4EBBA76C2A5F0CE200193E19 /* SceneDelegate.swift */,
2722118B2CF77C3600A4D881 /* LoginView.swift */,
4EF54F232A6F456B00F5E407 /* CartManager.swift */,
4EBBA7A92A5F124F00193E19 /* StorefrontClient.swift */,
4EBBA76E2A5F0CE200193E19 /* ProductView.swift */,
Expand Down Expand Up @@ -167,6 +185,20 @@
name = Extensions;
sourceTree = "<group>";
};
C053D5932C820913003E30D4 /* Login */ = {
isa = PBXGroup;
children = (
27F6B1BE2CF9E30200AA2A59 /* AuthenticationWebView.swift */,
C053D5942C82110E003E30D4 /* LoginViewController.swift */,
C053D5982C821435003E30D4 /* LoginViewController.xib */,
C053D58F2C81EAF7003E30D4 /* CustomerAccountClient.swift */,
C0408BA22C82217F00F263EC /* LoginWebView.swift */,
C0408BA42C82706F00F263EC /* LoginWebViewController.swift */,
C0408BA62C85B69A00F263EC /* JwtDecoder.swift */,
);
name = Login;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -239,6 +271,7 @@
6A0FA77E2CE4D7F7003070F8 /* MobileBuyIntegration.entitlements in Resources */,
4EBBA7772A5F0CE200193E19 /* LaunchScreen.storyboard in Resources */,
6A3D7ADC2B8E01460010EB27 /* Localizable.xcstrings in Resources */,
C053D5992C821435003E30D4 /* LoginViewController.xib in Resources */,
4EBBA7742A5F0CE200193E19 /* Assets.xcassets in Resources */,
2147F3E62B502AFD005546F3 /* checkout-sheet-kit-swift in Resources */,
);
Expand Down Expand Up @@ -291,21 +324,28 @@
buildActionMask = 2147483647;
files = (
4EBBA7AE2A5F1BBF00193E19 /* UIImageView+URL.swift in Sources */,
C0408BA32C82217F00F263EC /* LoginWebView.swift in Sources */,
6A34672F2B5FFEFB007314A8 /* WebPixelEventsView.swift in Sources */,
4EBBA7B02A5F222F00193E19 /* MoneyV2+Format.swift in Sources */,
6A2E77BE2CE606490067062D /* Catalog.swift in Sources */,
4EF54F242A6F456B00F5E407 /* CartManager.swift in Sources */,
4EBBA76F2A5F0CE200193E19 /* ProductView.swift in Sources */,
4EF54F272A6F4C4F00F5E407 /* CartViewController.swift in Sources */,
4EBBA76B2A5F0CE200193E19 /* AppDelegate.swift in Sources */,
C0408BA52C82706F00F263EC /* LoginWebViewController.swift in Sources */,
86250DE42AD5521C002E45C2 /* AppConfiguration.swift in Sources */,
4EBBA7AA2A5F124F00193E19 /* StorefrontClient.swift in Sources */,
6A257A152AFBB06300610DA5 /* Logger.swift in Sources */,
4EBBA76D2A5F0CE200193E19 /* SceneDelegate.swift in Sources */,
C053D5952C82110E003E30D4 /* LoginViewController.swift in Sources */,
C0408BA72C85B69A00F263EC /* JwtDecoder.swift in Sources */,
6A2E77C02CE618720067062D /* Cart.swift in Sources */,
6A3393652CEF9E8100E89FAA /* Theme.swift in Sources */,
6A774DD12B58023400C8EF7E /* CountryCode+inferRegion.swift in Sources */,
27F6B1BF2CF9E30500AA2A59 /* AuthenticationWebView.swift in Sources */,
C053D5902C81EAF7003E30D4 /* CustomerAccountClient.swift in Sources */,
4EA7F9B62A9D2B9D003276A1 /* SettingsViewController.swift in Sources */,
2722118C2CF77C3600A4D881 /* LoginView.swift in Sources */,
6A3467332B600E64007314A8 /* LogsView.swift in Sources */,
6A257A132AFBA78500610DA5 /* LogReader.swift in Sources */,
6A3393632CEF742500E89FAA /* CheckoutController.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public final class AppConfiguration: ObservableObject {
/// Prefill buyer information
@Published public var useVaultedState: Bool = false

/// Pass in customerAccessToken after user logs in
public var useAuthenticatedState: Bool = true

/// Logger to retain Web Pixel events
let webPixelsLogger = FileLogger("analytics.txt")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import SwiftUI
@preconcurrency import WebKit

struct AuthenticationWebView: UIViewRepresentable {
let url: URL?
let authData: AuthData?
let redirectUri: URLComponents?
let completionHandler: (String?) -> Void

func makeUIView(context: Context) -> LoginWebView {
let webView = LoginWebView()
webView.navigationDelegate = context.coordinator
return webView
}

func updateUIView(_ uiView: LoginWebView, context _: Context) {
guard let nonNilUrl = url else {
print("No authorization URL")
return
}

let request = URLRequest(url: nonNilUrl)
uiView.load(request)
}

func makeCoordinator() -> Coordinator {
Coordinator(
authData: authData,
redirectUri: redirectUri,
completionHandler: completionHandler
)
}

class Coordinator: NSObject, WKNavigationDelegate {
private var authData: AuthData?
private var redirectUri: URLComponents?
private var completionHandler: (String?) -> Void

init(authData: AuthData?, redirectUri: URLComponents?, completionHandler: @escaping (String?) -> Void) {
self.completionHandler = completionHandler
self.authData = authData
self.redirectUri = redirectUri
}

func webView(_: WKWebView, decidePolicyFor action: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
guard let nonNullRedirectUrl = redirectUri, let nonNullAuthData = authData else {
decisionHandler(.allow)
completionHandler(nil)
return
}

if let url = action.request.url {
guard let redirectComponents = URLComponents(url: url, resolvingAgainstBaseURL: true) else {
decisionHandler(.allow)
return
}

guard redirectComponents.scheme == nonNullRedirectUrl.scheme else {
decisionHandler(.allow)
return
}

CustomerAccountClient.shared.handleAuthorizationCodeRedirect(url, authData: nonNullAuthData, callback: { token, _ in

guard let nonNilToken = token else {
print("login failed")

return
}
self.completionHandler(nonNilToken)
})
decisionHandler(.cancel)
return
}

decisionHandler(.allow)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,15 @@ class CartManager: ObservableObject {
}

private func performCartCreate(items: [GraphQL.ID] = [], handler: @escaping CartResultHandler) {
let input = (appConfiguration.useVaultedState) ? vaultedStateCart(items) : defaultCart(items)
let sfApiAccessToken = CustomerAccountClient.shared.sfApiAccessToken

let input = if appConfiguration.useAuthenticatedState && sfApiAccessToken != nil {
authenticatedCart(sfApiAccessToken!, items)
} else if appConfiguration.useVaultedState {
vaultedStateCart(items)
} else {
defaultCart(items)
}

let countryCode: Storefront.CountryCode = appConfiguration.useVaultedState
? ((Storefront.CountryCode(rawValue: Bundle.main.infoDictionary?["Country"] as? String ?? "")) ?? .ca)
Expand All @@ -159,6 +167,10 @@ class CartManager: ObservableObject {
let mutation = Storefront.buildMutation(inContext: Storefront.InContextDirective(country: countryCode)) { $0
.cartCreate(input: input) { $0
.cart { $0.cartManagerFragment() }
.userErrors {
$0.field()
$0.message()
}
}
}

Expand Down Expand Up @@ -223,6 +235,15 @@ class CartManager: ObservableObject {
))
)
}

private func authenticatedCart(_ customerAccessToken: String, _ items: [GraphQL.ID] = []) -> Storefront.CartInput {
return Storefront.CartInput.create(
lines: Input(orNull: items.map { Storefront.CartLineInput.create(merchandiseId: $0) }),
buyerIdentity: Input(orNull: Storefront.CartBuyerIdentityInput.create(
customerAccessToken: Input(orNull: customerAccessToken)
))
)
}
}

extension Storefront.CartQuery {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,21 @@ import Foundation

extension Storefront.CountryCode {
static func inferRegion() -> Storefront.CountryCode {
if #available(iOS 16, *) {
if let regionCode = Locale.current.region?.identifier {
return Storefront.CountryCode(rawValue: regionCode) ?? .ca
}
} else if #available(iOS 13, *) {
if let regionCode = Locale.current.regionCode {
return Storefront.CountryCode(rawValue: regionCode) ?? .ca
}
}
let regionCode = regionCode()
return Storefront.CountryCode(rawValue: regionCode)!
}
}

return .ca
func regionCode() -> String {
if #available(iOS 16, *) {
if let regionCode = Locale.current.region?.identifier {
return regionCode
}
} else if #available(iOS 13, *) {
if let regionCode = Locale.current.regionCode {
return regionCode
}
}

return "CA"
}
Loading
Loading