I am using AWS Cognito to login using Google in my React project. Before I just was saving the user information in localStorage when signed In using email and password.
I got the following function for Google login button in react:
const signUpWithGoogle = () => {
const cognitoDomain = "cognito domain"; // Example: 'myapp.auth.us-west-2.amazoncognito.com'
const responseType = "code"; // or 'code' if you've setup code grant
const clientId = "CLIENT_ID";
const redirectUri = encodeURIComponent("http://localhost:3000/"); // Example: 'https://www.myapp.com/callback'
// const redirectUri = encodeURIComponent("REDIRECT_URL"); // Example: 'https://www.myapp.com/callback'
const identityProvider = "Google";
const signUpUrl = `https://cognito_domain/oauth2/authorize?identity_provider=${identityProvider}&response_type=${responseType}&client_id=${clientId}&redirect_uri=${redirectUri}&`;
window.location.href = signUpUrl;
};
After the pressed then I am redirected to the specified page with a code as a url parameter. Then I get The code and login via this code:
if (code) {
try {
const poolData = {
UserPoolId: "UserPoolId",
ClientId: "ClientId",
};
const userPool = new CognitoUserPool(poolData);
const url = `https://oddsview.auth.us-west-1.amazoncognito.com/oauth2/token?grant_type=authorization_code&client_id=ClientId&code=${code}&redirect_uri=http://localhost:3000/`;
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
})
.then((response) => response.json())
.then((data) => {
const idToken = data.id_token;
try {
const decodedToken = jwtDecode(idToken);
console.log(idToken);
console.log(decodedToken.sub);
//Do more stuff with it.
} catch (err) {}
// console.log(decodedToken);
});
} catch (error) {
console.log( error);
}
}
Now the problem I am facing is I cannot get any solution how to logout the user. I see in my localhost cookie some sessions like cognitoand other cookies, when I delete the cookies manually then after clicking the button I get the select account google page.
But otherwise when I click the google login button I do not get the Google select account page rather it returns to my redirect url as the user logged in.
I want to logout the google or anyother method so that at least if I click the login button it gives me the select account page.