我尝试通过vkontakte(vk.com)(passport-vkontakte)进行身份验证时出错了
错误: ReferenceError:未定义用户
这是我的 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
答案 0 :(得分:1)
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) {
});
您需要创建一个模型,以便您的策略可以正确地与您的应用程序进行交互。在您的情况下,此模型将针对用户,因此它被恰当地命名为User
作为实体。
此代码是从策略开发人员中添加的,以帮助您入门。
User.findOrCreate({ vkontakteId: profile.id }, function (err, user) {
return done(err, user);
});
这是一个相同的概念,希望这可以解决问题。