WIP
This commit is contained in:
parent
252bba2cdb
commit
c4fd8da72b
@ -77,6 +77,10 @@ const mimeTypesAccepted: { [key: string]: IMimeTypes } = {
|
||||
extension: "txt",
|
||||
size: 104857600, // 100MB
|
||||
},
|
||||
"application/json": {
|
||||
extension: "json",
|
||||
size: 104857600, // 100MB
|
||||
},
|
||||
};
|
||||
|
||||
type IDocumentFileBase = {
|
||||
|
@ -13,7 +13,6 @@ type IProps = {
|
||||
|
||||
export default function StepImportProfile(props: IProps) {
|
||||
const { onSubmit, onBack, validationErrors } = props;
|
||||
const [importedFile, setImportedFile] = useState<File | null>(null);
|
||||
const [profileData, setProfileData] = useState<any>(null);
|
||||
const [error, setError] = useState<string>("");
|
||||
|
||||
@ -22,44 +21,32 @@ export default function StepImportProfile(props: IProps) {
|
||||
return data &&
|
||||
typeof data === "object" &&
|
||||
data.version &&
|
||||
data.userData &&
|
||||
typeof data.userData === "object";
|
||||
data.user_data &&
|
||||
typeof data.user_data === "object";
|
||||
};
|
||||
|
||||
const handleFileUpload = useCallback(async (files: File[]) => {
|
||||
const handleFileUpload = useCallback((files: File[]) => {
|
||||
const file = files[0];
|
||||
if (!file) return;
|
||||
|
||||
// Validate file type
|
||||
if (file.type !== "application/json") {
|
||||
setError("Veuillez sélectionner un fichier JSON valide.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Validate file size (max 1MB)
|
||||
if (file.size > 1024 * 1024) {
|
||||
setError("Le fichier est trop volumineux. Taille maximum : 1MB.");
|
||||
return;
|
||||
}
|
||||
|
||||
setImportedFile(file);
|
||||
setError("");
|
||||
|
||||
// Read and parse JSON
|
||||
try {
|
||||
const text = await file.text();
|
||||
file.text()
|
||||
.then((text) => {
|
||||
const data = JSON.parse(text);
|
||||
|
||||
// Validate profile structure
|
||||
if (!validateProfileStructure(data)) {
|
||||
setError("Le fichier ne contient pas un profil valide.");
|
||||
setError('Le fichier ne contient pas un profil valide.');
|
||||
return;
|
||||
}
|
||||
|
||||
setProfileData(data);
|
||||
} catch (err) {
|
||||
setError("Erreur lors de la lecture du fichier JSON.");
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
setError(`Erreur lors de la lecture du fichier JSON: ${e}`);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleSubmit = useCallback(() => {
|
||||
@ -100,15 +87,12 @@ export default function StepImportProfile(props: IProps) {
|
||||
<Typography typo={ETypo.TEXT_LG_SEMIBOLD} className={classes["preview-title"]}>
|
||||
Profil détecté :
|
||||
</Typography>
|
||||
<Typography typo={ETypo.TEXT_MD_REGULAR}>
|
||||
{profileData.userData?.email || "Email non disponible"}
|
||||
</Typography>
|
||||
<Typography typo={ETypo.TEXT_SM_REGULAR} color={ETypoColor.COLOR_NEUTRAL_600}>
|
||||
Version : {profileData.version}
|
||||
</Typography>
|
||||
{profileData.exportedAt && (
|
||||
{profileData.exported_at && (
|
||||
<Typography typo={ETypo.TEXT_SM_REGULAR} color={ETypoColor.COLOR_NEUTRAL_600}>
|
||||
Exporté le : {new Date(profileData.exportedAt).toLocaleDateString('fr-FR')}
|
||||
Exporté le : {new Date(profileData.exported_at).toLocaleDateString('fr-FR')}
|
||||
</Typography>
|
||||
)}
|
||||
</div>
|
||||
|
@ -22,6 +22,8 @@ import UserStore from "@Front/Stores/UserStore";
|
||||
import AuthModal from "src/sdk/AuthModal";
|
||||
import CustomerService from "src/common/Api/LeCoffreApi/sdk/CustomerService";
|
||||
import StepImportProfile from "./StepImportProfile";
|
||||
import MessageBus from "src/sdk/MessageBus";
|
||||
import Iframe from "src/sdk/Iframe";
|
||||
|
||||
export enum LoginStep {
|
||||
EMAIL,
|
||||
@ -46,6 +48,7 @@ export default function Login() {
|
||||
const [validationErrors, setValidationErrors] = useState<ValidationError[]>([]);
|
||||
const [isAuthModalOpen, setIsAuthModalOpen] = useState(false);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [showIframeForImport, setShowIframeForImport] = useState(false);
|
||||
|
||||
// const openErrorModal = useCallback(() => {
|
||||
// setIsErrorModalOpen(true);
|
||||
@ -249,25 +252,21 @@ export default function Login() {
|
||||
setIsLoading(true);
|
||||
setValidationErrors([]);
|
||||
|
||||
// Call API to validate and import profile
|
||||
// Note: You'll need to implement this method in your Auth service
|
||||
// const response = await Auth.getInstance().importProfile(profileData);
|
||||
// Show iframe for import operation (but hidden from view)
|
||||
setShowIframeForImport(true);
|
||||
|
||||
// For now, we'll simulate a successful import
|
||||
// In a real implementation, you would:
|
||||
// 1. Send the profile data to your backend
|
||||
// 2. Validate the profile on the server
|
||||
// 3. Return authentication tokens
|
||||
// 4. Connect the user
|
||||
// Initialize message listener for import operation
|
||||
MessageBus.getInstance().initMessageListener();
|
||||
|
||||
// Simulate API call
|
||||
await new Promise(resolve => setTimeout(resolve, 1000));
|
||||
// Wait for the iframe to be ready
|
||||
await MessageBus.getInstance().isReady();
|
||||
|
||||
// For demo purposes, we'll just redirect to the dashboard
|
||||
// In reality, you'd use the response from the API
|
||||
// CustomerStore.instance.connect(response.accessToken, response.refreshToken);
|
||||
await MessageBus.getInstance().importBackup(profileData);
|
||||
|
||||
// Clean up message listener after import
|
||||
MessageBus.getInstance().destroyMessageListener();
|
||||
setShowIframeForImport(false);
|
||||
|
||||
// Redirect to dashboard
|
||||
router.push(Module.getInstance().get().modules.pages.Folder.pages.Select.props.path);
|
||||
} catch (error: any) {
|
||||
setValidationErrors([
|
||||
@ -280,6 +279,7 @@ export default function Login() {
|
||||
]);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setShowIframeForImport(false);
|
||||
}
|
||||
}, [router]);
|
||||
|
||||
@ -337,6 +337,7 @@ export default function Login() {
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
{showIframeForImport && <Iframe showIframe={false} />}
|
||||
{/* <Confirm
|
||||
isOpen={isErrorModalOpen}
|
||||
onClose={closeErrorModal}
|
||||
|
@ -105,12 +105,7 @@ export default function LoginCallBack() {
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
async function getUser() {
|
||||
UserStore.instance.disconnect();
|
||||
|
||||
// TODO: review
|
||||
// HACK: If start with http://local.lecoffreio.4nkweb:3000/authorized-client
|
||||
// Replace with http://localhost:3000/authorized-client
|
||||
UserStore.instance.disconnect().then(() => {
|
||||
if (window.location.href.startsWith('http://local.lecoffreio.4nkweb:3000/authorized-client')) {
|
||||
window.location.href = window.location.href.replace('http://local.lecoffreio.4nkweb:3000/authorized-client', 'http://localhost:3000/authorized-client');
|
||||
return;
|
||||
@ -119,22 +114,11 @@ export default function LoginCallBack() {
|
||||
const code = router.query["code"];
|
||||
if (code) {
|
||||
try {
|
||||
const idNotUser: any = await Auth.getInstance().getIdNotUser(code as string);
|
||||
Auth.getInstance().getIdNotUser(code as string).then((idNotUser: any) => {
|
||||
setIdNotUser(idNotUser);
|
||||
setIsAuthModalOpen(true);
|
||||
/*
|
||||
const token: any = null;
|
||||
if (!token) return router.push(Module.getInstance().get().modules.pages.Login.props.path);
|
||||
await UserStore.instance.connect(token.accessToken, token.refreshToken);
|
||||
const jwt = JwtService.getInstance().decodeJwt();
|
||||
if (!jwt) return router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=1");
|
||||
if (jwt.rules && !jwt.rules.includes("GET folders")) {
|
||||
return router.push(Module.getInstance().get().modules.pages.Subscription.pages.New.props.path);
|
||||
}
|
||||
setIsAuthModalOpen(true);
|
||||
//return router.push(Module.getInstance().get().modules.pages.Folder.props.path);
|
||||
*/
|
||||
return;
|
||||
});
|
||||
} catch (e: any) {
|
||||
if (e.http_status === 401 && e.message === "Email not found") {
|
||||
return router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=3");
|
||||
@ -145,24 +129,8 @@ export default function LoginCallBack() {
|
||||
return router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=1");
|
||||
}
|
||||
}
|
||||
/*
|
||||
const refreshToken = CookieService.getInstance().getCookie("leCoffreRefreshToken");
|
||||
if (!refreshToken) return router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=1");
|
||||
const isTokenRefreshed = await JwtService.getInstance().refreshToken(refreshToken);
|
||||
const jwt = JwtService.getInstance().decodeJwt();
|
||||
if (!jwt) return router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=1");
|
||||
if (!jwt.rules.includes("GET folders")) {
|
||||
return router.push(Module.getInstance().get().modules.pages.Subscription.pages.New.props.path);
|
||||
}
|
||||
if (isTokenRefreshed) {
|
||||
//setIsAuthModalOpen(true);
|
||||
//return router.push(Module.getInstance().get().modules.pages.Folder.props.path);
|
||||
return;
|
||||
}
|
||||
*/
|
||||
return router.push(Module.getInstance().get().modules.pages.Login.props.path + "?error=2");
|
||||
}
|
||||
getUser();
|
||||
});
|
||||
}, [router]);
|
||||
|
||||
return (
|
||||
|
@ -677,6 +677,37 @@ export default class MessageBus {
|
||||
});
|
||||
}
|
||||
|
||||
public importBackup(backupFile: any): Promise<void> {
|
||||
return new Promise<void>((resolve: () => void, reject: (error: string) => void) => {
|
||||
const messageId = `IMPORT_BACKUP_${uuidv4()}`;
|
||||
|
||||
const unsubscribe = EventBus.getInstance().on('BACKUP_IMPORTED', (responseId: string) => {
|
||||
console.log('BACKUP_IMPORTED', responseId, messageId);
|
||||
if (responseId !== messageId) {
|
||||
return;
|
||||
}
|
||||
unsubscribe();
|
||||
resolve();
|
||||
});
|
||||
|
||||
const unsubscribeError = EventBus.getInstance().on('ERROR_BACKUP_IMPORTED', (responseId: string, error: string) => {
|
||||
if (responseId !== messageId) {
|
||||
return;
|
||||
}
|
||||
unsubscribeError();
|
||||
reject(error);
|
||||
});
|
||||
|
||||
console.log('IMPORT_BACKUP', backupFile);
|
||||
|
||||
this.sendMessage({
|
||||
type: 'IMPORT_BACKUP',
|
||||
backupFile: JSON.stringify(backupFile),
|
||||
messageId
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public verifyMerkleProof(merkleProof: string, documentHash: string): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve: (isValid: boolean) => void, reject: (error: string) => void) => {
|
||||
this.checkToken().then(() => {
|
||||
@ -900,6 +931,10 @@ export default class MessageBus {
|
||||
this.doHandleMessage(message.messageId, 'BACKUP_RETRIEVED', message, (message: any) => message.backupFile);
|
||||
break;
|
||||
|
||||
case 'BACKUP_IMPORTED': // IMPORT_BACKUP
|
||||
this.doHandleMessage(message.messageId, 'BACKUP_IMPORTED', message, () => { });
|
||||
break;
|
||||
|
||||
case 'ERROR':
|
||||
console.error('Error:', message);
|
||||
this.errors[message.messageId] = message.error;
|
||||
|
Loading…
x
Reference in New Issue
Block a user