英文:
How to know when a user is signed in
问题
我正在尝试使用Node.js和SQL实现一个购物车服务。我有两个购物车表,一个用于访客(guestCart),另一个用于用户登录后(memberCart)。实现如下:
if(用户未登录){
将商品添加到guestCart
}else{
将商品添加到memberCart
}
我正在使用passport.js进行身份验证。我的问题是:如何检查用户是否已登录?当用户已登录时,什么条件为TRUE?
英文:
I am trying to implement a Cart service using Node.js and SQL. I have two carts tables, one for guests (guestCart) and the other when a user is signed in (memberCart). The implementation looks like this:
if(user not signed in){
Add item to guestCart
}else{
Add item to memberCart
}
I am using passport.js for authentication. My question is: How do I check that a user has been signed in? When a user is signed in, what condition is TRUE?
答案1
得分: 1
你可以通过检查req.user来确定用户是否已登录。如果用户已登录,req.user应该是一个包含用户详细信息的对象。如果用户未登录,req.user可能为未定义。
if(req.user){
// 用户已登录,添加到会员购物车
}else{
// 未登录用户,添加到访客购物车
}
英文:
You can determine whether a user is logged in by checking req.user. If the user is logged in, req.user should be an object containing detailed information about the user. If the user is not logged in, req.user could be undefined.
if(req.user){
// user login add to memberCart
}else{
// don't login add to guestCart
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论