Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Commit ab6d1a0

Browse files
committed
Implement auth flow screen presentation and email login in demo app
1 parent ab628c0 commit ab6d1a0

File tree

4 files changed

+182
-1
lines changed

4 files changed

+182
-1
lines changed

Demo/AuthenticatorDemo.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
3F55C3AD292BAB23002923B1 /* UIViewController+Presentation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F55C3AB292BAB23002923B1 /* UIViewController+Presentation.swift */; };
2828
3F55C3AE292BAB23002923B1 /* UIAlertController+Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F55C3AC292BAB23002923B1 /* UIAlertController+Utils.swift */; };
2929
3F55C3B0292BAB54002923B1 /* WKWebView+UserAgent.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F55C3AF292BAB54002923B1 /* WKWebView+UserAgent.swift */; };
30+
3F55C3B9292BAD5E002923B1 /* ViewController+WordPressAuthenticator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F27A55F291E421C0000996B /* ViewController+WordPressAuthenticator.swift */; };
31+
3F55C3BA292BAD61002923B1 /* ViewController+WordPressAuthenticationDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3F27A55D291E41CC0000996B /* ViewController+WordPressAuthenticationDelegate.swift */; };
3032
3FB5B6EF2912420E00F40408 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FB5B6EE2912420E00F40408 /* AppDelegate.swift */; };
3133
3FB5B6F12912420E00F40408 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FB5B6F02912420E00F40408 /* SceneDelegate.swift */; };
3234
3FB5B6F32912420E00F40408 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3FB5B6F22912420E00F40408 /* ViewController.swift */; };
@@ -46,6 +48,8 @@
4648

