Reputation: 303
I've got an error while i try to auth via vkontakte (vk.com) (passport-vkontakte)
Error: ReferenceError: User is not defined
Here is my auth.js
var express = require('express');
var passport = require('passport'), VKontakteStrategy = require('passport-vkontakte').Strategy;
var router = express.Router();
var app = express();
router.get('/', function(req, res) {
res.send('Auth');
});
passport.use(new VKontakteStrategy({
clientID: 000, // VK.com docs call it 'API ID'
clientSecret: '***',
callbackURL: "http://127.0.0.1:3000/auth/vkontakte/callback"
},
function(accessToken, refreshToken, profile, done) {
User.findOrCreate({ vkontakteId: profile.id }, function (err, user) {
return done(err, user);
});
}
));
router.get('/vkontakte',
passport.authenticate('vkontakte'),
function(req, res){
// The request will be redirected to vk.com for authentication, so
// this function will not be called.
});
router.get('/vkontakte/callback',
passport.authenticate('vkontakte', { failureRedirect: '/' }),
function(req, res) {
// Successful authentication, redirect home.
var User = req.User;
res.redirect('/');
});
router.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
module.exports = router;
Express ver: 4
Upvotes: 2
Views: 23701
Reputation: 7746
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/my_database');
var Schema = mongoose.Schema
, ObjectId = Schema.ObjectId;
var BlogPost = new Schema({
author : ObjectId
, title : String
, body : String
, date : Date
});
var Post = mongoose.model('ModelName', BlogPost);
Post.findOrCreate({ id: QUERY }, function (err, docs) {
});
You need to create a model so your strategy can interact with your application properly. In your case, this model will be for users so it is appropriately named User
as an entity.
This code was added from the strategy developers to get you started.
User.findOrCreate({ vkontakteId: profile.id }, function (err, user) {
return done(err, user);
});
It's the same concept, hope this clears things up.
Upvotes: 3