- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add
signInWithOAuth
(#299)
- v2.25.0
- v2.24.7
- v2.24.6
- v2.24.5
- v2.24.4
- v2.24.3
- v2.24.2
- v2.24.1
- v2.24.0
- v2.23.0
- v2.22.1
- v2.22.0
- v2.21.0
- v2.20.5
- v2.20.4
- v2.20.3
- v2.20.2
- v2.20.1
- v2.20.0
- v2.19.0
- v2.18.0
- v2.17.1
- v2.17.0
- v2.16.1
- v2.16.0
- v2.15.3
- v2.15.2
- v2.15.1
- v2.15.0
- v2.14.3
- v2.14.2
- v2.14.1
- v2.14.0
- v2.13.9
- v2.13.8
- v2.13.7
- v2.13.6
- v2.13.5
- v2.13.4
- v2.13.3
- v2.13.2
- v2.13.1
- v2.13.0
- v2.12.0
- v2.11.0
- v2.10.1
- v2.10.0
- v2.9.0
- v2.8.5
- v2.8.4
- v2.8.3
- v2.8.2
- v2.8.1
- v2.8.0
- v2.7.0
Showing
14 changed files
with
468 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// | ||
// SignInWithOAuth.swift | ||
// Examples | ||
// | ||
// Created by Guilherme Souza on 10/04/24. | ||
// | ||
|
||
import AuthenticationServices | ||
import Supabase | ||
import SwiftUI | ||
|
||
struct SignInWithOAuth: View { | ||
let providers = Provider.allCases | ||
|
||
@State var provider = Provider.allCases[0] | ||
@Environment(\.webAuthenticationSession) var webAuthenticationSession | ||
|
||
var body: some View { | ||
VStack { | ||
Picker("Provider", selection: $provider) { | ||
ForEach(providers) { provider in | ||
Text("\(provider)").tag(provider) | ||
} | ||
} | ||
|
||
Button("Start Sign-in Flow") { | ||
Task { | ||
do { | ||
try await supabase.auth.signInWithOAuth( | ||
provider: provider, | ||
redirectTo: Constants.redirectToURL, | ||
launchFlow: { @MainActor url in | ||
try await webAuthenticationSession.authenticate( | ||
using: url, | ||
callbackURLScheme: Constants.redirectToURL.scheme! | ||
) | ||
} | ||
) | ||
} catch { | ||
debug("Failed to sign-in with OAuth flow: \(error)") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
final class SignInWithOAuthViewController: UIViewController, UIPickerViewDataSource, | ||
UIPickerViewDelegate | ||
{ | ||
let providers = Provider.allCases | ||
var provider = Provider.allCases[0] | ||
|
||
let providerPicker = UIPickerView() | ||
let signInButton = UIButton(type: .system) | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
setupViews() | ||
} | ||
|
||
func setupViews() { | ||
view.backgroundColor = .white | ||
|
||
providerPicker.dataSource = self | ||
providerPicker.delegate = self | ||
view.addSubview(providerPicker) | ||
providerPicker.translatesAutoresizingMaskIntoConstraints = false | ||
NSLayoutConstraint.activate([ | ||
providerPicker.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
providerPicker.centerYAnchor.constraint(equalTo: view.centerYAnchor), | ||
providerPicker.widthAnchor.constraint(equalToConstant: 200), | ||
providerPicker.heightAnchor.constraint(equalToConstant: 100), | ||
]) | ||
|
||
signInButton.setTitle("Start Sign-in Flow", for: .normal) | ||
signInButton.addTarget(self, action: #selector(signInButtonTapped), for: .touchUpInside) | ||
view.addSubview(signInButton) | ||
signInButton.translatesAutoresizingMaskIntoConstraints = false | ||
NSLayoutConstraint.activate([ | ||
signInButton.centerXAnchor.constraint(equalTo: view.centerXAnchor), | ||
signInButton.topAnchor.constraint(equalTo: providerPicker.bottomAnchor, constant: 20), | ||
]) | ||
} | ||
|
||
@objc func signInButtonTapped() { | ||
Task { | ||
do { | ||
try await supabase.auth.signInWithOAuth( | ||
provider: provider, | ||
redirectTo: Constants.redirectToURL | ||
) | ||
} catch { | ||
debug("Failed to sign-in with OAuth flow: \(error)") | ||
} | ||
} | ||
} | ||
|
||
func numberOfComponents(in _: UIPickerView) -> Int { | ||
1 | ||
} | ||
|
||
func pickerView(_: UIPickerView, numberOfRowsInComponent _: Int) -> Int { | ||
providers.count | ||
} | ||
|
||
func pickerView(_: UIPickerView, titleForRow row: Int, forComponent _: Int) -> String? { | ||
"\(providers[row])" | ||
} | ||
|
||
func pickerView(_: UIPickerView, didSelectRow row: Int, inComponent _: Int) { | ||
provider = providers[row] | ||
} | ||
} | ||
|
||
#Preview("SwiftUI") { | ||
SignInWithOAuth() | ||
} | ||
|
||
#Preview("UIKit") { | ||
SignInWithOAuthViewController() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// | ||
// UIViewControllerWrapper.swift | ||
// Examples | ||
// | ||
// Created by Guilherme Souza on 10/04/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct UIViewControllerWrapper<T: UIViewController>: UIViewControllerRepresentable { | ||
typealias UIViewControllerType = T | ||
|
||
let viewController: T | ||
|
||
init(_ viewController: T) { | ||
self.viewController = viewController | ||
} | ||
|
||
func makeUIViewController(context _: Context) -> T { | ||
viewController | ||
} | ||
|
||
func updateUIViewController(_: T, context _: Context) { | ||
// Update the view controller if needed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters