Created
August 1, 2025 13:00
-
-
Save emilwalter/15d45439c6ccdba28d2e23e21ea5c853 to your computer and use it in GitHub Desktop.
Adding social providers login (OAuth) using clerk-ios
This file contains hidden or 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
| import SwiftUI | |
| import Clerk | |
| struct OAuthView: View { | |
| @State private var email = "" | |
| @State private var password = "" | |
| @State private var errorMessage: String? | |
| var body: some View { | |
| VStack(spacing: 24) { | |
| VStack(spacing: 16) { | |
| TextField("Email", text: $email) | |
| .keyboardType(.emailAddress) | |
| .textInputAutocapitalization(.never) | |
| .autocorrectionDisabled(true) | |
| .padding() | |
| .background(Color(.secondarySystemBackground)) | |
| .cornerRadius(8) | |
| SecureField("Password", text: $password) | |
| .padding() | |
| .background(Color(.secondarySystemBackground)) | |
| .cornerRadius(8) | |
| Button { | |
| Task { await signInWithEmail(email: email, password: password) } | |
| } label: { | |
| Text("Continue with Email") | |
| .frame(maxWidth: .infinity) | |
| .padding() | |
| .background(Color.blue) | |
| .foregroundColor(.white) | |
| .cornerRadius(8) | |
| } | |
| } | |
| Divider() | |
| VStack(spacing: 16) { | |
| Button { | |
| Task { await signInWithOAuth(provider: .google) } | |
| } label: { | |
| HStack { | |
| Image(systemName: "globe") | |
| Text("Sign in with Google") | |
| } | |
| .frame(maxWidth: .infinity) | |
| .padding() | |
| .background(Color.gray.opacity(0.2)) | |
| .cornerRadius(8) | |
| } | |
| Button { | |
| Task { await signInWithOAuth(provider: .microsoft) } | |
| } label: { | |
| HStack { | |
| Image(systemName: "person.crop.circle") | |
| Text("Sign in with Microsoft") | |
| } | |
| .frame(maxWidth: .infinity) | |
| .padding() | |
| .background(Color.gray.opacity(0.2)) | |
| .cornerRadius(8) | |
| } | |
| } | |
| if let errorMessage { | |
| Text(errorMessage) | |
| .foregroundColor(.red) | |
| .font(.caption) | |
| .multilineTextAlignment(.center) | |
| .padding(.top, 8) | |
| } | |
| } | |
| .padding() | |
| } | |
| func signInWithOAuth(provider: OAuthProvider) async { | |
| do { | |
| let result = try await SignIn.authenticateWithRedirect(strategy: .oauth(provider: provider)) | |
| switch result { | |
| case .signIn(let signIn): | |
| if signIn.status == .complete { | |
| print("Signed in") | |
| } | |
| case .signUp(let signUp): | |
| if signUp.status == .complete { | |
| print("Signed up") | |
| } | |
| } | |
| } catch { | |
| if let apiError = error as? ClerkAPIError { | |
| errorMessage = apiError.message ?? "Authentication error" | |
| } else { | |
| errorMessage = error.localizedDescription | |
| } | |
| } | |
| } | |
| func signInWithEmail(email: String, password: String) async { | |
| do { | |
| try await SignIn.create(strategy: .identifier(email, password: password)) | |
| print("Signed in with email") | |
| } catch { | |
| if let apiError = error as? ClerkAPIError { | |
| errorMessage = apiError.message ?? "Authentication error" | |
| } else { | |
| errorMessage = error.localizedDescription | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment