Currently, I'm trying to use my company SSO(Oauth 2.0) Service,and I pass the url to the oauth service and
then I get the url with a code to get access token from front side, and then I pass the fetch
the code from client side url to backend server with the post, and then I get the code in server
side and redirect to the other url param in server side, i can get the user's information to client and server.
However, the client side URL show the code that i sent , so I want to know how to get rid of it.
and I searched a solution that i can redirect the page to another page, but I do not know how to .
and I want to know what i was doing is right way .
Thank you in advance.i hope it's not bad explanation
this below is what i tried :
client Side
var client_id =
"client_random_id";
var state_val = "RANDOM_STATE";
var redirectURI = "http://localhost:3000";
let api_url =
"https://www.??????/oauth2.0/authorize?response_type=code&client_id="
client_id
"&redirect_uri="
redirectURI
"&state="
state_val;
const logout = `https://www.??????/oauth2.0/Logout?client_id=${client_id}&logout_redirect_uri=${redirectURI}`;
const queryParams = new URLSearchParams(window.location.search);
const [userinfo, setuserinfo] = useState();
useEffect(() => {
fetch("http://localhost:5000/create", {
method: "post",
body: queryParams,
})
.then(response => response.json())
.then(json => {
setuserinfo(json);
})
.then("<Redirect to={routes.home.path}/>")
.catch(ex => {});
});
Server Side
app.post("/create", function (req, res) {
const code = req.body.code;
res.redirect(`/callback/?code=${code}`);
});
app.get("/callback", cors(), (req, res) => {
if (req.query.code !== null && req.query.code !== undefined) {
var token_url = "https://www.?????.kr/oauth2.0/token";
var options = {
url: token_url,
method: "POST",
form: {
grant_type: "authorization_code",
client_id: client_id,
client_secret: client_secret,
redirect_uri: redirectURI,
code: req.query.code,
state: req.query.state,
},
};
request(options, function (error1, response1, body1) {
if (!error1 && response1.statusCode == 200) {
var tdata = JSON.parse(body1);
var options2 = {
url: "https://www.??????/oauth2.0/resource",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " tdata.access_token,
},
};
request(options2, function (error2, response2, body2) {
if (!error2 && response2.statusCode == 200) {
var rdata = JSON.parse(body2);
res.writeHead(200, { "Content-Type": "text/json;charset=utf-8" });
res.end(body2);
} else {
res.status(response2.statusCode).end();
console.log("error2 = " response2.statusCode);
}
});
} else {
res.status(response1.statusCode).end();
console.log("error1 = " response1.statusCode);
}
});
}
});
CodePudding user response:
All you need is to change this part .then("<Redirect to={routes.home.path}/>")
to something like this
.then(() => {
window.location.replace("your url")
})
you could read more about replace
here