Google OAuth2 callback error - mongodb

Very, very new to user authentication. Looking to allow users to register with Google.
Current status: I'm successfully making it to this step -
(hiding my email(s))
But, when I click an account, I'm getting this error (500) (using longjohn for longer stack traces):
Error
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:95:21
at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:132:9)
at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
at IncomingMessage.Readable.on (_stream_readable.js:689:35)
at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:560:20)
---------------------------------------------
at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
at exports.OAuth2.get (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:227:8)
at Strategy.userProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-google-oauth20/lib/strategy.js:84:16)
at loadIt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:345:17)
at Strategy.OAuth2Strategy._loadUserProfile (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:360:25)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:168:16
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:209:7
at passBackControl (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:134:9)
at IncomingMessage.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:157:7)
at emitNone (events.js:91:20)
at IncomingMessage.emit (events.js:188:7)
at endReadableNT (_stream_readable.js:975:12)
at _combinedTickCallback (internal/process/next_tick.js:80:11)
at process._tickCallback (internal/process/next_tick.js:104:9)
---------------------------------------------
at IncomingMessage.Readable.on (_stream_readable.js:689:35)
at ClientRequest.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:156:14)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at HTTPParser.parserOnIncomingClient (_http_client.js:522:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:411:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at TLSSocket.Readable.push (_stream_readable.js:136:10)
at TLSWrap.onread (net.js:560:20)
---------------------------------------------
at exports.OAuth2._executeRequest (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:147:11)
at exports.OAuth2._request (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:120:8)
at exports.OAuth2.getOAuthAccessToken (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/oauth/lib/oauth2.js:190:8)
at loaded (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:164:20)
at NullStore.verify (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/state/null.js:9:3)
at Strategy.OAuth2Strategy.authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport-oauth2/lib/strategy.js:210:26)
at attempt (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:348:16)
at authenticate (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/passport/lib/middleware/authenticate.js:349:7)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
at next (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:174:3)
at router (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:317:13)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/express/lib/router/index.js:335:12)
---------------------------------------------
at handleOperationCallback (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:454:24)
at /Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:490:9
at authenticateStragglers (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:429:16)
at Connection.messageHandler (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:463:5)
at Socket.<anonymous> (/Users/johnsoct/Dropbox/Development/squashtomato/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:319:22)
at emitOne (events.js:96:13)
at Socket.emit (events.js:191:7)
at readableAddChunk (_stream_readable.js:178:18)
at Socket.Readable.push (_stream_readable.js:136:10)
at TCP.onread (net.js:560:20)
My code surrounding this functionality is:
// routes.js
router.get('/auth/google', authController.google);
router.get('/oauth2callback', authController.googleCallback
);
// passport.js
const GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: "http://localhost:21015/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
const user = new User();
user.google.id = profile.id;
user.google.token = accessToken;
user.google.name = profile.displayName;
user.google.email = profile.emails[0].value;
user.save(function(err) {
if (err) throw err;
return done(null, user);
});
}
));
// authController.js
const passport = require('passport');
exports.google = passport.authenticate('google', {
scope: 'https://www.google.com/m8/feeds'
});
exports.googleCallback = passport.authenticate('google', {
failureRedirect: '/login', // if fail, where to go
failureFlash: 'Failed Login!', // if fail, what to flash
successRedirect: '/', // if success, where to go
successFlash: 'You are now logged in!' // if success, what to flash
});

Apparently you can not use an IP address as and end-point, so try to change your callback-url to : http://localhost:21015/oauth2callback

