Search code examples
androidfirebasefirebase-authenticationchatchatsdk

How to authenticate a Firebase user with ChatSDK?


I've integrated Firebase into my app. I can authenticate with Firebase via email/password. Then I initialize the ChatSDK and call InterfaceManager.shared().a.startLoginActivity(this,true); From there, the app is "taken over" by the default chat user interface and the functionality works great and ChatSDK.currentUser() returns the expected User object.

I would like to do the same thing with my own UI. To authenticate a user after ChatSDK initialization, I've tried: ChatSDK.auth().authenticateWithCachedToken(); ChatSDK.auth().authenticate(AccountDetails.signUp(email,pwd)); ChatSDK.auth().authenticate(AccountDetails.username(email,pwd));

It is my understanding that I wouldn't be able to do ChatSDK.thread().createThread(...) until I have a valid User. However, after each authentication attempt, ChatSDK.currentUser() is null.

Looking at the ChatSDK source code and documentation, it appears this is the mechanism for authentication. Is there something I'm missing?


Solution

  • enter image description here Subscribe is necessary, even if you aren't using it.

            ChatSDK.auth()
                .authenticateWithCachedToken()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action() {
                    @Override
                    public void run() throws Exception {
                        Log.d("Success","We're in!");
                    }
                    }, new Consumer<Throwable>() {
                        @Override
                        public void accept(Throwable throwable) throws Exception {
                            Log.d("Err",throwable.toString());
                        }
                    });
    

    Also, here's some code to start a new chat thread with a known user id.

        UserWrapper userWrapper = UserWrapper.initWithEntityId(firebaseUser.uid);
        userWrapper.metaOn();
        userWrapper.onlineOn();
        User otherUser = userWrapper.getModel();
    
        ProgressDialog pd = new ProgressDialog(MainActivity.this);
        pd.show();
    
        ChatSDK.thread().createThread("", otherUser, ChatSDK.currentUser())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    pd.dismiss();
                })
                .subscribe(thread -> {
                    ChatSDK.ui().startChatActivityForID(getApplicationContext(), thread.getEntityID());
                }, throwable -> {
                    ToastHelper.show(getApplicationContext(), throwable.getLocalizedMessage());
                });