【问题标题】:oauth2-server implementation nodejsoauth2-server 实现 nodejs
【发布时间】:2018-06-12 00:53:33
【问题描述】:

我正在尝试在 nodeJS 中实现 OAUTH2 服务器,它允许客户端应用程序使用我的网站登录用户(例如使用 google 登录,在我的情况下是使用此 API/客户端应用程序的亚马逊 alexa)。
我尝试使用 oauth2orise(https://www.npmjs.com/package/oauth2orize) 并引用了一些链接:-


提前致谢。

【问题讨论】:

    标签: javascript node.js oauth-2.0 oauth2orize


    【解决方案1】:

    我从这个存储库中得到了一个非常解释性的答案。

    https://github.com/FrankHassanabad/Oauth2orizeRecipes

    谢谢。

    【讨论】:

      【解决方案2】:

      您可以使用 passportjs 为您提供 oauth 2.0 支持。您将需要 googleClientID 和 googleClientSecret。您可以通过将您的应用程序注册到谷歌开发者网站来获得

      var GoogleStrategy = require('passport-google-oauth20').Strategy;
      
       const mongoose = require('mongoose');
       const keys = require('./keys');
       const User = mongoose.model('users');
      
      module.exports = function(passport){
        passport.use(
      new GoogleStrategy({
        clientID:keys.googleClientID,
        clientSecret:keys.googleClientSecret,
        callbackURL:'/auth/google/callback',
        proxy:true
      },(accessToken,refreshToken,profile,done)=>{
      //     console.log(accessToken);
      //     console.log(profile);
        const image = profile.photos[0].value.substring(0,profile.photos[0].value.indexOf('?'));
      
        const newUser = {
          googleID:profile.id,
          firstName:profile.name.givenName,
          lastName :profile.name.familyName,
          email:profile.emails[0].value,
          image:image
        }
      
        //Check for existing user
        User.findOne({
          googleID:profile.id
        }).then(user=>{
          if(user){
            //Return user
            done(null,user);
          }
          else{
            //Create a new user
            new User(newUser)
            .save()
            .then(user=> done(null,user)); 
          }
        })
       })
      )
      
      passport.serializeUser(function(user, done) {
      done(null, user.id);
      });
      
      passport.deserializeUser(function(id, done) {
       User.findById(id, function(err, user) {
         done(err, user);
           });
         });
       }
      

      依赖 = "护照": "^0.4.0", "passport-google-oauth": "^1.0.0"

      This will redirect req. to above code..
      const express = require('express');
      const router = express.Router();
      const passport = require('passport');
      
       router.get('/google',passport.authenticate('google',{scope:
            ['profile','email']}));
      
       router.get('/google/callback', 
         passport.authenticate('google', { failureRedirect: '/' }),
         function(req, res) {
         // Successful authentication, redirect home.
         res.redirect('/dashboard');
        });
      
         router.get('/verify',(req,res)=>{
         if(req.user){
         console.log(req.user);
        }else{
         console.log('Not Auth');
        }
      });
      
      router.get('/logout',(req,res)=>{
         req.logout();
         res.redirect('/');
       })
       module.exports = router;
      

      【讨论】:

      • 谢谢.. 但我正在努力成为一个资源提供者,就像你的谷歌一样
      猜你喜欢
      • 1970-01-01
      • 2017-03-08
      • 2015-01-04
      • 2013-01-09
      • 2017-11-29
      • 2017-08-06
      • 2016-07-03
      • 2020-11-03
      • 2016-12-27
      相关资源
      最近更新 更多