Skip to content

Commit

Permalink
Merge to main (#343)
Browse files Browse the repository at this point in the history
* Fix for SSO and Keycloak redirects

* Fix for Keycloak

* Fix for redirect on logout

* Redirect bug

* redirect issue

* Redirect issue

* Redirect issue

* Redirect problem

* redirect

* Logging

* Back to implicit

* Remove implicit flow

* Another hack

* Again with NAVSA

* Keycloak fix

* Keycloak fix

* Keycloak fix

* Keycloak fix

* Keycloak fix

* remove logging

* Updated to bugfixed @recogito/text + pdf annotator versions

---------

Co-authored-by: Rainer Simon <[email protected]>
Co-authored-by: Rainer Simon <[email protected]>
  • Loading branch information
3 people authored Dec 19, 2024
1 parent 6bcc42f commit 63bd329
Show file tree
Hide file tree
Showing 15 changed files with 1,778 additions and 1,879 deletions.
3,507 changes: 1,664 additions & 1,843 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@
"@radix-ui/react-visually-hidden": "^1.1.0",
"@react-spring/web": "^9.7.5",
"@recogito/annotorious-supabase": "^1.1.0",
"@recogito/pdf-annotator": "1.0.0-rc.40",
"@recogito/react-pdf-annotator": "1.0.0-rc.40",
"@recogito/react-text-annotator": "3.0.0-rc.53",
"@recogito/pdf-annotator": "1.0.0-rc.41",
"@recogito/react-pdf-annotator": "1.0.0-rc.41",
"@recogito/react-text-annotator": "3.0.0-rc.54",
"@supabase/ssr": "0.5.2",
"@supabase/supabase-js": "2.46.1",
"@table-library/react-table-library": "^4.1.7",
Expand Down
44 changes: 44 additions & 0 deletions src/apps/auth-keycloak/AuthKeycloak.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { useEffect } from 'react';
import type { Translations } from 'src/Types';
import { supabaseImplicit } from '@backend/supabaseBrowserClient';

interface AuthKeycloakProps {
i18n: Translations;
}
export const AuthKeycloak = (props: AuthKeycloakProps) => {
const { t } = props.i18n;

const url = new URLSearchParams(window.location.search);
let redirectUrl = url.get('redirect-to');
if (redirectUrl) {
localStorage.setItem('redirect-to', redirectUrl);
} else {
redirectUrl = localStorage.getItem('redirect-to');
if (redirectUrl && redirectUrl.length === 0) {
redirectUrl = null;
}
}

useEffect(() => {
supabaseImplicit.auth
.signInWithOAuth({
provider: 'keycloak',
options: {
scopes: 'openid',
redirectTo: redirectUrl
? redirectUrl
: `/${props.i18n.lang}/projects`,
},
})
.then(({ data, error }) => {
if (data?.url) {
localStorage.removeItem('redirect-to');
window.location.href = data.url;
} else {
console.error(error);
}
});
}, []);

return <div className='keycloak-main'>{t['Redirecting']}</div>;
};
Empty file added src/apps/auth-keycloak/index.ts
Empty file.
22 changes: 3 additions & 19 deletions src/apps/auth-login/Login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const clearCookies = () => {

export const Login = (props: {
i18n: Translations;
logo: boolean;
methods: LoginMethod[];
}) => {
const [isChecking, setIsChecking] = useState(true);
Expand Down Expand Up @@ -56,6 +55,7 @@ export const Login = (props: {
supabase.auth.onAuthStateChange((event, session) => {
if (event === 'SIGNED_OUT') {
clearCookies();
redirectUrl = null;
} else if (event === 'SIGNED_IN' || event === 'TOKEN_REFRESHED') {
setCookies(session);
if (redirectUrl) {
Expand Down Expand Up @@ -94,31 +94,15 @@ export const Login = (props: {
})
.then(({ data, error }) => {
if (data?.url) {
localStorage.removeItem('redirect-to');
window.location.href = `${data.url}?next=${redirectUrl || '/'}`;
window.location.href = data.url;
} else {
console.error(error);
}
});
};

const signInWithKeycloak = () => {
supabase.auth
.signInWithOAuth({
provider: 'keycloak',
options: {
scopes: 'openid',
redirectTo: `${host}/auth/callback`,
},
})
.then(({ data, error }) => {
if (data?.url) {
localStorage.removeItem('redirect-to');
window.location.href = `${data.url}?next=${redirectUrl || '/'}`;
} else {
console.error(error);
}
});
window.location.href = `/${props.i18n.lang}/keycloak`;
};

const renderLoginButton = useCallback(
Expand Down
18 changes: 16 additions & 2 deletions src/apps/auth-logout/Logout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useEffect, useState } from 'react';
import { SmileySad } from '@phosphor-icons/react';
import { useEffect } from 'react';
import { supabase } from '@backend/supabaseBrowserClient';
import type { Translations } from 'src/Types';
import { Spinner } from '@components/Spinner';
Expand All @@ -14,6 +13,21 @@ const clearCookies = () => {
};

export const Logout = (props: { i18n: Translations }) => {
localStorage.removeItem('redirect-to');
const arr = []; // Array to hold the keys
// Iterate over localStorage and find 'sb_
for (let i = 0; i < localStorage.length; i++) {
if (localStorage.key(i)?.substring(0, 3) == 'sb-') {
arr.push(localStorage.key(i));
}
}

// Iterate over arr and remove the items by key
for (let i = 0; i < arr.length; i++) {
if (arr[i]) {
localStorage.removeItem(arr[i] as string);
}
}
useEffect(() => {
supabase.auth.signOut().then(() => {
clearCookies();
Expand Down
6 changes: 5 additions & 1 deletion src/backend/supabaseBrowserClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type SupabaseClient } from '@supabase/supabase-js';
import { type SupabaseClient, createClient } from '@supabase/supabase-js';
import { createBrowserClient } from '@supabase/ssr';

const SUPABASE = import.meta.env.PUBLIC_SUPABASE;
Expand All @@ -10,3 +10,7 @@ const SUPABASE_API_KEY = import.meta.env.PUBLIC_SUPABASE_API_KEY;
export const supabase: SupabaseClient =
typeof window !== 'undefined' &&
createBrowserClient(SUPABASE, SUPABASE_API_KEY);

// @ts-ignore
export const supabaseImplicit: SupabaseClient =
typeof window !== 'undefined' && createClient(SUPABASE, SUPABASE_API_KEY);
14 changes: 14 additions & 0 deletions src/components/CheckRedirect/CheckRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect } from 'react';

export const CheckRedirect = () => {
useEffect(() => {
const redirectUrl = localStorage.getItem('redirect-to');

if (redirectUrl && redirectUrl.length > 0) {
localStorage.removeItem('redirect-to');
window.location.href = redirectUrl;
}
}, []);

return <div />;
};
1 change: 1 addition & 0 deletions src/components/CheckRedirect/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './CheckRedirect';
2 changes: 1 addition & 1 deletion src/pages/[lang]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const lang = getLangFromUrl(Astro.url);
const me = await getMyProfile(supabase);
if (me.error || !me.data) {
return Astro.redirect(`/${lang}/sign-in?redirect-to=${Astro.url.pathname}`);
return Astro.redirect(`/${lang}/sign-in`);
} else {
return Astro.redirect(`/${lang}/projects/`);
}
Expand Down
12 changes: 12 additions & 0 deletions src/pages/[lang]/keycloak.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
import { getTranslations } from '@i18n';
import LoginLayout from '@layouts/login/LoginLayout.astro';
import { AuthKeycloak } from '@apps/auth-keycloak/AuthKeycloak';
---

<LoginLayout title='' showLogo={true}>
<AuthKeycloak
client:only='react'
i18n={getTranslations(Astro.request, 'auth-login')}
/>
</LoginLayout>
2 changes: 2 additions & 0 deletions src/pages/[lang]/projects/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getMyProfile, listMyInvites } from '@backend/crud';
import { ProjectsHome } from '@apps/dashboard-projects';
import { listMyProjectsExtended } from '@backend/helpers';
import BaseLayout from '@layouts/BaseLayout.astro';
import { CheckRedirect } from '@components/CheckRedirect';
const lang = getLangFromUrl(Astro.url);
Expand All @@ -30,6 +31,7 @@ if (!projects.data || !invitations.data) {
---

<BaseLayout title='Dashboard'>
<CheckRedirect client:only='react' />
<ProjectsHome
client:only='react'
me={me.data}
Expand Down
14 changes: 9 additions & 5 deletions src/pages/[lang]/sign-out.astro
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import { createSupabaseServerClient } from '@backend/supabaseServerClient';
const supabase = await createSupabaseServerClient(Astro.request, Astro.cookies);
await supabase.auth.signOut();
---
<BaseLayout title="Recogito | Logout">
<div class="container">
<div class="column">
<Logout i18n={getTranslations(Astro.request, 'error')} client:load />

<BaseLayout title='Recogito | Logout'>
<div class='container'>
<div class='column'>
<Logout
i18n={getTranslations(Astro.request, 'error')}
client:only='react'
/>
</div>
</div>
</BaseLayout>
Expand All @@ -32,4 +36,4 @@ await supabase.auth.signOut();
position: relative;
width: 100%;
}
</style>
</style>
2 changes: 0 additions & 2 deletions src/pages/auth/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export const GET: APIRoute = async ({ request, cookies, redirect, url }) => {
const code = requestUrl.searchParams.get('code');
const next = requestUrl.searchParams.get('next') || '/';

console.log('In callback, url = ', url, ', next = ', next);

if (code) {
const supabase = createServerClient(
import.meta.env.PUBLIC_SUPABASE,
Expand Down
7 changes: 4 additions & 3 deletions src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ import parser from 'accept-language-parser';
const accepts = Astro.request.headers.get('accept-language');
const lang = accepts ? parser.pick(Object.keys(languages), accepts) : defaultLang;
const lang = accepts
? parser.pick(Object.keys(languages), accepts)
: defaultLang;
const supabase = await createSupabaseServerClient(Astro.request, Astro.cookies);
const me = await getMyProfile(supabase);
if (me.error || !me.data) {
return Astro.redirect(`/${lang}/sign-in?redirect-to=${Astro.url.pathname}`);
return Astro.redirect(`/${lang}/sign-in`);
} else {
return Astro.redirect(`/${lang}/projects/`);
}
---

0 comments on commit 63bd329

Please sign in to comment.