Skip to content

Commit 5f4c0f2

Browse files
authored
fix: general auth improvements (#561)
1 parent 71dee2a commit 5f4c0f2

34 files changed

+618
-346
lines changed

Examples/Examples.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,13 @@
723723
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
724724
PRODUCT_BUNDLE_IDENTIFIER = "com.supabase.swift-examples";
725725
PRODUCT_NAME = "$(TARGET_NAME)";
726+
REGISTER_APP_GROUPS = NO;
727+
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
728+
SUPPORTS_MACCATALYST = NO;
729+
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
730+
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
726731
SWIFT_EMIT_LOC_STRINGS = YES;
732+
TARGETED_DEVICE_FAMILY = 1;
727733
};
728734
name = Debug;
729735
};
@@ -743,7 +749,13 @@
743749
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
744750
PRODUCT_BUNDLE_IDENTIFIER = "com.supabase.swift-examples";
745751
PRODUCT_NAME = "$(TARGET_NAME)";
752+
REGISTER_APP_GROUPS = NO;
753+
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
754+
SUPPORTS_MACCATALYST = NO;
755+
SUPPORTS_MAC_DESIGNED_FOR_IPHONE_IPAD = NO;
756+
SUPPORTS_XR_DESIGNED_FOR_IPHONE_IPAD = NO;
746757
SWIFT_EMIT_LOC_STRINGS = YES;
758+
TARGETED_DEVICE_FAMILY = 1;
747759
};
748760
name = Release;
749761
};

Examples/Examples/Auth/AuthView.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ struct AuthView: View {
1414
case signInWithPhone
1515
case signInWithApple
1616
case signInWithOAuth
17-
case signInWithOAuthUsingUIKit
17+
#if canImport(UIKit)
18+
case signInWithOAuthUsingUIKit
19+
#endif
1820
case googleSignInSDKFlow
1921
case signInAnonymously
2022

@@ -25,7 +27,9 @@ struct AuthView: View {
2527
case .signInWithPhone: "Sign in with Phone"
2628
case .signInWithApple: "Sign in with Apple"
2729
case .signInWithOAuth: "Sign in with OAuth flow"
28-
case .signInWithOAuthUsingUIKit: "Sign in with OAuth flow (UIKit)"
30+
#if canImport(UIKit)
31+
case .signInWithOAuthUsingUIKit: "Sign in with OAuth flow (UIKit)"
32+
#endif
2933
case .googleSignInSDKFlow: "Google Sign in (GIDSignIn SDK Flow)"
3034
case .signInAnonymously: "Sign in Anonymously"
3135
}
@@ -43,7 +47,9 @@ struct AuthView: View {
4347
options
4448
.navigationTitle(options.title)
4549
}
50+
#if !os(macOS)
4651
.navigationBarTitleDisplayMode(.inline)
52+
#endif
4753
}
4854
}
4955
}
@@ -56,8 +62,10 @@ extension AuthView.Option: View {
5662
case .signInWithPhone: SignInWithPhone()
5763
case .signInWithApple: SignInWithApple()
5864
case .signInWithOAuth: SignInWithOAuth()
59-
case .signInWithOAuthUsingUIKit: UIViewControllerWrapper(SignInWithOAuthViewController())
60-
.edgesIgnoringSafeArea(.all)
65+
#if canImport(UIKit)
66+
case .signInWithOAuthUsingUIKit: UIViewControllerWrapper(SignInWithOAuthViewController())
67+
.edgesIgnoringSafeArea(.all)
68+
#endif
6169
case .googleSignInSDKFlow: GoogleSignInSDKFlow()
6270
case .signInAnonymously: SignInAnonymously()
6371
}

