Skip to content

feat(auth): auth-ready Promise #264

Closed
@nicolasgarnier

Description

@nicolasgarnier

In some case I'd like to delay some events until I'm sure the auth state is ready and that the redux store contains the auth state of the signed-in user.

Typically I'd like to delay generating the UI until I'm sure that the auth state is ready which happens after the auth().onAuthStateChanged listener has fired once. There are multiple reasons why:

On the client I'd want to avoid a potential "refresh" of the UI where we'd display the UI of a signed-out user first and then redisplay once the redux store has loaded the signed-in users..

But this is really mandatory on the server as I really have to wait for the auth state to be ready because I only get one chance of generating the UI :)

In my app, as a workaround, I have a small tool that does this:

  // Auth state promise resolver.
  let authReadyPromiseResolver;
  const authReadyPromise = new Promise(resolve => {
    authReadyPromiseResolver = resolve
  });

  const unsubscribe = firebaseApp.auth().onAuthStateChanged(() => {
    authReadyPromiseResolver();
    unsubscribe();
  });

Then I can wait for the authReadyPromise promise to resolve and I know the firebase auth state si now ready. e.g. on a server I'd do:

// ...

authReadyPromise.then(() => {  // HERE I wait for the auth state to be ready.
      // Render the App.
      const body = ReactDOMServer.renderToString(
        React.createElement(app.App, {store: store, history: history})
      );
      // Get the redux store state. 
      const initialState = store.getState();
      // Serve the app generated template
      res.send(template({body, initialState}));
);

But really this is a workaround and I'm lucky that this works because my onAuthStateChanged event listener could very well be triggered before the one you use internally inside of react-redux-firebase.

So Feature request is:

It would be great to have a Promise I could use that would be built-in react-redux-firebase that would resolve when the auth state is ready and all the Redux store state has been updated. For instance, I'm thinking of:

const reactReduxFirebaseMiddleware = reactReduxFirebase(firebaseApp, {enableRedirectHandling: false});

const authIsReadypromise = reactReduxFirebaseMiddleware.authIsReady; // THIS IS THE BUILT-IN PROMISE

const store = createStore(
    combineReducers({
      ...reducers,
      router: routerReducer,
      firebaseState: firebaseStateReducer
    }),
    initialState,
    compose(
      applyMiddleware(thunk.withExtraArgument(getFirebase)),
      applyMiddleware(historyMiddleware),
      reactReduxFirebaseMiddleware
    )
  );

authIsReadypromise.then(() => {  // HERE I wait for the auth state to be ready.
      // Render the App.
      const body = ReactDOMServer.renderToString(
        React.createElement(app.App, {store: store, history: history})
      );

      // ...

Not sure if it should be on reactReduxFirebaseMiddleware or if there is somewhere more appropriate.

If there is another built in way to easily know when the auth state is ready please lmk, I'm kinda new-ish to redux so I may be missing some stuff :)

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions