Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PassportJS + RestAPI +SPA

I am using ExpressJS to build RestAPI, client is SPA and support authenticate by Google/FaceBook/GitHub/... via PassportJS. My question, callback from social login will return to RestAPI or SPA? If system returns to RestAPI, how can to redirect to home page on SPA. Another case, if system callback SPA, how can RestAPI receive and validate token from client. Please let me know common approachs.

Thanks,

like image 879
Jacky Phuong Avatar asked Jun 01 '16 02:06

Jacky Phuong


People also ask

What is the use of Passportjs?

Passport is authentication middleware for Node. js. Extremely flexible and modular, Passport can be unobtrusively dropped in to any Express-based web application. A comprehensive set of strategies support authentication using a username and password, Facebook, Twitter, and more.

Is js passport secure?

Passport. js provides authentication, not security. It is fairly easy to misconfigure by following online tutorials, so take care - the tool is only as good as the hand it is in.

What does passport authenticate () do?

In this route, passport. authenticate() is middleware which will authenticate the request. By default, when authentication succeeds, the req. user property is set to the authenticated user, a login session is established, and the next function in the stack is called.

Is Passportjs open source?

Funding. This software is provided to you as open source, free of charge.

What makes passport’s spa facilities unique?

Each of Passport’s pavilions feature signature spa offerings from around the world. Float on the world’s largest Dead Sea flotation pool, excluding the Dead Sea itself, alongside mud and clay pools. Journey through the Grotto, a subterranean cave network featuring snow, steam, sauna and salt rooms

What is passport JS?

Until now, Passport.js still a robust, flexible, and modular authentication middleware for Node.js environment. So, in this tutorial, you will see a lot of Passport.js domination.

What to do at passport springs & spa?

Passport Springs & Spa recreates the most celebrated hot springs from around the world. Soak in pools amid a tropical jungle. Float in North America’s largest Dead Sea pool. Recline in a Roman courtyard. Sip sake under a pagoda.


1 Answers

You provide the callback url to the authentication service, you decide whether you handle the route by the SPA or the API. Oauth authentication (simplified) has two steps. Illustration on github:

Step 1) https://github.com/login/oauth/authorize?client_id=*YOUR_CLIENT_ID*$redirect_uri=*YOUR_REDIRECT_URI*
Opens a popup dialog that requests the user to authorize your application, if successful returns to your redirect_uri with a query parameter ?code=AUTHORIZATION_CODE

Step 2)You exchange the above AUTHORIZATION_CODE for a long-term access token via https://github.com/login/oauth/access_token

In your architecture, you should do Step 1 in the SPA and Step 2 in the rest api. You should rely on the spa to get the authorization code from the authentication provider, send that to your rest api, let the rest api exchange for a long term access token, save that token to the database, use it to retrieve user information or do whatever you want with it, then log in the user.

For step 1, you only need the CLIENT_ID, for step 2 CLIENT_ID and CLIENT_SECRET as well, so you can keep your application secure by storing the CLIENT_SECRET on the server side only.

The problem with having the callback uri handled by your rest api, is that the callback uri is called by the authentication provider (in this case github) and not by your SPA, therefore you can't send a response that redirects the user to the homepage. This would only work if your templates and routing were handled on the server side, which I assume is not the case in your architecture.

It's not obvious from the documentation, but when you register a passport middleware on a route like app.post('/login', passport.authenticate('github'),, the middleware will check if the 'code' query param contains an AUTHORIZATION_CODE, if not, it kicks off step 1, if yes step2.

like image 158
marton Avatar answered Sep 19 '22 16:09

marton