@@ -77,31 +77,61 @@ export default class UserStore {
7777 * Documentation Flow: Initiates the redirect to the backend auth endpoint.
7878 * We use a popup window to handle the redirect cycle so the user doesn't leave the current page.
7979 */
80+ private popupWindow : Window | null = null ;
81+
8082 login ( ) {
8183 const width = 500 ;
82- const height = 600 ;
84+ const height = 650 ;
8385 const left = window . screenX + ( window . outerWidth - width ) / 2 ;
8486 const top = window . screenY + ( window . outerHeight - height ) / 2 ;
87+ const name = 'Google Authentication' ;
88+ const strWindowFeatures = `toolbar=no, menubar=no, width=${ width } , height=${ height } , top=${ top } , left=${ left } , status=no, resizable=yes, scrollbars=yes` ;
8589
86- // Use the current origin as the redirect URL for the backend to send the user back to
87- const redirectUrl = window . location . origin + '/close -popup.html ' ;
90+ // Use the specific trusted route name from the Anyway app config
91+ const redirectUrl = window . location . origin + '/login -popup-redirect ' ;
8892 const authUrl = this . authService . getAuthorizeUrl ( redirectUrl ) ;
8993
90- const popup = window . open (
91- authUrl ,
92- 'googleLogin' ,
93- `width=${ width } ,height=${ height } ,left=${ left } ,top=${ top } ,status=no,resizable=yes,toolbar=no,menubar=no,scrollbars=yes`
94- ) ;
94+ // Remove any existing event listeners before adding a new one
95+ window . removeEventListener ( 'message' , this . handleAuthMessage ) ;
96+ window . addEventListener ( 'message' , this . handleAuthMessage ) ;
97+
98+ if ( this . popupWindow === null || this . popupWindow . closed ) {
99+ this . popupWindow = window . open ( authUrl , name , strWindowFeatures ) ;
100+ } else {
101+ this . popupWindow . focus ( ) ;
102+ if ( this . popupWindow . location . origin !== window . location . origin ) {
103+ this . popupWindow . location . href = authUrl ;
104+ }
105+ }
95106
96- // Check if popup is closed and then refresh auth status
107+ // Fallback: Check if popup was closed manually
97108 const checkPopup = setInterval ( ( ) => {
98- if ( ! popup || popup . closed ) {
109+ if ( ! this . popupWindow || this . popupWindow . closed ) {
99110 clearInterval ( checkPopup ) ;
100111 this . checkAuthStatus ( ) ;
101112 }
102113 } , 1000 ) ;
103114 }
104115
116+ private handleAuthMessage = ( event : MessageEvent ) => {
117+ // Verify the origin for security as per provided Anyway app code
118+ if ( event . origin !== window . location . origin ) {
119+ console . warn ( 'Authentication redirect origin is not valid' ) ;
120+ return ;
121+ }
122+
123+ if ( event . data === 'login-success' ) {
124+ console . info ( 'authentication success!, redirect to main page...' ) ;
125+ if ( this . popupWindow ) {
126+ this . popupWindow . close ( ) ;
127+ }
128+ window . removeEventListener ( 'message' , this . handleAuthMessage ) ;
129+
130+ // Refresh the app to ensure all stores and cookies are in sync
131+ window . location . pathname = '/' ;
132+ }
133+ } ;
134+
105135 async logout ( ) {
106136 try {
107137 await this . authService . logout ( ) ;
0 commit comments