2022-06-29 23:47:06 -05:00
|
|
|
import { LogtoProvider, useLogto, IdTokenClaims, Prompt } from '@logto/react';
|
2022-06-02 02:32:46 -05:00
|
|
|
import { demoAppApplicationId } from '@logto/schemas/lib/seeds';
|
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-06-29 01:55:56 -05:00
|
|
|
const { isAuthenticated, 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-05 22:37:06 -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
|
|
|
})();
|
|
|
|
} else {
|
|
|
|
void signIn(window.location.href);
|
|
|
|
}
|
2022-06-29 01:55:56 -05:00
|
|
|
}, [getIdTokenClaims, isAuthenticated, 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
|
|
|
|
className={styles.button}
|
|
|
|
onClick={async () => signOut(`${window.location.origin}/demo-app`)}
|
|
|
|
>
|
|
|
|
{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;
|