admin 管理员组

文章数量: 1086019

I've got an error while i try to auth via vkontakte (vk) (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 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 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

I've got an error while i try to auth via vkontakte (vk.) (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. 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. 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

Share Improve this question edited Jan 8, 2015 at 22:23 Martijn Pieters 1.1m321 gold badges4.2k silver badges3.4k bronze badges asked Apr 25, 2014 at 10:02 yeswecodeyeswecode 3031 gold badge7 silver badges16 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

Define User Model

Mongoose or Mongoskin Example

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.

Boilerplate Code to Get You Started

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.

本文标签: javascriptReferenceError User is not definedStack Overflow