Make sure you have enabled Google+ API in https://console.cloud.google.com.
To use google Auth you have to enable the API in google console. A very common mistake is when you go to Google Developer Console to enable an API for your project, first time for a project it asks you to create Credentials. The problem is after you create credentials the google+ API (or any other APIs) won't be enabled automatically and you should enable it from your project page in google developer console.
STEPS:
Go to Google Console
Select your project from the top nav (if you didn't yet)
Click on Enable APIS and Services
In the search box search for Google Plus
Select the first result , Google+ API
Click Enable
If you don't see the Enable button and see the 'Create Credentials' button,it means you didn't create credentials yet and follow the instructions on the page to create one. Don't forget to enable API after creating credentials.
For more information you can read this instruction from google Enable and disable APIs
Besides what mentioned above, you don't need to use an absolute URL for your callback and simply you can use a relative one like: /auth/google/callback
passport.use(new GoogleStrategy({
clientID: process.env.GOOGLE_CONSUMER_KEY,
clientSecret: process.env.GOOGLE_CONSUMER_SECRET,
callbackURL: "/auth/google/callback"
},
function(accessToken, refreshToken, profile, done) {
const user = new User();
user.google.id = profile.id;
user.google.token = accessToken;
user.google.name = profile.displayName;
user.google.email = profile.emails[0].value;
user.save(function(err) {
if (err) throw err;
return done(null, user);
});
}
));

Related

Error: MongoError: bad auth: Authentication failed

You might be trying to tell me to "change my password" or "remove brackets, these <>"
I have done both. I have made sure that the password I'm using is correct.
Here is the URI:
mongodb+srv://user:pass#notlbot.iclv0tm.mongodb.net/?retryWrites=true&w=majority
no, my username isnt 'user' and my password also is not 'pass'
I don't know what to do from here, though it may be important to note I am trying to connect from a Linodes server. I'm using AWS to host Atlas.
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at TLSSocket.ondata (node:internal/streams/readable:754:22)
at TLSSocket.emit (node:events:527:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) {
ok: 0,
code: 8000,
codeName: 'AtlasError'
}

Heroku app fails to connect to mongodb atlas server (after migration from mlab)

I recently migrated from mlab to mongodb atlas due to its deprecation. I managed to successfully get a connection to the db and tested it with the mongoDB compass but when I add the environment variable to heroku it gives me the following error message:
MongoNetworkError: failed to connect to server [cluster-grwbxfbx-shard-00-02.e6jtl.mongodb.net:27017] on first connect [MongoNetworkError: connection 4 to cluster-grwbxfbx-shard-00-02.e6jtl.mongodb.net:27017 closed
2020-11-11T23:53:44.726144+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:372:9)
2020-11-11T23:53:44.726144+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726145+00:00 app[web.1]: at TLSSocket.emit (events.js:314:20)
2020-11-11T23:53:44.726145+00:00 app[web.1]: at net.js:676:12
2020-11-11T23:53:44.726146+00:00 app[web.1]: at TCP.done (_tls_wrap.js:568:7)]
2020-11-11T23:53:44.726147+00:00 app[web.1]: at Pool.<anonymous> (/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
2020-11-11T23:53:44.726147+00:00 app[web.1]: at Pool.emit (events.js:314:20)
2020-11-11T23:53:44.726148+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:562:14
2020-11-11T23:53:44.726149+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/pool.js:1009:9
2020-11-11T23:53:44.726149+00:00 app[web.1]: at callback (/app/node_modules/mongodb/lib/core/connection/connect.js:97:5)
2020-11-11T23:53:44.726150+00:00 app[web.1]: at /app/node_modules/mongodb/lib/core/connection/connect.js:124:7
2020-11-11T23:53:44.726150+00:00 app[web.1]: at _callback (/app/node_modules/mongodb/lib/core/connection/connect.js:349:5)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Connection.errorHandler (/app/node_modules/mongodb/lib/core/connection/connect.js:365:5)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726151+00:00 app[web.1]: at Connection.emit (events.js:314:20)
2020-11-11T23:53:44.726152+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/mongodb/lib/core/connection/connection.js:370:12)
2020-11-11T23:53:44.726152+00:00 app[web.1]: at Object.onceWrapper (events.js:421:26)
2020-11-11T23:53:44.726153+00:00 app[web.1]: at TLSSocket.emit (events.js:314:20)
2020-11-11T23:53:44.726153+00:00 app[web.1]: at net.js:676:12
2020-11-11T23:53:44.726153+00:00 app[web.1]: at TCP.done (_tls_wrap.js:568:7)
2020-11-11T23:53:44.812696+00:00 heroku[web.1]: Process exited with status 0
2020-11-11T23:53:44.850554+00:00 heroku[web.1]: State changed from starting to crashed
2020-11-11T23:53:44.854049+00:00 heroku[web.1]: State changed from crashed to starting
2020-11-11T23:53:47.149749+00:00 heroku[web.1]: Starting process with command `npm start`
2020-11-11T23:53:49.907225+00:00 app[web.1]:
It seems as if heroku is adding a port to the end of the MONGODB_URI. Does anyone know whats going on?
my config var in heroku is:
MONGODB_URI = mongodb+srv://username:password#cluster-grwbxfbx.e6jtl.mongodb.net/test?retryWrites=true&w=majority
with username and password being replaced by my admin username and password.
Also here is my code for connecting to the db
import mongoose from 'mongoose'
import options from '../config'
export const connect = (url = options.dbUrl, opts = {}) => {
return mongoose.connect(url, { ...opts, useNewUrlParser: true })
}
export const config = {
secrets: {
jwt: 'learneverything'
},
dbUrl: process.env.MONGODB_URI || 'mongodb://localhost:27017/api-design'
}
Let me know if you need me to post anything else

