Skip to content

Commit

Permalink
fix: don't logout when connecting new login provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
zicklag committed Jan 18, 2025
1 parent 8773d4f commit a27297b
Showing 1 changed file with 7 additions and 30 deletions.
37 changes: 7 additions & 30 deletions src/routes/(app)/[username]/components/ManageAccountModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import type { UserInfo } from '$lib/rauthy';
import type { Provider } from '$lib/rauthy/client';
import getPkce from 'oauth-pkce';
import { checkResponse } from '$lib/utils/http';
const modalStore = getModalStore();
Expand All @@ -18,20 +19,6 @@
providers.find((x) => x.id == userInfo?.auth_provider_id)?.name
);
const getKey = (i: number) => {
let res = '';
const target = i || 8;
for (let i = 0; i < target; i += 1) {
let nextNumber = 60;
while ((nextNumber > 57 && nextNumber < 65) || (nextNumber > 90 && nextNumber < 97)) {
nextNumber = Math.floor(Math.random() * 74) + 48;
}
res = res.concat(String.fromCharCode(nextNumber));
}
return res;
};
async function providerLinkPkce(provider_id: string, pkce_challenge: string) {
const data = {
pkce_challenge,
Expand All @@ -40,25 +27,15 @@
email: userInfo?.email,
provider_id
};
await fetch(`/auth/v1/providers/${provider_id}/link`, {
const resp = await fetch(`/auth/v1/providers/${provider_id}/link`, {
method: 'POST',
headers: [['csrf-token', localStorage.getItem('csrfToken')!]],
body: JSON.stringify(data)
})
.then(() => {
getPkce(64, async (error, { challenge, verifier }) => {
if (!error) {
localStorage.setItem('pkce_verifier', verifier);
const nonce = getKey(24);
const s = 'account';
const redirect_uri = encodeURIComponent(
`${window.location.origin}/auth/v1/oidc/callback`
);
window.location.href = `/auth/v1/oidc/logout?post_logout_redirect_uri=%2Fauth%2Fv1%2Foidc%2Fauthorize%3Fclient_id%3Drauthy%26redirect_uri%3D${redirect_uri}%26response_type%3Dcode%26code_challenge%3D${challenge}%26code_challenge_method%3DS256%26scope%3Dopenid%2Bprofile%2Bemail%26nonce%3D${nonce}%26state%3D${s}`;
}
});
})
.catch((err) => console.log(err, 'a'));
});
await checkResponse(resp);
const csrfToken = await resp.text();
localStorage.setItem('csrfToken', csrfToken);
window.location.href = resp.headers.get('location')!;
}
async function linkAccount(providerId: string) {
getPkce(64, (error, { challenge, verifier }) => {
Expand Down

0 comments on commit a27297b

Please sign in to comment.