Skip to content

Commit

Permalink
Add createUserWithIdp endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
OtterleyW committed Oct 1, 2020
1 parent dfa7025 commit ade859f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
81 changes: 81 additions & 0 deletions server/api/auth/createUserWithIdp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const http = require('http');
const https = require('https');
const sharetribeSdk = require('sharetribe-flex-sdk');
const { handleError, serialize, typeHandlers } = require('../../api-util/sdk');

const CLIENT_ID = process.env.REACT_APP_SHARETRIBE_SDK_CLIENT_ID;
const CLIENT_SECRET = process.env.SHARETRIBE_SDK_CLIENT_SECRET;
const TRANSIT_VERBOSE = process.env.REACT_APP_SHARETRIBE_SDK_TRANSIT_VERBOSE === 'true';
const USING_SSL = process.env.REACT_APP_SHARETRIBE_USING_SSL === 'true';
const BASE_URL = process.env.REACT_APP_SHARETRIBE_SDK_BASE_URL;

const FACBOOK_APP_ID = process.env.REACT_APP_FACEBOOK_APP_ID;
const GOOGLE_CLIENT_ID = process.env.REACT_APP_GOOGLE_CLIENT_ID;

const FACEBOOK_IDP_ID = 'facebook';
const GOOGLE_IDP_ID = 'google';

// Instantiate HTTP(S) Agents with keepAlive set to true.
// This will reduce the request time for consecutive requests by
// reusing the existing TCP connection, thus eliminating the time used
// for setting up new TCP connections.
const httpAgent = new http.Agent({ keepAlive: true });
const httpsAgent = new https.Agent({ keepAlive: true });

const baseUrl = BASE_URL ? { baseUrl: BASE_URL } : {};

module.exports = (req, res) => {
const tokenStore = sharetribeSdk.tokenStore.expressCookieStore({
clientId: CLIENT_ID,
req,
res,
secure: USING_SSL,
});

const sdk = sharetribeSdk.createInstance({
transitVerbose: TRANSIT_VERBOSE,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
httpAgent,
httpsAgent,
tokenStore,
typeHandlers,
...baseUrl,
});

const { idpToken, idpId, ...rest } = req.body;

// Choose the idpClientId based on which authentication method is used.
const idpClientId =
idpId === FACEBOOK_IDP_ID ? FACBOOK_APP_ID : idpId === GOOGLE_IDP_ID ? GOOGLE_CLIENT_ID : null;

sdk.currentUser
.createWithIdp({ idpId: FACEBOOK_IDP_ID, idpClientId, idpToken, ...rest })
.then(() =>
// After the user is created, we need to call loginWithIdp endpoint
// so that the user will be logged in.
sdk.loginWithIdp({
idpId,
idpClientId: `${idpClientId}`,
idpToken: `${idpToken}`,
})
)
.then(apiResponse => {
const { status, statusText, data } = apiResponse;
res
.clearCookie('st-authinfo')
.status(status)
.set('Content-Type', 'application/transit+json')
.send(
serialize({
status,
statusText,
data,
})
)
.end();
})
.catch(e => {
handleError(res, e);
});
};
4 changes: 4 additions & 0 deletions server/apiRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const transactionLineItems = require('./api/transaction-line-items');
const initiatePrivileged = require('./api/initiate-privileged');
const transitionPrivileged = require('./api/transition-privileged');

const createUserWithIdp = require('./api/auth/createUserWithIdp');

const router = express.Router();

// ================ API router middleware: ================ //
Expand Down Expand Up @@ -50,4 +52,6 @@ router.post('/transaction-line-items', transactionLineItems);
router.post('/initiate-privileged', initiatePrivileged);
router.post('/transition-privileged', transitionPrivileged);

router.post('/auth/create-user-with-idp', createUserWithIdp);

module.exports = router;

0 comments on commit ade859f

Please sign in to comment.