Ответы пользователя по тегу Node.js
  • Авторизация через node.js passport-facebook + jwtwebtoken в rest api приложении?

    @akyl-kb
    Для начала нужно удобнее организовать структуру
    Пример как у меня организовано:
    - /models/User.js
    - /passports/facebook.js
    - /passports/google.js
    - /passport.js
    - /routes.js

    Содержимое passport.js
    const User = require('./models/User')
    const facebookStrategy = require('./passports/facebook')
    const googleStrategy = require('./passports/google')
    
    module.exports = function (passport) {
      passport.serializeUser(function (user, done) {
          return done(null, user.id)
      })
    
      passport.deserializeUser(async (id, done) => {
        try {
          const user = await User.findOne({ id })
          done(null, user)
        } catch (err) {
          done(err)
        }
      })
    
      passport.use(facebookStrategy())
      passport.use(googleStrategy())
    }


    Содержимое facebookStrategy.js:
    const assert = require('assert')
    const FacebookStrategy = require('passport-facebook').Strategy
    const User = require('../models/User')
    
    assert(process.env.FACEBOOK_APP_ID)
    assert(process.env.FACEBOOK_APP_SECRET)
    assert(process.env.APP_URL)
    
    module.exports = () => {
      return new FacebookStrategy({
        clientID: process.env.FACEBOOK_APP_ID,
        clientSecret: process.env.FACEBOOK_APP_SECRET,
        callbackURL: `${process.env.APP_URL}/auth/facebook/callback`
      },
      async (accessToken, refreshToken, profile, done) => {
        try {
          const user = await User.findOrCreate({ facebook_id: profile.id }, {
            facebook_id: profile.id,
            name: profile.displayName,
            provider: 'facebook'
          })
          return done(null, user.toJSON())
        } catch (err) {
          return done(err)
        }
      })
    }


    routes.js
    app.get('/auth/facebook', passport.authenticate('facebook'))
    app.get('/auth/facebook/callback', passport.authenticate('facebook', { failureRedirect: '/login' }, (req, res) => {
      const user = req.user;
      const payload = {
          id: user._id,
          email: user.email,
      };
    
      jwt.sign(payload, secret, {expiresIn: 3600}, (err, token) => {
          if (err) {
              res.status(500).send({
                  error: 'Error signing token',
                  raw: err,
              });
          }
          // Отдаем html
          res.send(`
          <script>
            localStorage.setItem('token', '${`Bearer ${token}`}');
            localtion.href = '/'
          </script>
          `);
      });
    })


    в app.js
    const passport = require('passport')
    require('./passport')(passport)
    app.use(passport.initialize())
    Ответ написан
    3 комментария