2022-10-21 00:14:17 -05:00
|
|
|
import type { IdTokenClaims } from '@logto/react';
|
|
|
|
import { LogtoProvider, useLogto, Prompt } from '@logto/react';
|
2022-12-19 11:01:40 -05:00
|
|
|
import { demoAppApplicationId } from '@logto/schemas';
|
2022-07-15 05:01:42 -05:00
|
|
|
import { useEffect, useState } from 'react';
|
2022-06-06 07:06:34 -05:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2022-05-29 08:11:58 -05:00
|
|
|
|
|
|
|
import '@/scss/normalized.scss';
|
2022-05-30 23:39:03 -05:00
|
|
|
import * as styles from './App.module.scss';
|
|
|
|
import Callback from './Callback';
|
2022-06-22 09:49:32 -05:00
|
|
|
import congratsDark from './assets/congrats-dark.svg';
|
2022-05-30 23:39:03 -05:00
|
|
|
import congrats from './assets/congrats.svg';
|
2022-06-06 07:06:34 -05:00
|
|
|
import initI18n from './i18n/init';
|
|
|
|
|
|
|
|
void initI18n();
|
2022-05-30 23:39:03 -05:00
|
|
|
|
|
|
|
const Main = () => {
|
2022-09-09 05:37:04 -05:00
|
|
|
const { isAuthenticated, isLoading, getIdTokenClaims, signIn, signOut } = useLogto();
|
2022-06-29 21:29:29 -05:00
|
|
|
const [user, setUser] = useState<Pick<IdTokenClaims, 'sub' | 'username'>>();
|
2022-06-06 07:06:34 -05:00
|
|
|
const { t } = useTranslation(undefined, { keyPrefix: 'demo_app' });
|
2022-05-30 23:39:03 -05:00
|
|
|
const isInCallback = Boolean(new URL(window.location.href).searchParams.get('code'));
|
2022-06-22 09:49:32 -05:00
|
|
|
const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
|
|
const [congratsIcon, setCongratsIcon] = useState<string>(isDarkMode ? congratsDark : congrats);
|
2022-05-30 23:39:03 -05:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-06-09 02:19:44 -05:00
|
|
|
if (isInCallback) {
|
|
|
|
return;
|
2022-05-30 23:39:03 -05:00
|
|
|
}
|
2022-06-06 07:06:34 -05:00
|
|
|
|
2022-06-09 02:19:44 -05:00
|
|
|
if (isAuthenticated) {
|
|
|
|
(async () => {
|
2022-09-09 05:37:04 -05:00
|
|
|
const userInfo = await getIdTokenClaims();
|
2022-06-29 21:29:29 -05:00
|
|
|
setUser(userInfo ?? { sub: 'N/A', username: 'N/A' });
|
2022-06-09 02:19:44 -05:00
|
|
|
})();
|
2022-09-09 05:37:04 -05:00
|
|
|
} else if (!isLoading) {
|
2022-06-09 02:19:44 -05:00
|
|
|
void signIn(window.location.href);
|
|
|
|
}
|
2022-09-09 05:37:04 -05:00
|
|
|
}, [getIdTokenClaims, isAuthenticated, isLoading, isInCallback, signIn, t]);
|
2022-05-30 23:39:03 -05:00
|
|
|
|
2022-06-22 09:49:32 -05:00
|
|
|
useEffect(() => {
|
|
|
|
const onThemeChange = (event: MediaQueryListEvent) => {
|
|
|
|
const isDarkMode = event.matches;
|
|
|
|
setCongratsIcon(isDarkMode ? congratsDark : congrats);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', onThemeChange);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window
|
|
|
|
.matchMedia('(prefers-color-scheme: dark)')
|
|
|
|
.removeEventListener('change', onThemeChange);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2022-05-30 23:39:03 -05:00
|
|
|
if (isInCallback) {
|
|
|
|
return <Callback />;
|
|
|
|
}
|
|
|
|
|
2022-06-06 07:06:34 -05:00
|
|
|
if (!isAuthenticated || !user) {
|
2022-05-30 23:39:03 -05:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={styles.app}>
|
|
|
|
<div className={styles.card}>
|
2022-06-22 09:49:32 -05:00
|
|
|
{congratsIcon && <img src={congratsIcon} alt="Congrats" />}
|
2022-06-06 07:06:34 -05:00
|
|
|
<div className={styles.title}>{t('title')}</div>
|
|
|
|
<div className={styles.text}>{t('subtitle')}</div>
|
2022-05-30 23:39:03 -05:00
|
|
|
<div className={styles.infoCard}>
|
2022-07-21 04:29:22 -05:00
|
|
|
{user.username && (
|
|
|
|
<div>
|
|
|
|
{t('username')}
|
|
|
|
<span>{user.username}</span>
|
|
|
|
</div>
|
|
|
|
)}
|
2022-06-06 07:06:34 -05:00
|
|
|
<div>
|
|
|
|
{t('user_id')}
|
|
|
|
<span>{user.sub}</span>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div
|
2022-10-09 22:47:52 -05:00
|
|
|
role="button"
|
|
|
|
tabIndex={0}
|
2022-06-06 07:06:34 -05:00
|
|
|
className={styles.button}
|
|
|
|
onClick={async () => signOut(`${window.location.origin}/demo-app`)}
|
2022-10-11 02:23:10 -05:00
|
|
|
onKeyDown={({ key }) => {
|
|
|
|
if (key === 'Enter' || key === ' ') {
|
|
|
|
void signOut(`${window.location.origin}/demo-app`);
|
|
|
|
}
|
|
|
|
}}
|
2022-06-06 07:06:34 -05:00
|
|
|
>
|
|
|
|
{t('sign_out')}
|
2022-05-30 23:39:03 -05:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
2022-05-29 08:11:58 -05:00
|
|
|
|
|
|
|
const App = () => {
|
2022-05-30 23:39:03 -05:00
|
|
|
return (
|
|
|
|
<LogtoProvider
|
|
|
|
config={{
|
|
|
|
endpoint: window.location.origin,
|
|
|
|
appId: demoAppApplicationId,
|
2022-06-29 23:47:06 -05:00
|
|
|
prompt: Prompt.Login,
|
2022-05-30 23:39:03 -05:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Main />
|
|
|
|
</LogtoProvider>
|
|
|
|
);
|
2022-05-29 08:11:58 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|