What is this Mongoose CursorNotFound Error?

I have mongoose throwing this error from time to time, it's not stopping the node application from running but I can't find any information about whats causing the error and I would like to fix it.
The link to the repo is here https://github.com/bkawk/divergence2
I have found I can stop the error by adding a limit to the records here
https://github.com/bkawk/divergence2/blob/master/src/db.js#L94
{ MongoError: cursor id 31871209567 not found
at /Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:598:61
at authenticateStragglers (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:516:16)
at Connection.messageHandler (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/pool.js:552:5)
at emitMessageHandler (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:309:10)
at Socket.<anonymous> (/Users/bkawk/Documents/divergence2/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:488:15)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
at Socket.Readable.push (_stream_readable.js:208:10)
at TCP.onread (net.js:607:20)
name: 'MongoError',
message: 'cursor id 31871209567 not found',
ok: 0,
errmsg: 'cursor id 31871209567 not found',
code: 43,
codeName: 'CursorNotFound' }

Error: connection timeout - expressjs mongodb mongoose

I'm new to express/node and web programming in general. What is the best way to deal with this error when mongoose's mongodb connection times out, this is how I connect:
mongoose.connect(config.mongoUrl);
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'error connecting with mongodb database:'));
db.once('open', function() {
console.log('connected to mongodb database');
});
This is the error when it times out while my server is running:
error connecting with mongodb database: Error: connection timeout
at Db. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongoose\lib\drivers\node-mongodb-native\connection.js:169:17)
at emitTwo (events.js:106:13)
at Db.emit (events.js:191:7)
at Server.listener (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb\lib\db.js:1798:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Server. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb\lib\server.js:274:14)
at emitOne (events.js:96:13)
at Server.emit (events.js:188:7)
at Pool. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb-core\lib\topologies\server.js:335:12)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection. (C:\Users\Sean\OneDrive\webpages\000\lasttry\node_modules\mongodb-core\lib\connection\pool.js:270:12)
at Connection.g (events.js:291:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
How about on disconnect just reconnect to mongo. See below:
mongoose.connect(config.mongoUrl);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'error connecting with mongodb database:'));
db.once('open', function() {
console.log('connected to mongodb database');
});
db.on('disconnected', function () {
//Reconnect on timeout
mongoose.connect(config.mongoUrl);
db = mongoose.connection;
});
You can also set a timeout value on the connection.
mongoose.connect(url, { server: { socketOptions: { connectTimeoutMS: 1000 }}}, function(err) { ... });
Also, make sure that mongo is still running on your machine. A connection timeout could mean mongo isn't running.
Reference: Another stack overflow question
Check mongod is running.
typing mongo in shell.
Add connectTimeoutMS=300000 paramter for you uri.
uri looks like mongodb://localhost/collectionName?connectTimeoutMS=300000

Can't do basic find with Waterline in sails console

I have a Job model that searches a table of jobs. The table was created using rails, and I set 'migrate' to 'safe' in the Job model. I have written out all the attributes in the model. I cannot get basic functions to work in the sails console. I have three jobs in the table, with ids 1,2, and 3. When I execute:
sails> Job.findOneById(1).done(function(err,job){console.log(j.id)})
It poops out on me. The sails console crashes (super annoying). The output looks like this:
sails> Job.findOneById(1).done(function(err,job){console.log(j.id)})
undefined
sails>
/home/redmage/tac/node_modules/sails-postgresql/lib/query.js:544
var type = self._schema[key].type;
^
TypeError: Cannot read property 'type' of undefined
at /home/redmage/tac/node_modules/sails-postgresql/lib/query.js:544:33
at Array.forEach (native)
at Query.cast (/home/redmage/tac/node_modules/sails-postgresql/lib/query.js:541:23)
at /home/redmage/tac/node_modules/sails-postgresql/lib/adapter.js:372:32
at Array.forEach (native)
at __FIND__ (/home/redmage/tac/node_modules/sails-postgresql/lib/adapter.js:371:23)
at Query.handleReadyForQuery (/home/redmage/tac/node_modules/sails-postgresql/node_modules/pg/lib/query.js:84:10)
at null.<anonymous> (/home/redmage/tac/node_modules/sails-postgresql/node_modules/pg/lib/client.js:159:19)
at EventEmitter.emit (events.js:117:20)
at null.<anonymous> (/home/redmage/tac/node_modules/sails-postgresql/node_modules/pg/lib/connection.js:97:12)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
at TCP.onread (net.js:528:21)
Here is the ORM for Job. It is very basic:
module.exports = {
//VERY IMPORTANT:
migrate: 'safe',
tableName: 'jobs',
attributes: {
}
}