0
私はpassport-remember-meを設定しようとしています。私がログインして私のチェックボックスをチェックインすると "Token.saveは関数ではありません"というメッセージが表示されます。ここでは、コードは次のようになります。Token.saveは関数ではありません
構成:
var express = require("express");
var app = express();
var bodyParser = require("body-parser")
var mongoose = require("mongoose")
var methodOverride = require("method-override")
var passport = require("passport")
var LocalStrategy = require("passport-local").Strategy;
var passportLocalMongoose = require("passport-local-mongoose")
var User = require("./models/user.js")
var RememberMeStrategy = require("passport-remember-me").Strategy;
var utils = require("npm-utils")
var flash = require("connect-flash")
var uid = require('uid2');
var cookieParser = require("cookie-parser")
function generateToken(number) {
return uid(number);
}
app.use(bodyParser.urlencoded({
extended: true
}));
mongoose.connect("mongodb://localhost/rina_app");
//mongoose.connect(process.env.DATABASEURL);
app.use(express.static("public"));
app.use(require("express-session")({
secret: "Sky",
resave: false,
saveUninitialized: false
}))
app.use(cookieParser());
app.use(passport.authenticate('remember-me'));
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride("_method"));
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
passport.use(new RememberMeStrategy(
function(token, done) {
Token.consume(token, function (err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
return done(null, user);
});
},
function(user, done) {
var token = utils.generateToken(64);
Token.save(token, { userId: user.id }, function(err) {
if (err) { return done(err); }
return done(null, token);
});
}
));
app.use(function(req, res, next){
res.locals.currentUser = req.user;
next();
})
トークンモデル:
var TokenSchema = new mongoose.Schema({
token: {type: String},
createDate: {type: Date, default: Date.now}
});
var time = mongoose.model("time", timeSchema);
var Token = mongoose.model("Token", TokenSchema);
ログインルート:
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
function(req, res, next) {
// issue a remember me cookie if the option was checked
if (!req.body.remember_me) { return next(); }
var token = generateToken(64);
Token.save(token, { userId: req.user.id }, function(err, done) {
if (err) { return done(err); }
res.cookie('remember_me', token, { path: '/', httpOnly: true, maxAge: 604800000 }); // 7 days
return next();
});
},
function(req, res) {
res.redirect('/');
});