title: Passport & JWT & Google/Facebook Strategy - How do I combine JWT and Google/Facebook Strategy?
body: <p>This question is for anyone who is familiar with</p>
  
  <ul>
  <li>Node.js</li>
  <li>Express</li>
  <li>Passport</li>
  <li>JWT Authentication with passport (JSON Web Tokens)</li>
  <li>Facebook OAuth2.0 OR Google OAuth2.0</li>
  </ul>
  
  <p>I have been doing some online courses and understand how to do the two following things:</p>
  
  <ol>
  <li>Authentication using Passport Local Strategy + JWT Tokens</li>
  <li>Authentication using Passport Google/Facebook Strategy + Cookie/sessions.</li>
  </ol>
  
  <p>I am trying to combine the content from these two courses basically. I want to use Google Strategy + JWT Authentication. I want to use JWT instead of cookies because my app is going to be a web/mobile/tablet app, and I need to be accessing the api from different domains.</p>
  
  <p><strong>There are two issues I am having with this:</strong>
  To kick off the Google/facebook OAuth pipelines, you need to call either '/auth/facebook' or '/auth/google'. Both Oauth flows work basically the same so when I say '/auth/google' from now on, I am referring to either. Now the issue I'm having is: On the client, do I call the '/auth/google' route with a href button link or an axios/ajax call? If I use the href or axios/ajax approach I am still getting problems with both solutions.</p>
  
  <p><strong>The href approach problem:</strong>
  When I assign an <code>&lt;a&gt;</code> tag with a href to '/auth/google' the authentication works perfectly fine. The user gets pushed through the Google Auth flow, they log in and the '/auth/google/callback' route gets called. The problem I have now is how do I correctly send the JWT token back to the client from '/auth/google/callback'?</p>
  
  <p>After a lot of googling I have seen that people have simply passed the the JWT back to the client from the oauth callback in the redirect query param. For example: </p>
  
  <pre><code>res.redirect(301, `/dashboard?token=${tokenForUser(req.user)}`);
  </code></pre>
  
  <p>The issue I have with this is that now the the ability to authenticate is saved in my browser history! I could log out (destroying the token saved in localStorage), and then simply look at my browser url history, go back to the url that contains the token in the query param, and I would automatically log in again without having to go through the Google Strategy! This is a huge security flaw and is obviously the incorrect way to approach it.</p>
  
  <p><strong>The axios/ajax approach problem:</strong>
  Now before I explain the problem with this issue, I know for sure that If I get this working, it will solve all issues I was having with the previous href problem. If I manage to call '/google/auth' from an axios.get() call and receive the JWT in the response body, I will not be sending the token as url param, and it will not get saved in the browser history! Perfect right? well there is still some problems with this approach :(</p>
  
  <p>When try to call <code>axios.get('/auth/google')</code> I get the following error:</p>
  
  <p><a href="https://i.stack.imgur.com/DGrOs.png" rel="noreferrer"><img src="https://i.stack.imgur.com/DGrOs.png" alt="enter image description here"></a></p>
  
  <p>How I've tried to solve the problem:</p>
  
  <ul>
  <li>I installed cors to my npm server, and added <code>app.use(cors());</code> to my index.js. </li>
  <li>I took a stab and added "<a href="http://localhost:3000" rel="noreferrer">http://localhost:3000</a>" to the "Authorised JavaScript origins" in Google developer console.</li>
  </ul>
  
  <p>Neither of these solutions solved the issue, so now I really feel stuck. I want to use the axios/ajax approach, but I'm not sure how to get past this cors error.</p>
  
  <p>Sorry for such a long message, but I really felt I had to give you all the information in order for you to properly help me.</p>
  
  <p>Thanks again, looking forward to hear from you!</p>