我正在嘗試使用護照在KOA2中進行社交登錄。當我嘗試在passport.authenticate()的幫助下對用戶進行身份驗證時。它應該被執行,並應該把我扔到fb登錄頁面,但是不是這樣做的。即使它顯示Not Found,也不會引發任何類型的錯誤。這裏是我的代碼示例:passport.authenticate根本不工作
app.js
import Koa from 'koa'
import views from 'koa-views'
import serve from 'koa-static'
import rootRoutes from './routes/index'
import userRoutes from './routes/user'
import authConfig from './config/oauth'
import authRoutes from './routes/social'
const app = new Koa()
// trust proxy
app.proxy = true
// sessions
const convert = require('koa-convert')
const session = require('koa-generic-session')
const passport = require('koa-passport')
app.keys = ['your-session-secret']
// body parser
const bodyParser = require('koa-bodyparser')
// require('./auth')
app.use(bodyParser())
app.use(convert(session()))
// authentication
app.use(convert(passport.initialize()))
app.use(convert(passport.session()))
app.use(views(`${__dirname}/views`, { extension: 'html' }))
app.use(serve(`${__dirname}/public`))
app.use(authRoutes.routes())
app.use(authRoutes.allowedMethods())
app.use(rootRoutes.routes())
console.log("authRoutes", authRoutes);
app.use(rootRoutes.allowedMethods())
app.use(userRoutes.routes())
app.use(userRoutes.allowedMethods())
app.use(async (ctx, next) => {
try {
await next(); // next is now a function, await instead of yield
} catch (err) {
ctx.body = { message: err.message };
ctx.status = err.status || 500;
}
});
app.listen(1337,() => {
console.log('Server running at http://localhost:1337 and http://127.0.0.1:1337')
})
export default app
social.js
import Router from 'koa-router'
const router = new Router()
import authConfig from '../config/oauth'
import passport from 'koa-passport'
var user = { id: 1, username: 'test' }
passport.serializeUser((user, done) => {
console.log(user)
done(null, user);
});
passport.deserializeUser((id, done) => {
(async() => {
try {
// const user = await User.findById(id);
console.log("user", user);
done(null, user);
} catch (error) {
console.log("error")
done(error);
}
})();
});
var LocalStrategy = require('passport-local').Strategy
passport.use(new LocalStrategy(function(username, password, done) {
console.log("user", user);
// retrieve user ...
if (username === 'test' && password === 'test') {
done(null, user)
} else {
done(null, false)
}
}))
var FacebookStrategy = require('passport-facebook').Strategy
passport.use(new FacebookStrategy({
clientID: '214873665615110',
clientSecret: 'f30ba1c22c6fef150c4b8ffae3cbffe4',
callbackURL: 'http://127.0.0.1:1337/auth/facebook/callback'
},
function(token, tokenSecret, profile, done) {
console.log("profile", profile);
// retrieve user ...
done(null, user)
}
))
var TwitterStrategy = require('passport-twitter').Strategy
passport.use(new TwitterStrategy({
consumerKey: 'authConfig.twitter.consumerKey',
consumerSecret: 'authConfig.twitter.consumerSecret',
callbackURL: 'http://127.0.0.1:' + (process.env.PORT || 1337) + '/auth/twitter/callback'
},
function(token, tokenSecret, profile, done) {
// retrieve user ...
done(null, user)
}
))
var GoogleStrategy = require('passport-google-auth').Strategy
passport.use(new GoogleStrategy({
clientId: 'authConfig.google.clientID',
clientSecret: 'authConfig.google.clientSecret',
callbackURL: 'http://127.0.0.1:' + (process.env.PORT || 1337) + '/auth/google/callback'
},
function(token, tokenSecret, profile, done) {
// retrieve user ...
done(null, user)
}
))
// import authConfig from '../config/oauth'
router.post('/login', async(ctx, next) => {
console.log("login");
await passport.authenticate('local', {
successRedirect: '/about',
failureRedirect: '/'
})
return next();
})
router.get('/auth/facebook', async(ctx, next) => {
console.log("call aayi");
await passport.authenticate('facebook');
// ctx.body = "aa gaya bhai ";
})
router.get('/auth/facebook/callback', async(ctx, next) => {
passport.authenticate('facebook', {
successRedirect: '/about',
failureRedirect: '/'
})
})
export default router
oath.js
是具有社交插件的憑據。
有趣的問題是,當我使用koa-route
而不是koa-router
。它正在工作。其餘同
const route = require('koa-route');
app.use(route.get('/auth/facebook',
passport.authenticate('facebook')
))
app.use(route.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect: '/app',
failureRedirect: '/'
})
))
一切,只是用koa-route
這兩個路線和它的正常工作。
它仍然是一樣的。我已經更新了這個問題。 –
順便說一句同樣的工作,那麼這怎麼可能在koa中產生任何問題?事情是,我們應該存儲最少的數據是會話,這就是爲什麼我們使用user.id –