title: Authentication with Passport + Facebook + Express + create-react-app + React-Router + proxy
body: <p>TLDR: How do I initiate the Facebook Auth process from a page in my React application?</p>
  
  <p>I've found quite a few posts that touch on most, but not all, of the items I've listed in the title of this post. And I'm pretty close to getting this working, but there must be something I'm missing. </p>
  
  <p>My app consists of a create-react-app front-end using react-router for routing. I have this running on port 51000 in my development environment. In package.json, I'm using "proxy" to redirect other routes to my express server, running on port 51001. This works in general for making requests within my React components that hit the server. For example:</p>
  
  <pre><code>axios.get('/api/some-stuff')
  </code></pre>
  
  <p>When calling within my React component, this will successfully hit my Express server. </p>
  
  <p>I want to ensure that my back-end routes are authenticated, and I've chosen to use Facebook auth via passport-facebook. I've set this up just like the many online tutorials I've seen. For example, I have a route on my express server named '/auth/facebook'. If I go to localhost:51001/auth/facebook, it redirects me to facebook to login, and then redirects me back to some the route I gave it. So that flow works as expected.</p>
  
  <p>The specific problem I'm having is getting React to successfully initiate and complete the api call that triggers auth. I have tried two approach.</p>
  
  <p><strong>Approach 1: Calling the route via ajax</strong></p>
  
  <p>I called the following in my componentDidMount:</p>
  
  <pre><code>axios.get('/auth/facebook')
  </code></pre>
  
  <p>This successfully hits my express server, but then immediate generates the following error in the browser console:</p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load
    <a href="https://www.facebook.com/dialog/oauth?response_type=code&amp;redirect_uri=http%" rel="noreferrer">https://www.facebook.com/dialog/oauth?response_type=code&amp;redirect_uri=http%</a>…2Flocalhost%3A51000%2Fauth%2Ffacebook%2Fcallback&amp;client_id=XYZ. Redirect from
    '<a href="https://www.facebook.com/dialog/oauth?response_type=code&amp;redirect_uri=http%" rel="noreferrer">https://www.facebook.com/dialog/oauth?response_type=code&amp;redirect_uri=http%</a>…2Flocalhost%3A51000%2Fauth%2Ffacebook%2Fcallback&amp;client_id=XYZ’ to
    '<a href="https://www.facebook.com/login.php?skip_api_login=1&amp;api_key=XYZ" rel="noreferrer">https://www.facebook.com/login.php?skip_api_login=1&amp;api_key=XYZ</a>…_&amp;display=page&amp;locale=en_US&amp;logger_id=7f30b5a1-2ad1-dc31-f08b-70d3cfdd55fa'
    has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
    header is present on the requested resource. Origin
    '<a href="http://localhost:51000" rel="noreferrer">http://localhost:51000</a>' is therefore not allowed access.</p>
  </blockquote>
  
  <p>**Approach 2: Going to the route in the browser **</p>
  
  <p>The other thing I've tried, which I've seen in all the tutorials, is to add a link to one of my pages that simply navigates to the authentication route. For example:</p>
  
  <pre><code>&lt;a href="/auth/facebook"&gt;Login with Facebook&lt;/a&gt;
  </code></pre>
  
  <p>However, when I click this, I just end up at <a href="http://localhost:51000/auth/facebook" rel="noreferrer">http://localhost:51000/auth/facebook</a>, or just a blank page in my React app. The proxy I have doesn't kick in, and I don't see any activity on my Express server when I go to this route in the browser, even though it's not a Route I've defined in react-router.</p>
  
  <p>So I'm a bit stuck here. Should clicking that  tag have triggered that route to be fired against my Express server? Is there a way to tell react-router to ignore certain routes so that they're redirected to my express server? This seems to work fine when making AJAX calls, but if I go to localhost:51000/not/a/route, this doesn't cause the request to be redirected to Express. It just loads a react page with no content. </p>
  
  <p>Any help is appreciated. </p>
  
  <p>Thanks,</p>
  
  <p>-Dan</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>So I kind of have this working now, though I'm not at all confident it' the "right" way to do things. </p>
  
  <p>In my react app (which recall is running on port 51000), I have the following link:</p>
  
  <pre><code>&lt;a href="http://localhost:51001/auth/facebook"&gt;Facebook Login&lt;/a&gt;
  </code></pre>
  
  <p>Note that this is pointing directly at my API server, not at a react route. Navigating to this link hits the express server, and immediately redirects to Facebook. Logging in redirects back to my Express facebook callback, which itself redirects to <a href="http://localhost:51000/somePage" rel="noreferrer">http://localhost:51000/somePage</a>, which is now back to my react app. </p>
  
  <p>Ideally that  tag would be going to a /api/auth/facebook, instead of hard-coded to go to a different URL/port. I'll update this issue if/when I figure out how to get react-router to pass that through instead of treating it like a page it needs to load.</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I'm open to someone telling me otherwise, but I think my current approach is actually pretty much fine. I considered how things will behave in production. I will have my react site at www.example.com, with my express server accessible via api.example.com. My link to the Facebook login will be to "api.example.com/auth/facebook". The callback, assuming successful auth, will redirect to "www.example.com/some/route". I think it's reasonable to bounce over to the "api" URL to perform the login. Ultimately there's no point in trying to get react-router to ignore certain routes, since all of my api calls will be to an explicit route.</p>
  
  <p>I realize I'm not providing a complete helpful solution to others. Once I have it all put together, I'll include a link to my repo that contains this login flow, in case anyone else finds it helpful. </p>
  
  <p>** EDIT 3 **</p>
  
  <p>Here are links to my project. It won't be trivial to get the whole project running, but there are only a handful of files associated with the auth flow.</p>
  
  <p>You can view the repo here: <a href="https://github.com/dan-goyette/Portfolio" rel="noreferrer">https://github.com/dan-goyette/Portfolio</a></p>
  
  <p>The Express server router for this is here:</p>
  
  <p><a href="https://github.com/dan-goyette/Portfolio/blob/master/portfolio-server/src/Routing/auth.js" rel="noreferrer">https://github.com/dan-goyette/Portfolio/blob/master/portfolio-server/src/Routing/auth.js</a></p>
  
  <p>In the UI, I'm triggering calls to the express server using this component:</p>
  
  <p><a href="https://github.com/dan-goyette/Portfolio/blob/75ca9326e6227d0601cdfa4c0cece672bd347302/portfolio-ui/src/Components/SocialMenuButton/SocialMenuButton.js" rel="noreferrer">https://github.com/dan-goyette/Portfolio/blob/75ca9326e6227d0601cdfa4c0cece672bd347302/portfolio-ui/src/Components/SocialMenuButton/SocialMenuButton.js</a></p>
  
  <p>You can see it working at <a href="https://www.dan-goyette.com/home" rel="noreferrer">https://www.dan-goyette.com/home</a>, the button at the top right. </p>