错误:未知的身份验证策略“本地”

时间:2018-10-25 07:05:58

标签: node.js

正如我在标题中提到的,当我运行代码时,出现类似“错误:未知身份验证策略”本地”的错误。所以请告诉我该怎么办。 该项目位于Web应用程序的Node和Express js上,系统中安装了mongo db local。

系统环境:我有Windows操作系统,并且我在Visual Studio代码中工作。 对于数据库,我使用本地安装mongo db罗盘。

注意:这里我将html代码与ejs一起用于前端

App.js:-我在其中编写主要应用程序代码的应用程序

const express = require('express');
const chalk = require('chalk');
const path = require('path');
const body_parser = require('body-parser');
// const passpore = require('passport');
const cookie_parser=require('cookie-parser');
const session = require('express-session');

const dbconfig = require('./public/config/database_config');
// const debug = require('debug')(app);


// route for site

const homerouter = require('./scr/route/home_route');
const bandrouter = require('./scr/route/band_route');
const tourrouter = require('./scr/route/tour_route');
const contactrouter = require('./scr/route/contact_route');
const authroute = require('./scr/route/auth');
const port = process.env.PORT || 3000;

 
const app = express();

app.use(body_parser.json()); 
app.use(body_parser.urlencoded({extended:false}));
app.use(cookie_parser());
app.use(session({secret:'librery'}));
require('./public/config/passport')(app);

app.use(express.static(path.join(__dirname, '/public/')));
app.use('/css', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/css')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/bootstrap/dist/js')));
app.use('/js', express.static(path.join(__dirname, '/node_modules/jquery/dist')));

app.set('views', './scr/views');
app.set('view engine', 'ejs');


// app.set('views', './scr/views');
// app.set('view engine', 'pug');

app.use('/', homerouter);
app.use('/', bandrouter);
app.use('/', tourrouter);
app.use('/', contactrouter);
app.use('/auth', authroute);


app.get('/', (req, res) => {
  res.render('new', { list: [{ link: '/home', title: 'Home' }, { link: '/band', title: 'Band' }, { link: '/tour', title: 'Tour' }, { link: '/contact', title: 'contact' }] });
  // res.render('indext', });

  // using for normal
  //   res.sendFile(path.join(__dirname, 'view/index.html'));
});

app.listen(port, () => {
  console.log(`App is running on${chalk.green(port)}`);
});

Auth.js:-身份验证JS

const express = require('express');
// const path = require('path');
const passport = require('passport');
const MongoClient=require('mongodb').MongoClient;
const authrouter=express.Router();

authrouter.route('/signup')
 .post((req,res)=>{
  
    req.body;
    // res.json(req.body);
    const {username , password} = req.body;
    const url = 'mongodb://localhost:27017';
    const dbname = 'mydb';

    (async function  adduser() {
      let client;
      try{
        client = await MongoClient.connect(url);
        console.log("Connected perfectly");

        const db = client.db(dbname);
        const col = db.collection('users');
        const user = {username,password};
        const result = await col.insertOne(user)
        console.log(result);
        req.logIn(result.ops[0],()=>
     {
       res.redirect('/auth/profile');
     });
     
      }catch(err){
        console.log(err);
      }
    }());

    // create login
     
     
  
 });

 authrouter.route('/signin')
  .get((req,res)=>{
    res.render('signin',{
      title : 'signin'
    }
  
    
    );  
  })
   .post(passport.authenticate('local',{
    successRedirect:'/auth/profile',
    failureRedirect:'/'
   }));

 authrouter.route('/profile')
  .all((req,res,next)=>{
   if(req.user){
     next
   }else{
     res.redirect('/');
   }
 })       
 .get((req,res)=>{
        res.json(req.user);
      });
module.exports = authrouter;

passport.js:-护照js

const passport = require('passport');
require('./strategy/local.Strategy');
module.exports = function pasportconfig(app){
    app.use(passport.initialize());
    app.use(passport.session());
 
    
    // store user in session 
    passport.serializeUser((user,done)=>{
        done(null,user);
    });


    // Retrive user from user
    passport.deserializeUser((user,done)=>{
        done(null,user);
    });

   
}

本地策略:在这里,我写我的本地策略

const passport = require('passport');
const { Strategy} = require('passport-local');
const MongoClient=require('mongodb').MongoClient;

module.exports = function localstrategy(){
 passport.use(new Strategy({
     
     usernameField:'username',
     passwordField:'password'
 }, (username, password, done)=>{
     
    
    const {username , password} = req.body;
    const url = 'mongodb://localhost:27017';
    const dbname = 'mydb';

    (async function  adduser() {
      let client;
      try{
        client = await MongoClient.connect(url);
        console.log("Connected perfectly");

        const db = client.db(dbname);
        const col = db.collection('users');
       
        const user = await col.findOne({username}); 
        if(user.password == password)
        {
            done(null,user);

        }else{
            done(null,false);
        }
        
      }
        catch(err){
            console.log(err.stack);

          }

          client.close();
        }());
    
    }
 ));      
};

这是我的全部代码。

0 个答案:

没有答案