Skip to content

Commit

Permalink
fix: token
Browse files Browse the repository at this point in the history
  • Loading branch information
lulu-tro committed Sep 3, 2024
1 parent 8cb62ac commit 133a16e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 14 deletions.
6 changes: 5 additions & 1 deletion src/APIClient/APIClient10.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import xhr from "axios/index";
import { PRECISION_RATE } from "utils/constants";
import Cookies from 'js-cookie';


class APIClient10 {
constructor() {
this.apiUrl = localStorage.getItem('NODE_URL') ? localStorage.getItem('NODE_URL') : "http://localhost:5001";
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}`
return new Promise(async (resolve, reject) => {
try {

let {data} = await xhr.post(
this.apiUrl + url,
this.apiUrl + url + addToken,
isFormData?body:
{
...body
Expand Down
3 changes: 1 addition & 2 deletions src/components/Login/Endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { t } from 'utils/text.js';

const Endpoint = ({ color }) => {
const inputRef = useRef(null);


const getEndpoint = ()=>{
const NODE_URL = localStorage.getItem('NODE_URL');
if(NODE_URL){
Expand All @@ -34,6 +32,7 @@ const Endpoint = ({ color }) => {
e.preventDefault();
e.stopPropagation();
const node_url = getNodeUrl();
localStorage.setItem('NODE_URL', node_url);
if (!node_url) return;
Emitter.emit('handleEndpoint', node_url);
};
Expand Down
2 changes: 1 addition & 1 deletion src/components/Login/PasswordLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const PasswordLogin = ({ color, endpoint }) => {
let psw = aseEncode(password, endpoint);
let res = await login(psw);
if (res && res.Success) {
Cookies.set(endpoint, res.Text, { expires: 1, domain: 'localhost' });
Cookies.set(endpoint, res.Text, { expires: 1 });
history.push('/admin/settings');
} else {
setTimes(times + 1);
Expand Down
2 changes: 1 addition & 1 deletion src/components/Login/SetPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const Endpoint = ({ endpoint, isReset }) => {
try {
let res = await login(password);
if (res && res.Success) {
Cookies.set(endpoint, res.Text, { expires: 1, domain: 'localhost' });
Cookies.set(endpoint, res.Text, { expires: 1});
history.push('/admin/settings');
} else {
Emitter.emit('showMessageAlert', { message: res.Text || 'error', status: 'error' });
Expand Down
10 changes: 8 additions & 2 deletions src/services/login.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import Client10 from "APIClient/APIClient10.js";
import xhr from "axios/index";

export const checkLoginPassword = async () => {


export const checkLoginPassword = async (url) => {
try {
let data = await Client10.checkLoginPassword();
let {data} = await xhr.post(url + '/api/v1/dashboard/check');
if(data){
localStorage.setItem('NODE_URL', url);
}
return data;
} catch (e) {
console.log(e);
Expand Down
14 changes: 7 additions & 7 deletions src/views/login/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ export default function Login(props) {
// }

const endpointChange = async val => {
if(loading)return;
setLoading(true)
if (loading) return;
setLoading(true);
try {
let res = await checkLoginPassword();
setLoading(false)
if(res.Type === 'error'){
let res = await checkLoginPassword(val);
setLoading(false);
if (res.Type === 'error') {
Emitter.emit('showMessageAlert', { message: res.Message || 'error', status: 'error' });
return;
}
setEndpoint(val);
if (res && res.Success) {
setHasPassword(true);
setEndpoint(val);
} else if (res && !res.Success) {
setHasPassword(false);
}
Expand All @@ -55,7 +55,7 @@ export default function Login(props) {
Emitter.on('handleEndpoint', endpointChange);
Emitter.on('handleLostPassword', lostPassword);
document.documentElement.classList.remove('dark');
// eslint-disable-next-line react-hooks/exhaustive-deps
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

return (
Expand Down

0 comments on commit 133a16e

Please sign in to comment.