oauth2-server 实现 nodejs
oauth2-server implementation nodejs
我正在尝试在 nodeJS 中实现一个 OAUTH2 服务器,它允许客户端应用程序使用我的网站登录用户(比如使用 google 登录,在我的例子中是亚马逊 alexa,它使用这个 API/Client 应用程序)。
我尝试使用 oauth2orise(https://www.npmjs.com/package/oauth2orize) 并引用了几个链接:-
- https://hnryjms.io/2014/07/oauth2/
- http://scottksmith.com/blog/2014/07/02/beer-locker-building-a-restful-api-with-node-oauth2-server/
但我仍然无法理解如何调用方法以及我应该如何实施流程。
因此,如果有人可以解释如何实施那将会有所帮助。
提前致谢。
我从这个存储库中得到了一个非常解释性的答案。
https://github.com/FrankHassanabad/Oauth2orizeRecipes
谢谢。
您可以使用 passportjs 为您提供 oauth 2.0 支持。您将需要 googleClientID 和 googleClientSecret。您可以通过将您的应用程序注册到 google 开发者网站
来获得
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);
});
});
}
Dependencies = "passport": "^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;
我正在尝试在 nodeJS 中实现一个 OAUTH2 服务器,它允许客户端应用程序使用我的网站登录用户(比如使用 google 登录,在我的例子中是亚马逊 alexa,它使用这个 API/Client 应用程序)。
我尝试使用 oauth2orise(https://www.npmjs.com/package/oauth2orize) 并引用了几个链接:-
- https://hnryjms.io/2014/07/oauth2/
- http://scottksmith.com/blog/2014/07/02/beer-locker-building-a-restful-api-with-node-oauth2-server/
但我仍然无法理解如何调用方法以及我应该如何实施流程。
因此,如果有人可以解释如何实施那将会有所帮助。
提前致谢。
我从这个存储库中得到了一个非常解释性的答案。
https://github.com/FrankHassanabad/Oauth2orizeRecipes
谢谢。
您可以使用 passportjs 为您提供 oauth 2.0 支持。您将需要 googleClientID 和 googleClientSecret。您可以通过将您的应用程序注册到 google 开发者网站
来获得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);
});
});
}
Dependencies = "passport": "^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;