Node.js/express 400 error

API Discussion
Hello!

I am trying to figure out how to build a basic node.js/express backend for oauth. I have never used express before so this is new to me. I am following the node.js/express bnet strategy
https://www.npmjs.com/package/passport-bnet
and I am also following this guide by Uglyer https://us.battle.net/forums/en/bnet/topic/20767577109?page=1#post-4

When I run my server (assuming I have it even set up correctly) and try to hit
localhost:8000/auth/bnet I get a 400 error 'The redirect_uri parameter must be a valid URI' and a URL of
https://us.battle.net/oauth/authorize?response_type=code&client_id=29a27f8c46ba464fb3826384e7ed6d7c

Here is my server.js file

require('dotenv').config();
process.env.NODE_ENV= "development";

const express = require('express');
const passport = require('passport');
const constants = require('constants');
const BnetStrategy = require('passport-bnet').Strategy;

const app = express();
const port = process.env.PORT || 8000;

app.listen(port, () => {
console.log('Server started!');
});

// ROUTES

//instance of router
const router = express.Router();

router.use((req, res, next) => {
console.log(req.method, req.url);
next();
});

// home page
router.get('/', (req, res) => {
res.send('I am the home page');
});

/*=========================
// Bnet OAuth2 Navigate to
// ===========================*/
// Authenticate Requests
router.get('/auth/bnet', (req, res, next) => {
passport.authenticate('bnet',
(err, user, info) => {
if (err) {
return next(err)
}
})(req, res, next);
});

//*============================
// Bnet OAuth2 Callback Route
// ==============================*
router.get('/auth/bnet/callback', (req, res, next) => {
passport.authenticate('bnet', { failureRedirect: constants.DOMAIN
+ '/loginFailure' }, (err, user, info) => {
if (err) {
return next(err);
}

console.log(user);
res.redirect(constants.DOMAIN + '/loginSuccess' );
})(req, res, next);
});

// handle CORS

const cors = require('cors');

// apply routes to app
app.use('/', router, cors());

// Use the BnetStrategy within Passport.
passport.use(new BnetStrategy({
grant_type: 'authorization_code',
clientID: process.env.BNET_ID,
clientSecret: process.env.BNET_SECRET,
callbackURL: constants.CALLBACKURL,
region: 'us'
}, (accessToken, refreshToken, profile, done) => {
// Profile returns the Bnet Id, Battletag
return done(null, profile);
}));


Here is my .env file

BNET_ID='myID'
BNET_SECRET='mySecret'


Here is my constants file

let constants = {};
if (process.env.NODE_ENV === 'production') {
constants.DOMAIN = '';
constants.CALLBACKURL = 'https://localhost:8000/auth/bnet/callback'
} else {
constants.DOMAIN = 'https://localhost:' + 4200;
constants.CALLBACKURL = 'https://myBackendApp.localtunnel.me/auth/bnet/callback'
}

module.exports = constants;


As I said I am new to to express. I'm sure I could move some of this stuff to separate files and clean up some areas, but at the moment I just want to make sure things are in order, as I'm aware code order matters greatly in express, and if I've set up things in a way that should be working as I intend. On my front end I have a button with the tag
<a href="https://myBackendApp.localtunnel.me/auth/bnet/"> that returns a 404 but that is another issue I can solve later. I just want to figure out the node.js/express stuff.

Thanks for taking the time to read this!

Join the Conversation

Return to Forum