Examples/Examples/Auth/AuthWithEmailAndPassword.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ struct AuthWithEmailAndPassword: View {
2929
Form {
3030
Section {
3131
TextField("Email", text: $email)
32-
.keyboardType(.emailAddress)
3332
.textContentType(.emailAddress)
3433
.autocorrectionDisabled()
34+
#if !os(macOS)
35+
.keyboardType(.emailAddress)
3536
.textInputAutocapitalization(.never)
37+
#endif
3638

3739
SecureField("Password", text: $password)
3840
.textContentType(.password)
3941
.autocorrectionDisabled()
42+
#if !os(macOS)
4043
.textInputAutocapitalization(.never)
44+
#endif
4145
}
4246

4347
Section {

Examples/Examples/Auth/AuthWithMagicLink.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ struct AuthWithMagicLink: View {
1515
Form {
1616
Section {
1717
TextField("Email", text: $email)
18-
.keyboardType(.emailAddress)
1918
.textContentType(.emailAddress)
2019
.autocorrectionDisabled()
20+
#if !os(macOS)
21+
.keyboardType(.emailAddress)
2122
.textInputAutocapitalization(.never)
23+
#endif
2224
}
2325

2426
Section {

Examples/Examples/Auth/GoogleSignInSDKFlow.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ struct GoogleSignInSDKFlow: View {
1919
func handleSignIn() {
2020
Task {
2121
do {
22-
let result = try await GIDSignIn.sharedInstance.signIn(withPresenting: rootViewController)
22+
let result = try await GIDSignIn.sharedInstance.signIn(withPresenting: root)
2323

2424
guard let idToken = result.user.idToken?.tokenString else {
2525
debug("No 'idToken' returned by GIDSignIn call.")
@@ -38,9 +38,15 @@ struct GoogleSignInSDKFlow: View {
3838
}
3939
}
4040

41-
var rootViewController: UIViewController {
42-
UIApplication.shared.firstKeyWindow?.rootViewController ?? UIViewController()
43-
}
41+
#if canImport(UIKit)
42+
var root: UIViewController {
43+
UIApplication.shared.firstKeyWindow?.rootViewController ?? UIViewController()
44+
}
45+
#else
46+
var root: NSWindow {
47+
NSApplication.shared.keyWindow ?? NSWindow()
48+
}
49+
#endif
4450
}
4551

4652
#Preview {

Examples/Examples/Auth/SignInWithOAuth.swift

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -45,78 +45,81 @@ struct SignInWithOAuth: View {
4545
}
4646
}
4747

48-
final class SignInWithOAuthViewController: UIViewController, UIPickerViewDataSource,
49-
UIPickerViewDelegate
50-
{
51-
let providers = Provider.allCases
52-
var provider = Provider.allCases[0]
48+
#if canImport(UIKit)
49+
final class SignInWithOAuthViewController: UIViewController, UIPickerViewDataSource,
50+
UIPickerViewDelegate
51+
{
52+
let providers = Provider.allCases
53+
var provider = Provider.allCases[0]
54+
55+
let providerPicker = UIPickerView()
56+
let signInButton = UIButton(type: .system)
57+
58+
override func viewDidLoad() {
59+
super.viewDidLoad()
60+
setupViews()
61+
}
5362

54-
let providerPicker = UIPickerView()
55-
let signInButton = UIButton(type: .system)
63+
func setupViews() {
64+
view.backgroundColor = .white
65+
66+
providerPicker.dataSource = self
67+
providerPicker.delegate = self
68+
view.addSubview(providerPicker)
69+
providerPicker.translatesAutoresizingMaskIntoConstraints = false
70+
NSLayoutConstraint.activate([
71+
providerPicker.centerXAnchor.constraint(equalTo: view.centerXAnchor),
72+
providerPicker.centerYAnchor.constraint(equalTo: view.centerYAnchor),
73+
providerPicker.widthAnchor.constraint(equalToConstant: 200),
74+
providerPicker.heightAnchor.constraint(equalToConstant: 100),
75+
])
76+
77+
signInButton.setTitle("Start Sign-in Flow", for: .normal)
78+
signInButton.addTarget(self, action: #selector(signInButtonTapped), for: .touchUpInside)
79+
view.addSubview(signInButton)
80+
signInButton.translatesAutoresizingMaskIntoConstraints = false
81+
NSLayoutConstraint.activate([
82+
signInButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
83+
signInButton.topAnchor.constraint(equalTo: providerPicker.bottomAnchor, constant: 20),
84+
])
85+
}
5686

57-
override func viewDidLoad() {
58-
super.viewDidLoad()
59-
setupViews()
60-
}
87+
@objc func signInButtonTapped() {
88+
Task {
89+
do {
90+
try await supabase.auth.signInWithOAuth(
91+
provider: provider,
92+
redirectTo: Constants.redirectToURL
93+
)
94+
} catch {
95+
debug("Failed to sign-in with OAuth flow: \(error)")
96+
}
97+
}
98+
}
6199

62-
func setupViews() {
63-
view.backgroundColor = .white
64-
65-
providerPicker.dataSource = self
66-
providerPicker.delegate = self
67-
view.addSubview(providerPicker)
68-
providerPicker.translatesAutoresizingMaskIntoConstraints = false
69-
NSLayoutConstraint.activate([
70-
providerPicker.centerXAnchor.constraint(equalTo: view.centerXAnchor),
71-
providerPicker.centerYAnchor.constraint(equalTo: view.centerYAnchor),
72-
providerPicker.widthAnchor.constraint(equalToConstant: 200),
73-
providerPicker.heightAnchor.constraint(equalToConstant: 100),
74-
])
75-
76-
signInButton.setTitle("Start Sign-in Flow", for: .normal)
77-
signInButton.addTarget(self, action: #selector(signInButtonTapped), for: .touchUpInside)
78-
view.addSubview(signInButton)
79-
signInButton.translatesAutoresizingMaskIntoConstraints = false
80-
NSLayoutConstraint.activate([
81-
signInButton.centerXAnchor.constraint(equalTo: view.centerXAnchor),
82-
signInButton.topAnchor.constraint(equalTo: providerPicker.bottomAnchor, constant: 20),
83-
])
84-
}
100+
func numberOfComponents(in _: UIPickerView) -> Int {
101+
1
102+
}
85103

86-
@objc func signInButtonTapped() {
87-
Task {
88-
do {
89-
try await supabase.auth.signInWithOAuth(
90-
provider: provider,
91-
redirectTo: Constants.redirectToURL
92-
)
93-
} catch {
94-
debug("Failed to sign-in with OAuth flow: \(error)")
95-
}
104+
func pickerView(_: UIPickerView, numberOfRowsInComponent _: Int) -> Int {
105+
providers.count
96106
}
97-
}
98107

99-
func numberOfComponents(in _: UIPickerView) -> Int {
100-
1
101-
}
108+
func pickerView(_: UIPickerView, titleForRow row: Int, forComponent _: Int) -> String? {
109+
"\(providers[row])"
110+
}
102111

103-
func pickerView(_: UIPickerView, numberOfRowsInComponent _: Int) -> Int {
104-
providers.count
112+
func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent _: Int) {
113+
provider = providers[row]
114+
}
105115
}
106116

107-
func pickerView(_: UIPickerView, titleForRow row: Int, forComponent _: Int) -> String? {
108-
"\(providers[row])"
117+
#Preview("UIKit") {
118+
SignInWithOAuthViewController()
109119
}
110120

111-
func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent _: Int) {
112-
provider = providers[row]
113-
}
114-
}
121+
#endif
115122

116123
#Preview("SwiftUI") {
117124
SignInWithOAuth()
118125
}
119-
120-
#Preview("UIKit") {
121-
SignInWithOAuthViewController()
122-
}

Examples/Examples/Auth/SignInWithPhone.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ struct SignInWithPhone: View {
3333
Form {
3434
Section {
3535
TextField("Phone", text: $phone)
36-
.keyboardType(.phonePad)
3736
.textContentType(.telephoneNumber)
3837
.autocorrectionDisabled()
38+
#if !os(macOS)
39+
.keyboardType(.phonePad)
3940
.textInputAutocapitalization(.never)
41+
#endif
4042
}
4143

4244
Section {
@@ -62,10 +64,12 @@ struct SignInWithPhone: View {
6264
Form {
6365
Section {
6466
TextField("Code", text: $code)
65-
.keyboardType(.numberPad)
6667
.textContentType(.oneTimeCode)
6768
.autocorrectionDisabled()
69+
#if !os(macOS)
70+
.keyboardType(.numberPad)
6871
.textInputAutocapitalization(.never)
72+
#endif
6973
}
7074

7175
Section {

Examples/Examples/Examples.entitlements

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>com.apple.developer.applesignin</key>
6-
<array>
7-
<string>Default</string>
8-
</array>
95
<key>com.apple.security.app-sandbox</key>
106
<true/>
117
<key>com.apple.security.files.user-selected.read-only</key>
128
<true/>
9+
<key>com.apple.security.network.client</key>
10+
<true/>
11+
<key>keychain-access-groups</key>
12+
<array/>
1313
</dict>
1414
</plist>

Examples/Examples/ExamplesApp.swift

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,15 @@ import GoogleSignIn
99
import Supabase
1010
import SwiftUI
1111

12-
class AppDelegate: UIResponder, UIApplicationDelegate {
13-
func application(
14-
_: UIApplication,
15-
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
16-
) -> Bool {
17-
if let url = launchOptions?[.url] as? URL {
18-
supabase.handle(url)
19-
}
20-
return true
21-
}
22-
23-
func application(_: UIApplication, open url: URL, options _: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
24-
supabase.handle(url)
25-
return true
26-
}
27-
28-
func application(_: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options _: UIScene.ConnectionOptions) -> UISceneConfiguration {
29-
let configuration = UISceneConfiguration(name: nil, sessionRole: connectingSceneSession.role)
30-
configuration.delegateClass = SceneDelegate.self
31-
return configuration
32-
}
33-
}
34-
35-
class SceneDelegate: UIResponder, UISceneDelegate {
36-
func scene(_: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
37-
guard let url = URLContexts.first?.url else { return }
38-
39-
supabase.handle(url)
40-
}
41-
}
42-
4312
@main
4413
struct ExamplesApp: App {
45-
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
46-
4714
var body: some Scene {
4815
WindowGroup {
4916
RootView()
5017
.environment(AuthController())
18+
.onOpenURL {
19+
supabase.handle($0)
20+
}
5121
}
5222
}
5323
}

Examples/Examples/Profile/ResetPasswordView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ struct ResetPasswordView: View {
2121

2222
TextField("Enter your email", text: $email)
2323
.textFieldStyle(RoundedBorderTextFieldStyle())
24+
#if !os(macOS)
2425
.autocapitalization(.none)
2526
.keyboardType(.emailAddress)
27+
#endif
2628

2729
Button(action: resetPassword) {
2830
Text("Send Reset Link")

0 commit comments

Comments
 (0)