亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建用戶登錄并以組織 mongodb 身份登錄

創建用戶登錄并以組織 mongodb 身份登錄

回首憶惘然 2022-09-23 10:05:11
我正在構建一個應用程序,該應用程序需要注冊和登錄用戶和組織。我正在使用節點js護照 - 相同的本地策略。我已經創建了所有視圖,并與用戶一起登錄以工作,但是當我復制登錄代碼作為組織時,我收到意外的錯誤或被重定向到任何其他視圖。這是我的應用程序.js文件// Passport Configrequire('./config/Orgpassport')(Orgpassport);require('./config/passport')(passport);// Express sessionapp.use(  session({    secret: 'secret',    resave: true,    saveUninitialized: true  }));// Passport middlewareapp.use(passport.initialize());app.use(passport.session());// Connect flashapp.use(flash());這是我的護照.js文件const LocalStrategy = require('passport-local').Strategy;const bcrypt = require('bcryptjs');// Load User modelconst User = require('../models/User');module.exports = function(passport) {  passport.use(    new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {      console.log(passport);      // Match user      User.findOne({        email: email      }).then(user => {        if (!user) {          return done(null, false, { message: 'That email is not registered' });        }        // Match password        bcrypt.compare(password, user.password, (err, isMatch) => {          if (err) throw err;          if (isMatch) {            return done(null, user);          } else {            return done(null, false, { message: 'Password incorrect' });          }        });      });    })  );  passport.serializeUser(function(user, done) {    done(null, user.id);  });  passport.deserializeUser(function(id, done) {    User.findById(id, function(err, user) {      done(err, user);    });  });};這是我對登錄表單的帖子請求。router.post('/Orglogin', (req, res, next) => {  passport.authenticate('local', {    successRedirect: '/Orgdashboard',    failureRedirect: '/org/Orglogin',    failureFlash: true  })(req, res, next);});我能夠使用用戶登錄,但不能使用組織登錄?
查看完整描述

1 回答

?
慕標5832272

TA貢獻1966條經驗 獲得超4個贊

從您的前端添加一個例如,以便在您的后端您將能夠識別該請求是針對組織的,然后在您的組織架構中添加一個像這樣的布爾值,這將使您識別嘗試登錄的用戶是否來自組織, 接下來在你的護照本地策略在這一行添加這個新的這將允許你得到,然后你可以用它來實現邏輯,使用你之前設置的參數并檢查它是否存在,如果它存在,然后驗證用戶是否在數據庫中,并檢查是否設置為如果是, 驗證用戶正常,否則如果不包含組織,則意味著它不是來自組織,那么您以這種方式使用普通用戶驗證,您不需要使用2個本地策略,并且可以在此方案中使用兩個不同的模型param"org""isorganisation=true"LocalStrategy({ usernameField: 'email' ,passReqToCallback:true}requestreq,params.orgisorganisationtruereq.params



查看完整回答
反對 回復 2022-09-23
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號