4749
/* Begin PBXFileReference section */
4850
3F27A555291AE5F00000996B /* Secrets.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Secrets.swift; path = ../Secrets.swift; sourceTree = BUILT_PRODUCTS_DIR; };
51+
3F27A55D291E41CC0000996B /* ViewController+WordPressAuthenticationDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ViewController+WordPressAuthenticationDelegate.swift"; sourceTree = "<group>"; };
52+
3F27A55F291E421C0000996B /* ViewController+WordPressAuthenticator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ViewController+WordPressAuthenticator.swift"; sourceTree = "<group>"; };
4953
3F55C3A7292BAAE0002923B1 /* CellConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellConfiguration.swift; sourceTree = "<group>"; };
5054
3F55C3AB292BAB23002923B1 /* UIViewController+Presentation.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIViewController+Presentation.swift"; sourceTree = "<group>"; };
5155
3F55C3AC292BAB23002923B1 /* UIAlertController+Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIAlertController+Utils.swift"; sourceTree = "<group>"; };
@@ -105,6 +109,8 @@
105109
3F55C3AC292BAB23002923B1 /* UIAlertController+Utils.swift */,
106110
3F55C3AB292BAB23002923B1 /* UIViewController+Presentation.swift */,
107111
3FB5B6F22912420E00F40408 /* ViewController.swift */,
112+
3F27A55D291E41CC0000996B /* ViewController+WordPressAuthenticationDelegate.swift */,
113+
3F27A55F291E421C0000996B /* ViewController+WordPressAuthenticator.swift */,
108114
3F55C3AF292BAB54002923B1 /* WKWebView+UserAgent.swift */,
109115
);
110116
path = AuthenticatorDemo;
@@ -268,6 +274,8 @@
268274
buildActionMask = 2147483647;
269275
files = (
270276
3F55C3AD292BAB23002923B1 /* UIViewController+Presentation.swift in Sources */,
277+
3F55C3BA292BAD61002923B1 /* ViewController+WordPressAuthenticationDelegate.swift in Sources */,
278+
3F55C3B9292BAD5E002923B1 /* ViewController+WordPressAuthenticator.swift in Sources */,
271279
3F27A556291AE5F00000996B /* Secrets.swift in Sources */,
272280
3FB5B6F32912420E00F40408 /* ViewController.swift in Sources */,
273281
3FB5B6EF2912420E00F40408 /* AppDelegate.swift in Sources */,
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import WordPressAuthenticator
2+
3+
extension ViewController: WordPressAuthenticatorDelegate {
4+
5+
var dismissActionEnabled: Bool { true }
6+
7+
var supportActionEnabled: Bool { true }
8+
9+
var wpcomTermsOfServiceEnabled: Bool { true }
10+
11+
var showSupportNotificationIndicator: Bool { true }
12+
13+
var supportEnabled: Bool { true }
14+
15+
var allowWPComLogin: Bool { true }
16+
17+
func createdWordPressComAccount(username: String, authToken: String) {
18+
print(username)
19+
print(authToken)
20+
}
21+
22+
func userAuthenticatedWithAppleUserID(_ appleUserID: String) {
23+
print(appleUserID)
24+
}
25+
26+
func presentSupportRequest(from sourceViewController: UIViewController, sourceTag: WordPressSupportSourceTag) {
27+
fatalError("TODO")
28+
}
29+
30+
func shouldPresentUsernamePasswordController(for siteInfo: WordPressComSiteInfo?, onCompletion: @escaping (WordPressAuthenticatorResult) -> Void) {
31+
fatalError("TODO")
32+
}
33+
34+
func presentLoginEpilogue(in navigationController: UINavigationController, for credentials: AuthenticatorCredentials, source: SignInSource?, onDismiss: @escaping () -> Void) {
35+
fatalError("TODO")
36+
}
37+
38+
func presentSignupEpilogue(in navigationController: UINavigationController, for credentials: AuthenticatorCredentials, service: SocialService?) {
39+
fatalError("TODO")
40+
}
41+
42+
func presentSupport(from sourceViewController: UIViewController, sourceTag: WordPressSupportSourceTag, lastStep: AuthenticatorAnalyticsTracker.Step, lastFlow: AuthenticatorAnalyticsTracker.Flow) {
43+
fatalError("TODO")
44+
}
45+
46+
func shouldPresentLoginEpilogue(isJetpackLogin: Bool) -> Bool {
47+
true
48+
}
49+
50+
func shouldHandleError(_ error: Error) -> Bool {
51+
print(error)
52+
return true
53+
}
54+
55+
func handleError(_ error: Error, onCompletion: @escaping (UIViewController) -> Void) {
56+
dismiss(animated: true) { [weak self] in
57+
self?.presentAlert(
58+
title: "Authentication Error",
59+
message: "\(error.localizedDescription)",
60+
onDismiss: {}
61+
)
62+
}
63+
}
64+
65+
func shouldPresentSignupEpilogue() -> Bool {
66+
true
67+
}
68+
69+
func sync(credentials: AuthenticatorCredentials, onCompletion: @escaping () -> Void) {
70+
dismiss(animated: true) { [weak self] in
71+
self?.presentAlert(
72+
title: "Authentication Successful",
73+
message: "Next step will be syncing credentials",
74+
onDismiss: {}
75+
)
76+
}
77+
}
78+
79+
func track(event: WPAnalyticsStat) {
80+
print(event)
81+
}
82+
83+
func track(event: WPAnalyticsStat, properties: [AnyHashable: Any]) {
84+
print(event)
85+
print(properties)
86+
}
87+
88+
func track(event: WPAnalyticsStat, error: Error) {
89+
print(event)
90+
print(error)
91+
}
92+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import WebKit
2+
import WordPressAuthenticator
3+
4+
extension ViewController {
5+
6+
func initializeWordPressAuthenticator() {
7+
// In a proper app, we'd want to split this call to keep the code readable. Here, it's
8+
// useful to keep it all in one block to show how insanely long it is.
9+
WordPressAuthenticator.initialize(
10+
configuration: WordPressAuthenticatorConfiguration(
11+
wpcomClientId: ApiCredentials.client,
12+
wpcomSecret: ApiCredentials.secret,
13+
wpcomScheme: "wordpress-authenticator-ios-demo",
14+
wpcomTermsOfServiceURL: "https://wordpress.com/tos/",
15+
wpcomBaseURL: "https://wordpress.com",
16+
wpcomAPIBaseURL: "https://public-api.wordpress.com/",
17+
googleLoginClientId: ApiCredentials.googleLoginClientId,
18+
googleLoginServerClientId: ApiCredentials.googleLoginServerClientId,
19+
googleLoginScheme: ApiCredentials.googleLoginSchemeId,
20+
userAgent: "\(WKWebView.userAgent)-wordpress-authenticator-demo-app",
21+
showLoginOptions: true,
22+
enableSignUp: true,
23+
// SIWA might require additional settings in the Developer Portal... Keeping it off
24+
// for the moment
25+
enableSignInWithApple: false,
26+
enableSignupWithGoogle: true,
27+
enableUnifiedAuth: true,
28+
enableUnifiedCarousel: true
29+
),
30+
style: WordPressAuthenticatorStyle(
31+
// Primary (normal and highlight) is the color of buttons such as "Log in or signup
32+
// with WordPress.com"
33+
primaryNormalBackgroundColor: .orange,
34+
primaryNormalBorderColor: .none,
35+
primaryHighlightBackgroundColor: .brown,
36+
primaryHighlightBorderColor: .none,
37+
// Secondary (normal and highlight) is the color of buttons such as "Enter your
38+
// existing site address" (the one just below "Log in or signup...") or "Continue
39+
// with Google".
40+
secondaryNormalBackgroundColor: .blue,
41+
secondaryNormalBorderColor: .black,
42+
secondaryHighlightBackgroundColor: .purple,
43+
secondaryHighlightBorderColor: .black,
44+
disabledBackgroundColor: .systemGray,
45+
disabledBorderColor: .systemGray,
46+
primaryTitleColor: .white,
47+
secondaryTitleColor: .white,
48+
disabledTitleColor: .white,
49+
disabledButtonActivityIndicatorColor: .label,
50+
textButtonColor: .red,
51+
textButtonHighlightColor: .red,
52+
instructionColor: .label,
53+
subheadlineColor: .secondaryLabel,
54+
placeholderColor: .red,
55+
viewControllerBackgroundColor: .red,
56+
textFieldBackgroundColor: .red,
57+
// The navBar settings here are ignored. Those in
58+
// `WordPressAuthenticatorUnifiedStyle` take precedence.
59+
navBarImage: UIImage(),
60+
navBarBadgeColor: .red,
61+
navBarBackgroundColor: .orange
62+
),
63+
unifiedStyle: WordPressAuthenticatorUnifiedStyle(
64+
borderColor: .separator,
65+
errorColor: .red,
66+
textColor: .label,
67+
textSubtleColor: .blue,
68+
textButtonColor: .purple,
69+
textButtonHighlightColor: .orange,
70+
viewControllerBackgroundColor: .systemBackground,
71+
navBarBackgroundColor: .blue,
72+
navButtonTextColor: .white,
73+
navTitleTextColor: .white
74+
)
75+
)
76+
}
77+
}

Demo/AuthenticatorDemo/ViewController.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ class ViewController: UIViewController {
88
/// Add `CellConfiguration` items to add new actionable rows to the table view in `ViewController`.
99
lazy var configuration: [CellConfiguration] = [
1010
CellConfiguration(text: "Show Login") { [weak self] in
11-
self?.presentAlert(title: "Coming Soon", message: "...", onDismiss: {})
11+
guard let self else { fatalError() }
12+
WordPressAuthenticator.showLoginFromPresenter(self, animated: true)
1213
}
1314
]
1415

@@ -21,6 +22,9 @@ class ViewController: UIViewController {
2122
title = "Authenticator Demo 🔐"
2223

2324
setUpTableView()
25+
26+
initializeWordPressAuthenticator()
27+
WordPressAuthenticator.shared.delegate = self
2428
}
2529

2630
func setUpTableView() {

0 commit comments

Comments
 (0)