Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example using React Router #5

Open
armand1m opened this issue Feb 23, 2019 · 2 comments
Open

Add example using React Router #5

armand1m opened this issue Feb 23, 2019 · 2 comments

Comments

@armand1m
Copy link
Owner

No description provided.

@joseph-allen
Copy link

Hey I happen to have done this, the trick here is basically passing props through React router with the following pattern. Each Route takes a parameter render and you can pass your props here.

         <Switch>
            <Route
              path="/login"
              render={() => (
                <Login
                  signInWithEmailAndPassword={props.signInWithEmailAndPassword}
                  signOut={props.signOut}
                  user={props.user}
                />
              )}
            />
          </Switch>

@fotoflo
Copy link

fotoflo commented Aug 3, 2021

I check if the user exists and if not send them to SignIn, if so, send them to the app... Not sure if it's better or cleaner, but its a full example :-)

import React from 'react';
import firebase from './firebase'
import withFirebaseAuth from 'react-with-firebase-auth'

import SignInPage from './SignInPage'
import AppContainer from './AppContainer'
import {
  Switch,
  Route,
  useHistory
} from "react-router-dom";

const firebaseAppAuth = firebase.auth();

function App({user, signOut, signInWithGoogle, error, loading}){
  
  const history = useHistory();

  if(user){
    history.push("/app")
  }else{
    history.push("/signin")
  }

  return (
    <div className="App">
        <Switch>
          <Route path="/app">
            <AppContainer 
              authUser={user}
              signOut={signOut}
            />
          </Route>
          <Route path="/signin">
            <SignInPage 
              signInWithGoogle={signInWithGoogle}
              error={error}
            />
          </Route>
        </Switch>
    </div>
  )}
  
  const providers = {
    googleProvider: new firebase.auth.GoogleAuthProvider()
  };
  
  export default withFirebaseAuth({
    providers,
    firebaseAppAuth,
  })(App);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants