in the middle, working for login and logout test,

This commit is contained in:
louiscklaw
2025-05-14 17:19:48 +08:00
parent 56f0f30ffb
commit 0fcc194860
5 changed files with 60 additions and 22 deletions

View File

@@ -1,5 +1,19 @@
import type { BillingAddress } from '@/components/dashboard/user_meta/type.d'; import type { BillingAddress } from '@/components/dashboard/user_meta/type.d';
// DBUserMeta type definitions
export interface DBUserMeta {
name: string;
avatar: string;
email: string;
phone: string;
quota: number;
status: 'active' | 'blocked' | 'pending';
//
collectionId: string;
id: string;
createdAt: Date;
}
// UserMeta type definitions // UserMeta type definitions
export interface UserMeta { export interface UserMeta {
id: string; id: string;

View File

@@ -0,0 +1,5 @@
import { DBUserMeta } from '../db/UserMetas/type';
export function getStudentAvatar(studentMeta: DBUserMeta) {
return `url(http://localhost:8090/api/files/${studentMeta.collectionId}/${studentMeta.id}/${studentMeta.avatar})`;
}

View File

@@ -49,6 +49,7 @@ function AuthorizedTest(): React.JSX.Element {
<IonGrid className="ion-padding"> <IonGrid className="ion-padding">
<IonCol> <IonCol>
<IonRow>Authorized page test</IonRow> <IonRow>Authorized page test</IonRow>
{JSON.stringify({ user })}
{/* */} {/* */}
<IonRow> <IonRow>
<IonButton onClick={handleViewStudentInfoOnClick}>{t('view-student-info')}</IonButton> <IonButton onClick={handleViewStudentInfoOnClick}>{t('view-student-info')}</IonButton>

View File

@@ -24,7 +24,7 @@ import { LoadingScreen } from '../../../components/LoadingScreen';
const AuthHome = () => { const AuthHome = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const { user } = useUser(); const { user, checkSession } = useUser();
const router = useIonRouter(); const router = useIonRouter();
const [showLoading, setShowLoading] = useState<boolean>(true); const [showLoading, setShowLoading] = useState<boolean>(true);
@@ -32,19 +32,29 @@ const AuthHome = () => {
show: false, show: false,
message: '', message: '',
}); });
const [checkingSession, setCheckingSession] = useState<boolean>(true);
useEffect(() => { useEffect(() => {
if (!user) { if (!checkingSession) {
} else { if (!user) {
if (user?.id) { router.push(Paths.AuthLogin);
} else {
router.push(Paths.AuthorizedTest); router.push(Paths.AuthorizedTest);
} }
} }
setShowLoading(false); setShowLoading(false);
}, []); }, [user, checkingSession]);
useEffect(() => {
checkSession?.()
.then(() => {
setCheckingSession(false);
})
.catch((err) => console.error(err));
}, [checkSession]);
if (showLoading) return <LoadingScreen />; if (showLoading) return <LoadingScreen />;
if (showError) return <>{showError.message}</>; // if (showError) return <>{showError.message}</>;
return ( return (
<IonPage className={'styles.homePage'}> <IonPage className={'styles.homePage'}>

View File

@@ -1,41 +1,35 @@
import { import {
IonAvatar,
IonBackButton,
IonButton, IonButton,
IonButtons,
IonCardTitle,
IonCol, IonCol,
IonContent, IonContent,
IonFooter, IonFooter,
IonGrid, IonGrid,
IonHeader, IonHeader,
IonIcon,
IonImg,
IonInput,
IonLabel,
IonPage, IonPage,
IonRow, IonRow,
IonText, IonText,
IonToolbar,
useIonRouter, useIonRouter,
} from '@ionic/react'; } from '@ionic/react';
import styles from './style.module.scss'; import styles from './style.module.scss';
import _ from 'lodash'; import { useParams } from 'react-router';
import { Router, useParams } from 'react-router';
import { Wave } from '../../../components/Wave'; import { Wave } from '../../../components/Wave';
import { Paths } from '../../../Paths'; import { Paths } from '../../../Paths';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { getUserMetaById } from '../../../db/UserMetas/GetById'; import { getUserMetaById } from '../../../db/UserMetas/GetById';
import { useTranslation } from 'react-i18next'; import { useTranslation } from 'react-i18next';
import { UserMeta } from '../../../db/UserMetas/type'; import { DBUserMeta } from '../../../db/UserMetas/type';
import { LoadingScreen } from '../../../components/LoadingScreen'; import { LoadingScreen } from '../../../components/LoadingScreen';
import { getStudentAvatar } from '../../../lib/getStudentAvatar';
import { authClient } from '../../../lib/auth/custom/client';
import { useUser } from '../../../hooks/use-user';
function StudentInfo(): React.JSX.Element { function StudentInfo(): React.JSX.Element {
const router = useIonRouter(); const router = useIonRouter();
const { id } = useParams<{ id: string }>(); const { id } = useParams<{ id: string }>();
const { t } = useTranslation(); const { t } = useTranslation();
const [studentMeta, setStudentMeta] = useState<UserMeta>(); const [studentMeta, setStudentMeta] = useState<DBUserMeta>();
const test = useUser();
const [showLoading, setShowLoading] = useState<boolean>(true); const [showLoading, setShowLoading] = useState<boolean>(true);
const [showError, setShowError] = useState<{ show: boolean; message: string }>({ show: false, message: '' }); const [showError, setShowError] = useState<{ show: boolean; message: string }>({ show: false, message: '' });
@@ -51,7 +45,7 @@ function StudentInfo(): React.JSX.Element {
async function handleFetchUserMeta() { async function handleFetchUserMeta() {
try { try {
const result = await getUserMetaById(id); const result = await getUserMetaById(id);
const tempStudentMeta = result as unknown as UserMeta; const tempStudentMeta = result as unknown as DBUserMeta;
setStudentMeta(tempStudentMeta); setStudentMeta(tempStudentMeta);
setShowLoading(false); setShowLoading(false);
@@ -61,11 +55,21 @@ function StudentInfo(): React.JSX.Element {
} }
} }
async function handleLogoutOnClick() {
try {
await authClient.signOut();
router.push(Paths.AuthLogin);
} catch (error) {
console.error(error);
}
}
useEffect(() => { useEffect(() => {
void handleFetchUserMeta(); void handleFetchUserMeta();
}, []); }, []);
if (showLoading) return <LoadingScreen />; if (showLoading) return <LoadingScreen />;
if (!studentMeta) return <LoadingScreen />;
if (showError.show) return <>{showError.message}</>; if (showError.show) return <>{showError.message}</>;
return ( return (
@@ -78,7 +82,7 @@ function StudentInfo(): React.JSX.Element {
<IonCol size={'3'}> <IonCol size={'3'}>
<div <div
style={{ style={{
backgroundImage: `url("https://docs-demo.ionic.io/assets/madison.jpg")`, backgroundImage: getStudentAvatar(studentMeta),
backgroundSize: 'cover', backgroundSize: 'cover',
backgroundPosition: 'center', backgroundPosition: 'center',
backgroundRepeat: 'no-repeat', backgroundRepeat: 'no-repeat',
@@ -129,7 +133,11 @@ function StudentInfo(): React.JSX.Element {
</IonRow> </IonRow>
{/* */} {/* */}
<IonRow className="ion-justify-content-center"> <IonRow className="ion-justify-content-center">
<IonButton onClick={handleBackOnClick}>Back</IonButton> <IonButton onClick={handleBackOnClick}>{t('back')}</IonButton>
</IonRow>
<IonRow className="ion-justify-content-center">
<IonButton onClick={handleLogoutOnClick}>{t('logout')}</IonButton>
</IonRow> </IonRow>
</IonGrid> </IonGrid>
</IonContent> </IonContent>