refactoring, check auth
This commit is contained in:
177
src/renderer/hooks/useAuth.ts
Normal file
177
src/renderer/hooks/useAuth.ts
Normal file
@ -0,0 +1,177 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
const useAuth = () => {
|
||||
const [status, setStatus] = useState('');
|
||||
|
||||
const validateSession = async (accessToken: string) => {
|
||||
try {
|
||||
const response = await fetch('https://authserver.ely.by/auth/validate', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
accessToken: accessToken,
|
||||
}),
|
||||
});
|
||||
return response.ok;
|
||||
} catch (error) {
|
||||
console.log(`Ошибка при проверке токена: ${error.message}`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const refreshSession = async (accessToken: string, clientToken: string) => {
|
||||
try {
|
||||
const refreshData = {
|
||||
accessToken: accessToken,
|
||||
clientToken: clientToken,
|
||||
};
|
||||
|
||||
const response = await fetch('https://authserver.ely.by/auth/refresh', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(refreshData),
|
||||
});
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json();
|
||||
const newAccessToken = data.accessToken;
|
||||
const profile = data.selectedProfile;
|
||||
const uuid = profile.id;
|
||||
const name = profile.name;
|
||||
|
||||
if (newAccessToken && uuid && name) {
|
||||
return {
|
||||
accessToken: newAccessToken,
|
||||
uuid: uuid,
|
||||
username: name,
|
||||
clientToken: clientToken,
|
||||
};
|
||||
}
|
||||
}
|
||||
return null;
|
||||
} catch (error) {
|
||||
console.log(`Ошибка при обновлении сессии: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const authenticateWithElyBy = async (
|
||||
username: string,
|
||||
password: string,
|
||||
saveConfig: Function,
|
||||
) => {
|
||||
try {
|
||||
const clientToken = crypto.randomUUID();
|
||||
const authData = {
|
||||
username: username,
|
||||
password: password,
|
||||
clientToken: clientToken,
|
||||
requestUser: true,
|
||||
};
|
||||
|
||||
console.log(`Аутентификация пользователя ${username} на Ely.by...`);
|
||||
|
||||
const response = await fetch(
|
||||
'https://authserver.ely.by/auth/authenticate',
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(authData),
|
||||
},
|
||||
);
|
||||
|
||||
const responseData = await response.json();
|
||||
if (response.ok) {
|
||||
const accessToken = responseData.accessToken;
|
||||
const profile = responseData.selectedProfile;
|
||||
const uuid = profile.id;
|
||||
const name = profile.name;
|
||||
|
||||
if (accessToken && uuid && name) {
|
||||
saveConfig(
|
||||
username,
|
||||
4096, // default memory
|
||||
accessToken,
|
||||
clientToken,
|
||||
'',
|
||||
password,
|
||||
);
|
||||
console.log(`Аутентификация успешна: UUID=${uuid}, Username=${name}`);
|
||||
|
||||
return {
|
||||
accessToken: accessToken,
|
||||
uuid: uuid,
|
||||
username: name,
|
||||
clientToken: clientToken,
|
||||
};
|
||||
}
|
||||
} else {
|
||||
if (responseData.error === 'Account protected with two factor auth') {
|
||||
const totpToken = prompt(
|
||||
'Введите код двухфакторной аутентификации:',
|
||||
'',
|
||||
);
|
||||
|
||||
if (totpToken) {
|
||||
authData.password = `${password}:${totpToken}`;
|
||||
const totpResponse = await fetch(
|
||||
'https://authserver.ely.by/auth/authenticate',
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(authData),
|
||||
},
|
||||
);
|
||||
if (totpResponse.ok) {
|
||||
const totpData = await totpResponse.json();
|
||||
const newAccessToken = totpData.accessToken;
|
||||
const newProfile = totpData.selectedProfile;
|
||||
const newUuid = newProfile.id;
|
||||
const newName = newProfile.name;
|
||||
|
||||
if (newAccessToken && newUuid && newName) {
|
||||
saveConfig(
|
||||
username,
|
||||
4096, // default memory
|
||||
newAccessToken,
|
||||
clientToken,
|
||||
'',
|
||||
password,
|
||||
);
|
||||
return {
|
||||
accessToken: newAccessToken,
|
||||
uuid: newUuid,
|
||||
username: newName,
|
||||
clientToken: clientToken,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(responseData.error || 'Ошибка авторизации');
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`Ошибка авторизации: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
status,
|
||||
setStatus,
|
||||
validateSession,
|
||||
refreshSession,
|
||||
authenticateWithElyBy,
|
||||
};
|
||||
};
|
||||
|
||||
export default useAuth;
|
69
src/renderer/hooks/useConfig.ts
Normal file
69
src/renderer/hooks/useConfig.ts
Normal file
@ -0,0 +1,69 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
const useConfig = () => {
|
||||
const [config, setConfig] = useState({
|
||||
username: '',
|
||||
password: '',
|
||||
memory: 4096,
|
||||
comfortVersion: '',
|
||||
accessToken: '',
|
||||
clientToken: '',
|
||||
});
|
||||
|
||||
const loadInitialConfig = () => {
|
||||
try {
|
||||
const savedConfig = localStorage.getItem('launcher_config');
|
||||
if (savedConfig) {
|
||||
return JSON.parse(savedConfig);
|
||||
}
|
||||
} catch (error) {
|
||||
console.log('Ошибка загрузки конфигурации:', error);
|
||||
}
|
||||
return {
|
||||
username: '',
|
||||
password: '',
|
||||
memory: 4096,
|
||||
comfortVersion: '',
|
||||
accessToken: '',
|
||||
clientToken: '',
|
||||
};
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
const savedConfig = loadInitialConfig();
|
||||
setConfig(savedConfig);
|
||||
}, []);
|
||||
|
||||
const saveConfig = (
|
||||
username: string,
|
||||
memory: number,
|
||||
accessToken = '',
|
||||
clientToken = '',
|
||||
comfortVersion = '',
|
||||
password = '',
|
||||
) => {
|
||||
try {
|
||||
const newConfig = {
|
||||
username,
|
||||
memory,
|
||||
accessToken: accessToken || config.accessToken,
|
||||
clientToken: clientToken || config.clientToken,
|
||||
comfortVersion: comfortVersion || config.comfortVersion,
|
||||
password: password || config.password,
|
||||
};
|
||||
setConfig(newConfig);
|
||||
localStorage.setItem('launcher_config', JSON.stringify(newConfig));
|
||||
} catch (error) {
|
||||
console.log(`Ошибка при сохранении конфигурации: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
const { name, value } = e.target;
|
||||
setConfig((prev) => ({ ...prev, [name]: value }));
|
||||
};
|
||||
|
||||
return { config, setConfig, saveConfig, handleInputChange };
|
||||
};
|
||||
|
||||
export default useConfig;
|
Reference in New Issue
Block a user