diff --git a/front/src/middleware/index.ts b/front/src/middleware/index.ts index 89e6322..03f8253 100644 --- a/front/src/middleware/index.ts +++ b/front/src/middleware/index.ts @@ -3,11 +3,12 @@ import PocketBase from 'pocketbase' import { defineMiddleware } from 'astro/middleware' import { getEnv } from 'libs/Env' -export const onRequest = defineMiddleware(async ({ locals, request }: any, next: () => any) => { +export const onRequest = defineMiddleware(async ({ locals, cookies}, next) => { locals.pb = new PocketBase(getEnv('POCKETBASE_URL','http://localhost:8080')) // load the store data from the request cookie string - locals.pb.authStore.loadFromCookie(request.headers.get('cookie') || '') + const pbcookie = cookies.get('session')?.value + locals.pb.authStore.loadFromCookie('pb_auth=' + pbcookie || '') try { // get an up-to-date auth store state by verifying and refreshing the loaded auth model (if any) @@ -20,8 +21,19 @@ export const onRequest = defineMiddleware(async ({ locals, request }: any, next: const response = await next() - // send back the default 'pb_auth' cookie to the client with the latest store state - response.headers.append('set-cookie', locals.pb.authStore.exportToCookie()) + let secure = true + if (getEnv('NODE_ENV', 'production') !== 'production') { + secure = false + } + const pbcookieStr = locals.pb.authStore.exportToCookie() + + cookies.set('session',pbcookieStr.slice(pbcookieStr.indexOf('=')+1,pbcookieStr.indexOf(';')),{ + httpOnly: true, + path: '/', + secure: secure, + sameSite: 'lax', + maxAge: 365000 + }) return response }) diff --git a/front/src/pages/account/oauth.astro b/front/src/pages/account/oauth.astro new file mode 100644 index 0000000..bd0df8d --- /dev/null +++ b/front/src/pages/account/oauth.astro @@ -0,0 +1,97 @@ +--- + +const pb = Astro.locals.pb +const redirectUrl = Astro.url.protocol + "//" + Astro.url.host + '/account/oauth'; + +console.log(redirectUrl) + +const params = Astro.url.searchParams + +const code = params.get('code') + +console.log(Astro.request.headers.get('cookie')) +//TODO socké dans les cookies +// load the previously stored provider's data +const providerstr = Astro.cookies.get('provider') + +if (!providerstr) { + console.error("Fail to load provider") + console.log(providerstr) + return +} +const provider = providerstr.json() + +if (!code) { + console.error("Fail to load code params"); + return +} + +// compare the redirect's state param and the stored provider's one +if (provider.state !== params.get('state')) { + throw "State parameters don't match."; +} + +pb.collection('users').authWithOAuth2Code( + provider.name, + code, + provider.codeVerifier, + redirectUrl, + // pass optional user create data + { + emailVisibility: false, + } +).then((authData) => { + //REDIRECT + console.log("oauth OK !!"); + console.log(JSON.stringify(authData, null, 2)); +}).catch((err) => { + console.log("oauth fail !!"); + console.log(err); +}); + +--- + + + + +
+ +Authenticating...+ + + + + \ No newline at end of file diff --git a/front/src/pages/account/register.astro b/front/src/pages/account/register.astro index 70d2ebb..2c57d4f 100644 --- a/front/src/pages/account/register.astro +++ b/front/src/pages/account/register.astro @@ -1,11 +1,28 @@ --- import Layout from 'layouts/Layout.astro'; import AstroUtils from 'libs/AstroUtils'; +import { getEnv } from 'libs/Env'; const pb = Astro.locals.pb const oauths = await pb.collection('users').listAuthMethods(); + +console.log(JSON.stringify(oauths.authProviders[0])); + +let secure = true +if (getEnv('NODE_ENV', 'production') !== 'production') { + secure = false +} + +Astro.cookies.set('provider', oauths.authProviders[0],{ + httpOnly: true, + path: '/', + secure: secure, + sameSite: 'lax', + maxAge: 365000 + }) + if(pb.authStore.isValid){ return Astro.redirect("/account") } @@ -55,21 +72,6 @@ await AstroUtils.wrap(async () => { - discord? + discord? - - \ No newline at end of file + \ No newline at end of file