LoginSignup
2
1

More than 5 years have passed since last update.

passportでユーザオブジェクトのプロパティ名を変更する

Posted at

概要

passportの認証ミドルウェアでコールバックに渡したユーザオブジェクトに,
他のconnectミドルウェアからアクセスしたい場合,
req.userという風に,リクエストオブジェクトを通してアクセスします.

デフォルトでは,リクエストオブジェクトのuserというプロパティにユーザオブジェクトはセットされます.
この設定を変えたい場合どうするか,というお話です.

解説

passportのオプションにassignPropertyを指定してやればOKです.

app.post(
  '/protected-page',
  passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/login',
    assignProperty: 'account'
  }),
  (req, res, next) => res.render('protected-page', { account : req.account }) 
);
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1