Firebaseui-web: signInSuccess redirect after checking database

Created on 14 May 2018  路  4Comments  路  Source: firebase/firebaseui-web

I'm trying to make a query to my firestore database upon signInSuccess to check if my user has created a 'group' and redirect based on the result. If the user has made a group, redirect to '/home', if not, redirect to '/create-group'.

I think I may be using the callback incorrectly. I tried this manual redirect in my FirebaseUI config but can't get it to work.. Any advice on how to achieve this outcome (redirect based on database query result)? Thank you

```
const uiConfig = {
signInFlow: 'popup',
signInOptions: [
firebase.auth.GoogleAuthProvider.PROVIDER_ID,
firebase.auth.EmailAuthProvider.PROVIDER_ID
],
'callbacks': {
'signInSuccessWithAuthResult': function(authResult) {

        // query database to check if user has made a group
        firebase.firestore().collection('users')
        .doc(authResult.user.uid).collection('groups')
        .get().then(function(groups) {

            // redirect based on result
            if (groups.docs.length === 0) {
                window.location.pathname = '/create-group'
            } else {
                window.location.pathname = '/home'
            }
        })
    }
}

}

Most helpful comment

You can, your snippet looks correct to me, just return false at the end:

        'signInSuccessWithAuthResult': function(authResult) {

            // query database to check if user has made a group
            firebase.firestore().collection('users')
            .doc(authResult.user.uid).collection('groups')
            .get().then(function(groups) {

                // redirect based on result
                if (groups.docs.length === 0) {
                    window.location.pathname = '/create-group'
                } else {
                    window.location.pathname = '/home'
                }
            })
            // Prevent automatic redirect to signInSuccessUrl since a conditional redirect is handled above.
           return false;
        }

All 4 comments

This question is better suited for stackoverflow (tag your questions with firebase-authentication and google-cloud-firestore). This forum is not for code discussions.
Other than returning false to avoid to an automatic redirect to signInSuccessUrl, you can do whatever you want here. Also we recommend using custom claims for access control based on user group.

@bojeil-google Ok, sorry for the inappropriate question.. so just to confirm, there is no way to do a conditional redirect using the signInSuccessWithAuthResult callback? Maybe I'm misunderstanding the docs but I thought this kind of functionality is exactly what the callback is for.

You can, your snippet looks correct to me, just return false at the end:

        'signInSuccessWithAuthResult': function(authResult) {

            // query database to check if user has made a group
            firebase.firestore().collection('users')
            .doc(authResult.user.uid).collection('groups')
            .get().then(function(groups) {

                // redirect based on result
                if (groups.docs.length === 0) {
                    window.location.pathname = '/create-group'
                } else {
                    window.location.pathname = '/home'
                }
            })
            // Prevent automatic redirect to signInSuccessUrl since a conditional redirect is handled above.
           return false;
        }

The search terms that landed me here were

custom signInSuccessUrl

Hopefully that gets people to the right place sooner than it took me.

Was this page helpful?
0 / 5 - 0 ratings