manfredsteyer / angular-oauth2-oidc

Support for OAuth 2 and OpenId Connect (OIDC) in Angular.
MIT License
1.89k stars 688 forks source link

ERROR ReferenceError: window is not defined - Support For SSR / prerender #1408

Open ev45ive opened 5 months ago

ev45ive commented 5 months ago

Angular 17 enabled SSR and prerender by default and this library uses window, not globalThis and doesn't detect if in SSR/Edge so programmer has to those checks and wrap things with isPlatformBrowser() checks.

I am not sure if whole server side flow is planed in scope of this library, but making tryLogin / initLoginFlow a NO-OP in SSR? maybe?

ev45ive commented 3 months ago

Temporary Fix:

 oauth = inject(OAuthService)
 pid =inject(PLATFORM_ID)

  ngOnInit(): void {
    if(isPlatformBrowser(this.pid))
    this.oauth.tryLogin()
  }

export const authConfig: AuthConfig = {

  // URL of the SPA to redirect the user to after loginemv
  redirectUri: globalThis.location?.origin + '/callback',
mmoya26 commented 1 month ago

@ev45ive Hey just came across this issue, I'm getting console errors saying the window is not defined whenever I do ng serve. is there a chance you can elaborate on this?

test

I tested the whole login flow and it is working. I'm looking at your temporary fix and I'm not sure where I'm supposed to place the first snippet of code you posted. I already adjusted the authConfig to have it look like yours. Again, I'm just not sure where in my code I need to place the first part.