I'm working on google oauth2 request code. I need to click on one button application and open a new window to start google oauth2 flow. I have a redirect uri setted as parameter. I write this function but I have this error into setInterval function:
Blocked a frame with origin from accessing a cross-origin frame
this is the function:
oauth2SignIn() {
// Google's OAuth 2.0 endpoint for requesting an access token
const oauth2Endpoint = 'https://accounts.google.com/o/oauth2/v2/auth';
// Create element to open OAuth 2.0 endpoint in new window.
let form = document.createElement('form');
form.setAttribute('method', 'GET'); // Send as a GET request.
form.setAttribute('target', 'previewWindow');
form.setAttribute('id', 'googleFormAuth');
form.setAttribute('action', oauth2Endpoint);
// Parameters to pass to OAuth 2.0 endpoint.
const params = {
client_id: environment.GAPI_CLIENT_ID,
redirect_uri: 'https://my-app/services/api/api/google/calendar/callback',
scope: 'https://www.googleapis.com/auth/calendar.readonly',
state: 'try_sample_request',
response_type: 'code',
access_type: 'offline',
prompt: 'consent',
};
// Add form parameters as hidden input values.
for (let p in params) {
let input = document.createElement('input');
input.setAttribute('type', 'hidden');
input.setAttribute('name', p);
input.setAttribute('value', params[p]);
form.appendChild(input);
}
// Add form to page and submit it to open the OAuth 2.0 endpoint.
document.documentElement.appendChild(form);
let newWin = window.open('', 'previewWindow', 'popup');
form.submit();
setInterval(()=> {
if(newWin.location.href.includes('my-app') {
newWin.close();
}
})
}
My focus is to close the newWin when url changed. I have also to read the document because sometimes I have an error printed in the dom, like 401 error by the server. Is it possible to solve this problem?