NodeJs configured and running fine with passportJS OAuth2, but the requirement is angular should call node api, both are runs in different ports, calling all nodeJS's rest API from angular and it runs fine using proxy.conf.json, while calling /googleauth/redirect from angular getting error response.
Response :
Failed to load https://accounts.google.com/o/oauth2/v2/auth?response_type=code... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.
Google API Setting :
Restrictions
Authorised JavaScript origins :
http://localhost:4200
http://localhost:8088
Authorised redirect URIs :
http://localhost:4200/api/googleauth/redirect
http://localhost:8088/api/googleauth/redirect
Update 1 : CORS to app.js (new): but no changes.
var cors = require('cors');
var app = express();
app.use(cors());
app.options('*', cors());
app.use('/api',cors(),require('./routes/api'));
Update 2 : api.js
router.get('/google', passport.authenticate('google', { scope: ['profile','email'] }));
passport setup
const passport = require('passport');
const GoogleStrategy = require('passport-google-oauth20');
const userModel = require('../model/user');
passport.serializeUser((user,done)=>{
done(null,user.id);
});
passport.deserializeUser((id,done)=>{
userModel.findById(id).then((user)=>{
done(null,user);
})
});
passport.use(
new GoogleStrategy({
callbackURL:'/api/googleauth/redirect',
clientID:'',
clientSecret:''
},(accessToken,refreshToken,profile,done) =>{
console.log("call back function fired");
//console.log(accessToken);
userModel.findOne({email:profile.emails[0].value,authType:'google'},function(err,user){
if(user){
done(null,user);
}else{
done(null,user);
}
});
}))
/googlewith $http.get? in this case angular will try to follow the redirect instead of changing the location of the browser to the google URL, causing the CORS error on the google URL. instead you need to make sure that the redirect causes a redirect on the browser - Qiong Wu