登录用户访问受保护资源时遇到问题(OAuth2 Google passport.js)

我可以登录,然后被重定向到受保护的页面(在前端).

但是当页面呈现时,我向api中的一个名为/login/success的路由发送一个请求,在那里我发送一些用户详细信息作为响应,但在此之前,我判断req.isAuthenticated是否存在,显然它存在于google登录后回调url的路由中,(callback - ive named it {AUTHOND_BASE_URL}/authorize)重定向(req.user和req.isAuthenticated),但它似乎不存在于任何其他路由中,我现在很困惑,我是一个菜鸟,所以我不知道我是否应该运行Passport .在所有受保护的路由逻辑之前验证中间件,或者只做一次-

https://github.com/DAObliterator/oauth2_googlesigin...以下是该项目的链接.


/*{CLIENT_URL}/authenticate -> (NOTE - via an anchor tag does not  work when i send a get request using axios when click event is fired , throws a NO ACCESS CONTROL ORIGIN on the requested header error at the client side , had to sit a whole day to figure out that anchor tags has to be used to prevent CORS error )*/

//{BACKEND_URL}/login ->

app.get(
"/login",
passport.authenticate("google", { scope: ["email", "profile"] })
);

//Auth Server (google) ->

//{BACKEND_URL}/authorize->

app.get(
"/authorize",
passport.authenticate("google", {
successRedirect: "/protected",
failureRedirect: "/auth/failure",
})
);

//onSuccess

//{BACKEND_URL}/protected->

app.get("/protected", async (req, res) => {
console.log("user-details-2 ", req.user);
if (req.user) {
return res.redirect(\${process.env.CLIENT_URL}protectedPage`);`
} else {
return res.redirect(\${process.env.CLIENT_URL}authenticate`);`
}
});

//{CLIENT_URL}/protectedPage

import React , { useState , useEffect } from 'react';
import axios from "axios";

export const ProtectedPage = () => {

const [text , setText ] = useState("");
const [access, setAccess] = useState(false);

useEffect(() => {
axios.get("http://localhost:6046/login/success").then((response) => {
console.log(response.data , "response data from /protected \n");
setAccess(true);
setText(response.data.user.displayName);
}).catch((error) => {
console.log(error , ' error happened while trying to access protectedResource endpoint')
})
},[])

const handleLogout = () => {
axios.get("http://localhost:6046/logout")
}

return (
<div id="Main" className="w-screen h-screen flex flex-col">
Hello to ProtectedPage <br /> you are{" "}
{access ? text + " and is permitted " : "not-permitted"} to view the protected Resource
{access && (
<button
id="Logout-btn"
className="bg-blue-400 h-16 w-40"
onClick={handleLogout}
>
logout
</button>
)}
</div>
);
}

//{BACKEND_URL}/login/success

app.get("/login/success", (req, res) => {
if (req.isAuthenticated()) {
// If the user is authenticated, respond with user information
res.status(200).json({
error: false,
message: "Successfully Logged In",
user: req.user,
});
} else {
// If the user is not authenticated, respond with an error
res.status(403).json({ error: true, message: "Not Authorized" });
}
});

在客户端抛出403错误

推荐答案

因此,在发送请求时,我没有将with Credentials设置为True,因为请求中没有包含哪些Cookie

Javascript相关问答推荐

Express.js:使用Passport.js实现基于角色的身份验证时出现太多重定向问题

警告!合同执行期间遇到错误[执行已恢复](Base Layer 2)

我开始使用/url?q=使用Cheerio

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

将自定义排序应用于角形数字数组

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

WebGL 2.0无符号整数输入变量

Websocket错误—有一个或多个保留位开启:reserved1 = 1,reserved2 = 0,reserved3 = 0

当使用';字母而不是与';var#39;一起使用时,访问窗口为什么返回未定义的?

在css中放置所需 colored颜色 以填充图像的透明区域

Jest toHaveBeenNthCalledWith返回当前设置的变量值,而不是调用时的值

向数组中的对象添加键而不改变原始变量

JS Animate()方法未按预期工作

无法重定向到Next.js中的动态URL

判断函数参数的类型

MongoDB中的嵌套搜索

Reaction即使在重新呈现后也会在方法内部保留局部值

如何使用Reaction路由导航测试挂钩?

如何动态呈现适合未知屏幕大小的最大数量的表行?苗条的

如何让noWrap在嵌套在Alert/AlertTitle组件中的排版组件中工作?