Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu-tro committed Sep 12, 2024
1 parent bcde6b0 commit 8825ba3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/APIClient/APIClient10.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import Cookies from 'js-cookie';
class APIClient10 {
constructor() {
this.apiUrl = localStorage.getItem('NODE_URL') ? localStorage.getItem('NODE_URL') : "http://localhost:5001";
this.token = Cookies.get(this.apiUrl) || '';
this.request = async (url, body, config) => {
const isFormData = body instanceof FormData
const token = Cookies.get(this.apiUrl) || '';
const addToken = url.includes('?')? `&token=${token}` : `?token=${token}`
const addToken = url.includes('?')? `&token=${this.token}` : `?token=${this.token}`
return new Promise(async (resolve, reject) => {
try {

Expand Down Expand Up @@ -67,6 +67,10 @@ class APIClient10 {
this.apiUrl = url;
}

updateToken() {
this.token = Cookies.get(this.apiUrl) || ''
}


getHostVersion() {
return this.request('/api/v1/version');
Expand Down
33 changes: 23 additions & 10 deletions src/components/Login/PasswordLogin.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useState, useEffect } from 'react';
import React, { useState, useEffect } from 'react';
import { useIntl } from 'react-intl';
import {useHistory } from 'react-router-dom';
import { Form, Input } from 'antd';
import { useHistory } from 'react-router-dom';
import { Form, Input } from 'antd';
import { t } from 'utils/text.js';
import { aseEncode } from 'utils/BTFSUtil';
import { ArrowLeftOutlined } from '@ant-design/icons';
Expand All @@ -10,6 +10,7 @@ import Cookies from 'js-cookie';
import Emitter from 'utils/eventBus';

import { login } from 'services/login.js';
import { updateLoginToken } from 'services/otherService';

const PasswordLogin = ({ color, endpoint }) => {
const history = useHistory();
Expand All @@ -26,6 +27,7 @@ const PasswordLogin = ({ color, endpoint }) => {
let res = await login(psw);
if (res && res.Success) {
Cookies.set(endpoint, res.Text, { expires: 1 });
updateLoginToken();
history.push('/admin/settings');
} else {
setTimes(times + 1);
Expand All @@ -34,12 +36,15 @@ const PasswordLogin = ({ color, endpoint }) => {
};

const LostPassword = () => {
if (isLock) {
return;
}
Emitter.emit('handleLostPassword');
};

const backPrevious = ()=>{
const backPrevious = () => {
Emitter.emit('showEndpoint');
}
};

useEffect(() => {
if (times >= 5) {
Expand All @@ -64,7 +69,12 @@ const PasswordLogin = ({ color, endpoint }) => {
return (
<div className="flex flex-col justify-center max-w-450px min-w-334px login-form-w ">
<div className="min-h-400">
<div className="login-title mb-12 theme-text-main"><span onClick={backPrevious} className='cursor-pointer pr-2'><ArrowLeftOutlined style={{ fontSize: 20 }} className='align-middle' /></span>{t('login_title')}</div>
<div className="login-title mb-12 theme-text-main">
<span onClick={backPrevious} className="cursor-pointer pr-2">
<ArrowLeftOutlined style={{ fontSize: 20 }} className="align-middle" />
</span>
{t('login_title')}
</div>
<Form
name="basic"
layout="vertical"
Expand All @@ -78,7 +88,10 @@ const PasswordLogin = ({ color, endpoint }) => {
<Form.Item
label={<div className="font-bold theme-text-main">API {t('endpoint')}</div>}
name="endpoint">
<Input className="mr-2 common-input theme-text-desc theme-base-bg border-none" disabled />
<Input
className="mr-2 common-input theme-text-desc theme-base-bg border-none"
disabled
/>
</Form.Item>

<Form.Item
Expand All @@ -103,9 +116,9 @@ const PasswordLogin = ({ color, endpoint }) => {
},
]}>
<Input.Password

placeholder={intl.formatMessage({ id: 'enter_password_placeholder' })}
className="mr-2 common-input theme-bg theme-border-color theme-text-main" />
placeholder={intl.formatMessage({ id: 'enter_password_placeholder' })}
className="mr-2 common-input theme-bg theme-border-color theme-text-main"
/>
</Form.Item>
<div className="flex justify-between w-full mt-2 ml-1 ">
<span className="theme-text-error text-sm pt-1">{validateMsg}</span>
Expand Down
11 changes: 8 additions & 3 deletions src/components/Modals/ChangePasswordModal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import React, { useEffect, useState } from 'react';
import { useIntl } from 'react-intl';
import { useHistory } from 'react-router-dom';
import { logout } from 'services/login.js';

import { LoadingOutlined } from '@ant-design/icons';
import { Spin, Form, Input } from 'antd';
import Emitter from 'utils/eventBus';
Expand All @@ -14,6 +17,7 @@ export default function ChangePasswordModal({ color }) {
const [form] = Form.useForm();
const [showModal, setShowModal] = useState(false);
const [loading, setLoading] = useState(false);
const history = useHistory();

useEffect(() => {
const set = async function (params) {
Expand Down Expand Up @@ -60,6 +64,9 @@ export default function ChangePasswordModal({ color }) {
type: 'frontEnd',
});
closeModal();
await logout();
Cookies.remove(NODE_URL);
history.push('/login');
} else {
Emitter.emit('showMessageAlert', {
message: 'change_password_fail',
Expand Down Expand Up @@ -104,9 +111,7 @@ export default function ChangePasswordModal({ color }) {
onFinish={onFinish}
autoComplete="off">
<Form.Item
label={
<div className="font-bold theme-text-main">{t('enter_old_password')}</div>
}
label={<div className="font-bold theme-text-main">{t('enter_old_password')}</div>}
name="oldpassword"
rules={[
{ required: true, message: t('private_key_validate_required') },
Expand Down
7 changes: 5 additions & 2 deletions src/components/Modals/EncryptFileModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export default function EncryptFileModal({ color }) {
const [isCurHost, setIsCurHost] = useState(true);
const [password, setPassword] = useState('');
const [validateKeyMsg, setValidateKeyMsg] = useState('');
const [path, setPath] = useState(null);

// const controller = useRef<AbortController>(new AbortController());

const init = () => {
Expand All @@ -61,8 +63,9 @@ export default function EncryptFileModal({ color }) {
};
useEffect(() => {
const set = async function (params) {
console.log('openEncryptFileModal event has occured');
console.log('openEncryptFileModal event has occured',params);
init();
setPath(params.path);
openModal();
};
Emitter.on('openEncryptFileModal', set);
Expand Down Expand Up @@ -121,7 +124,7 @@ export default function EncryptFileModal({ color }) {
setLoading(true);
let hostid = encryptType === 'host' ? hostId : '';
let passwords = encryptType === 'host' ? '' : password;
let result = await encryptUploadFiles(currentFile, hostid, passwords, onUploadProgress(fileName));
let result = await encryptUploadFiles(currentFile, hostid, passwords,path,onUploadProgress(fileName));
Emitter.emit('openEncryptFileCidModal', result);
Emitter.emit('updateFiles');
} catch (e) {
Expand Down
4 changes: 3 additions & 1 deletion src/services/filesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,12 @@ export const removeFiles = async (hash, name, path, type) => {
}
};

export const encryptUploadFiles = async (file, hostId, password, onUploadProgress) => {
export const encryptUploadFiles = async (file, hostId, password,path, onUploadProgress) => {
console.log(file, hostId, password,path, onUploadProgress)
try {
const formData = new FormData();
formData.append('file', file);
formData.append('path', path);
let res = await Client10.encrypt(formData, hostId, password, onUploadProgress);
if (res?.Type === 'error') {
return Promise.reject(res);
Expand Down
9 changes: 9 additions & 0 deletions src/services/otherService.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ export const setApiUrl = (url) => {
try {
Client10.setApiUrl(url);
setFileServiceApiUrl(url)
updateLoginToken()
} catch (e) {
console.log(e);
}
};

export const updateLoginToken = () => {
try {
Client10.updateToken();
} catch (e) {
console.log(e);
}
Expand Down

0 comments on commit 8825ba3

Please sign in to comment.