Back

Login with Facebook (SwiftUI) causing OpenURLAction error

  • 0
  • Accounts
  • Apple
ItsJustLandon
26 Jul, 2023, 01:02

I'm trying to use sign in with Facebook. Not having much luck. I've double-triple checked bundle IDs, app IDs, and redirect urls. I'm not sure what I'm doing wrong? Hopefully someone can provide some insight 🙏

New users are being created in AppWrite, but this error doesn't seem to go away no matter what I've tried. In addition, I think I may be missing the part where I'm supposed to redirect my users back to my app.

Please forgive me as I am new to SwiftUI (not Swift)

TypeScript
//
//  AppwriteViewModel.swift
//  FacebookGame
//
//  Created by Landon Hughes on 7/22/23.
//

import Foundation
import Appwrite
import AppwriteModels

class AppwriteViewModel {
    
    
    let client: Client
    let account: Account
    
    init() {
        client =  Client()
            .setEndpoint("https://cloud.appwrite.io/v1")
            .setProject("64bbfb9db0f6820db5ac")
            .setSelfSigned()
        
        account = Account(client)
    }
    
    
    func signIntoFacebook() {
        do {
            let success = try account.createOAuth2Session(provider: "facebook", success: "https://cloud.appwrite.io", scopes: ["public_profile", "user_friends", "email"])
            print("SUCCESS: \(String(describing: success))")
        } catch {
            print("Sign in error: \(error.localizedDescription)")
        }
        
    }
    
}
TypeScript
//
//  ContentView.swift
//  FacebookGame
//
//  Created by Landon Hughes on 7/22/23.
//

import SwiftUI
import Appwrite


struct ContentView: View {

    let viewModel = AppwriteViewModel()
    
    var body: some View {
        Button("Login") {
            viewModel.signIntoFacebook()
        }
        .registerOAuthHandler()

    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
TL;DR
The issue is with the OpenURLAction error when using Login with Facebook in SwiftUI. The solution is to make sure that the `AppwriteViewModel` class is an `ObservableObject`. Additionally, the redirect URL should be `https://cloud.appwrite.io/auth/oauth2/success?project=64bbfb9db0f6820db5ac&domain=.cloud.appwrite.io&key=a_session_64bbfb9db0f6820db5ac&secret=...`. Another suggestion is to not pass anything for the success parameter in the `account.createOAuth2Session` function.
Drake
26 Jul, 2023, 01:03

Don't pass anything for success

ItsJustLandon
26 Jul, 2023, 01:06

Ok. That fixes the issue of bringing the user back into my app. Thank you!

Next issue would be the OpenActionURL error

Drake
26 Jul, 2023, 01:14

@Jake Any ideas?

0xb4d1dea
27 Jul, 2023, 01:16
TypeScript
// let viewModel = AppwriteViewModel()
@StateObject var viewModel = AppwriteViewModel()

AppwriteViewModel should be an ObservableObject

TypeScript
class AppwriteViewModel: ObservableObject {
...
}
ItsJustLandon
27 Jul, 2023, 01:23

Hmm

0xb4d1dea
27 Jul, 2023, 01:27

I see, the users register correctly, but then they are not redirected to the application?

ItsJustLandon
27 Jul, 2023, 01:27

They are redirected back to the app.

It's just that darn OpenActionURL swiftui "bug"

0xb4d1dea
27 Jul, 2023, 01:28

Let me check the SDK code 🤔

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more