I am working on my first app using Satellizer that contains a NodeJS/Express back and an AngularJS front. I want my users to log in with their Google account.
The front-end is at port 8000 and I have configured it like so :
app.config(function($authProvider) {
$authProvider.google({
url: 'http://localhost:3000/auth/google',
clientId: 'id.apps.googleusercontent.com',
})
});
For the back (port 3000), i followed the github repo exemple. here
app.post('/auth/google', function(req, res) {
var accessTokenUrl = 'https://accounts.google.com/o/oauth2/token';
var peopleApiUrl = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect';
var params = {
code: req.body.code,
client_id: req.body.clientId,
client_secret: config.GOOGLE_SECRET,
redirect_uri: req.body.redirectUri,
grant_type: 'authorization_code'
};
...
When I click the "log in" button, the pop up pops up, I can use my credentials and it closes. But I am not logged and there is an error in the back: 'TypeError: Cannot read property 'code' of undefined.'
One console.log() later, I found that the problem is that req.body is empty. And that is where I am stuck. Am I missing a step?
Here are the requests send to Express :
[ 1531831850948 ] OPTIONS /auth/google
[ 1531831851007 ] POST /auth/google
I have also enable CORS
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Context is missing, but my guess is you need a body parser middleware.
Check out body-parser
(link), and in your app use it like this:
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
app.use(function (req, res) {
res.end('you posted:\n' + JSON.stringify(req.body, null, 2))
})
This example will send back the post payload.