0
votes

I'm currently trying to build an express server that allows the user to authenticate via two third-party services: Google and Steam. The authentication is done via JWT and it works when only one of the two services is activated, but I can't get the protected routes to work when the user is logged in using one of the two services.

The code:

const express = require('express');
const bodyParser = require('body-parser');
const {gAuth, gProtect} = require('./auth/google_auth');
const {sAuth, sProtect} = require('./auth/steam_auth');

const app = express();

gAuth(app);
sAuth(app);

//middlewares
app.use(express.static('views'));// folder in which to put the static files (html, css, js client)
app.use(bodyParser.json({limit: '50mb'})); // read json

//middlewares (just for the widget)
app.use(bodyParser.urlencoded({ extended: true , limit: '50mb'})); // read form enctype data
app.set('view engine', 'ejs'); // set the engine render ejs for dynamic building of html pages with ejs tags

//set up routers for v1 app
const genericRouter = require('./routes/generic_router'); //testing if the application is working
const secureRouter = require('./routes/secure_router'); //testing if normal auth is working

//set up routers for latest version app
app.use('/', genericRouter);
app.use('/', gProtect(), secureRouter);//protected end-points (requiring auth)
app.use('/', sProtect(), secureRouter);

module.exports = app;

The problem is in the

app.use('/', gProtect(), secureRouter);//protected end-points (requiring auth)
app.use('/', sProtect(), secureRouter);

bit, since the second app.use overwrites the first, making all authentication attempts with Google fail. What can I do to make it so that the user can access the protected routes either with Google or Steam?

1

1 Answers

1
votes

Use different endpoints

app.use('/', genericRouter);
app.use('/google', gProtect(), secureRouter);
app.use('/steam', sProtect(), secureRouter);