我们如何在受保护的路由中进行身份验证登录。
例如,我有一个受保护的地方,允许用户登录后连接他们的社交媒体资料。
通常使用href
事件(即<a href="base_url/auth/facebook"> Cick here to connect facebook </a>
现在,如果要在用户登录后连接用户,我不确定如何使用href
设置cookie(如果这是理想的方法)。
这是我的中间件的样子
async verifyMiddleWare(req, res, next) {
const token = req.cookies
if (token) {
try {
const userToken = token.userToken
const tokenVerficiation = await verifyToken(userToken)
res.locals.userId = tokenVerficiation.userId
res.locals.acessLevel = tokenVerficiation.acessLevel
if (tokenVerficiation.acessLevel === this.routePermission) next()
else
res
.status(AUTH_ERRORS.INVALID_ACCESS_TOKEN.status)
.send(AUTH_ERRORS.INVALID_ACCESS_TOKEN.message)
} catch (error) {
return res
.status(AUTH_ERRORS.UNAUTHORIZED_TO_VIEW.status)
.send(AUTH_ERRORS.UNAUTHORIZED_TO_VIEW.message)
}
} else {
return res
.status(AUTH_ERRORS.MISSING_ACCESS_TOKEN.status)
.send(AUTH_ERRORS.MISSING_ACCESS_TOKEN.message)
}
}
和护照代码(我不是序列化和反序列化用户)
const passport = require('passport')
const FacebookStrategy = require('passport-facebook').Strategy
const config = require('./../../config')
passport.use(
new FacebookStrategy(
{
clientID: config.FACEBOOK_APP_ID,
clientSecret: config.FACEBOOK_APP_SECRET,
callbackURL: config.FACEBOOK_REDIRECT_URL,
profileFields: ['id', 'displayName', 'email', 'gender']
},
async (accessToken, refreshToken, profile, done) => {
return done(null, { accessToken, refreshToken, profile })
}
)
)
我正在身份验证期间设置Cookie
app.get(`/${serviceName}/callback`, passport.authorize(serviceName), async (req, res) => {
const facebookDetials = req.account
// the output from passport should mandatorily have email
const userEmail = facebookDetials.profile.emails[0].value
// Checking if user data exsists, corresponding to email id entered
try {
const userData = await getSelectedThingFromTable(
SIGNUP_TABLES.userAuth,
`email = "${userEmail}"`
)
if (userData.length > 0) {
// Means email Exist
const { userId, acessLevel } = userData[0] //
const createNewAccessToken = await JWT.generateToken({ userId, acessLevel })
//setting cookies
res.cookie('userToken', createNewAccessToken)
return res.redirect(config.BASE_CONFIG_URL)
(localhost:8000/auth/facebook)
中登录用户,然后转到localhost:8000/social/twitter
->这时我的令牌即将变得不确定答案 0 :(得分:0)
我很容易错了,但是我猜想cookie似乎没有被设置,因为它与错误的域名相关联。
尝试更改
indexOf
到
addCourses() {
const currentCourses = this.packageForm.package_courses.map((item) => item.course_id);
const courses = this.courses.filter((item) => {
return this.selectedCourses.indexOf(item.id) && currentCourses.indexOf(item.id) >= 0
});
courses.forEach((course) => {
this.packageForm.package_courses.push({
course_id: course.id,
course: course,
price: 0
});
});
this.selectedCourses = [];
},