fix auth wip
Some checks failed
Build Docker Image Front / run (pull_request) Failing after 59s
Build Docker Image Back / run (pull_request) Successful in 23s
JsDocs / coverage (pull_request) Successful in 24s
Test and coverage / coverage (pull_request) Successful in 1m23s

This commit is contained in:
Clement 2024-05-20 14:24:27 +02:00
parent d65335683c
commit 6e43f12a4f
2 changed files with 14475 additions and 42 deletions

14468
front/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,14 +1,10 @@
---
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(redirectUrl)
console.log(Astro.request.headers.get('cookie'))
//TODO socké dans les cookies
// load the previously stored provider's data
@ -31,12 +27,11 @@ if (provider.state !== params.get('state')) {
throw "State parameters don't match.";
}
pb.collection('users').authWithOAuth2Code(
await pb.collection('users').authWithOAuth2Code(
provider.name,
code,
provider.codeVerifier,
redirectUrl,
// pass optional user create data
{
emailVisibility: false,
}
@ -44,11 +39,15 @@ pb.collection('users').authWithOAuth2Code(
//REDIRECT
console.log("oauth OK !!");
console.log(JSON.stringify(authData, null, 2));
console.log(pb.authStore.isValid);
console.log(pb.authStore.isValid);
return Astro.redirect("/account")
}).catch((err) => {
console.log("oauth fail !!");
console.log(err);
});
---
@ -59,39 +58,5 @@ pb.collection('users').authWithOAuth2Code(
<title>OAuth2 redirect page</title>
</head>
<body>
<pre id="content">Authenticating...</pre>
<script src="https://cdn.jsdelivr.net/gh/pocketbase/js-sdk@master/dist/pocketbase.umd.js"></script>
<script type="text/javascript">
const pb = new PocketBase("http://127.0.0.1:8090");
const redirectUrl = 'http://127.0.0.1:8090/redirect.html';
// parse the query parameters from the redirected url
const params = (new URL(window.location)).searchParams;
// load the previously stored provider's data
const provider = JSON.parse(localStorage.getItem('provider'))
// compare the redirect's state param and the stored provider's one
if (provider.state !== params.get('state')) {
throw "State parameters don't match.";
}
// authenticate
pb.collection('users').authWithOAuth2Code(
provider.name,
params.get('code'),
provider.codeVerifier,
redirectUrl,
// pass optional user create data
{
emailVisibility: false,
}
).then((authData) => {
document.getElementById('content').innerText = JSON.stringify(authData, null, 2);
}).catch((err) => {
document.getElementById('content').innerText = "Failed to exchange code.\n" + err;
});
</script>
</body>
</html>