Dan6erbond / sk-auth

Authentication library for use with SvelteKit featuring built-in OAuth providers and zero restriction customization!
MIT License
578 stars 70 forks source link

Prod builds with node adapter fail on google auth due to missing fetch #51

Open dcaslin opened 2 years ago

dcaslin commented 2 years ago

Looks like the oauth2.ts is using native fetch:

const res = await fetch(this.config.accessTokenUrl!, {

From what I've read, this won't work in SSR Sveltekit code, where we'd need to explicitly use node-fetch or pass through the sveltekit supplied "fetch". FWIW this is in sveltekit 1.0.0-next.165

Generated default settings
ReferenceError: fetch is not defined
    at GoogleOAuth2Provider.getTokens (C:\projects\531-calc\node_modules\sk-auth\dist\providers\oauth2.js:49:17)
    at GoogleOAuth2Provider.callback (C:\projects\531-calc\node_modules\sk-auth\dist\providers\oauth2.base.js:37:31)
    at Auth.handleProviderCallback (C:\projects\531-calc\node_modules\sk-auth\dist\auth.js:131:51)
    at Auth.handleEndpoint (C:\projects\531-calc\node_modules\sk-auth\dist\auth.js:180:29)
    at Auth.get (C:\projects\531-calc\node_modules\sk-auth\dist\auth.js:49:25)
    at render_endpoint (file:///C:/projects/531-calc/build/middlewares.js:1098:26)
    at async resolve (file:///C:/projects/531-calc/build/middlewares.js:2243:56)
    at async Object.handle (file:///C:/projects/531-calc/build/middlewares.js:2498:20)
    at async respond (file:///C:/projects/531-calc/build/middlewares.js:2227:12)
    at async Array.<anonymous> (file:///C:/projects/531-calc/build/middlewares.js:4322:22)
heshdotcc commented 2 years ago

The same applies to SvelteKit v1.0.0-next.195... Here I'm not explicitly using fetch, it seems to be done by the library itself.

heshdotcc commented 2 years ago

The problem also appears when trying to log in with username/email and password...

heshdotcc commented 2 years ago

Tried explicitly using both node-fetch and cross-fetch within lib itself w/o success...

heshdotcc commented 2 years ago

Also for the use case of localhost it works perfectly...

But adding host parameter to SvelteKitAuth constructor gives fetch is not defined.

And if you don't setup host parameter your production build may not set an HTTPS redirection URI.