[+] New auth system

Merge pull request #47 from hubHarmony/plugin-management-system
This commit is contained in:
charleslemaux 2024-09-21 11:30:44 +02:00 committed by GitHub
commit dfd80fd6d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 58 additions and 61 deletions

View File

@ -67,8 +67,9 @@ const CreateServer = ({ user, onCreateServer, onSubdomainUpdate, onCancel, noSer
}; };
useEffect(() => { useEffect(() => {
loadSubdomain(); loadSubdomain().then(r => r);
}, [user]); },
[user]);
const handleSaveSubdomain = async () => { const handleSaveSubdomain = async () => {
try { try {

View File

@ -1,5 +1,5 @@
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import styles from './serverCard.module.scss'; import styles from './DefaultServerCard.module.scss';
import bukkit from '../../assets/frameworks/bukkit.png'; import bukkit from '../../assets/frameworks/bukkit.png';
import fabric from '../../assets/frameworks/fabric.png'; import fabric from '../../assets/frameworks/fabric.png';
@ -10,9 +10,7 @@ import vanilla from '../../assets/frameworks/vanilla.png';
import PropTypes from "prop-types"; import PropTypes from "prop-types";
// eslint-disable-next-line react/prop-types // eslint-disable-next-line react/prop-types
const ServerCard = ({ status, version, name, framework, onRunClick, onStopClick, onDeleteClick , countPlayers , maxPlayers}) => { const DefaultServerCard = ({ status, version, name, framework, onRunClick, onStopClick, onDeleteClick , countPlayers , maxPlayers}) => {
const getFrameworkSource = () => { const getFrameworkSource = () => {
switch (framework) { switch (framework) {
case "bukkit": case "bukkit":
@ -89,7 +87,7 @@ const ServerCard = ({ status, version, name, framework, onRunClick, onStopClick,
}; };
ServerCard.propTypes = { DefaultServerCard.propTypes = {
key: PropTypes.string, key: PropTypes.string,
status: PropTypes.bool, status: PropTypes.bool,
version: PropTypes.string, version: PropTypes.string,
@ -102,4 +100,4 @@ ServerCard.propTypes = {
subdomain: PropTypes.string, subdomain: PropTypes.string,
}; };
export default ServerCard; export default DefaultServerCard;

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import ServerCard from '../../components/serverCard/serverCard'; import DefaultServerCard from '../../components/serverCards/DefaultServerCard.jsx';
import Navbar from '../../components/navbar/Navbar'; import Navbar from '../../components/navbar/Navbar';
import styles from './DashboardPage.module.scss'; import styles from './DashboardPage.module.scss';
import Loading from '../Loading/loading'; import Loading from '../Loading/loading';
@ -87,7 +87,7 @@ const DashboardPage = ({ user }) => {
const handleCopyAddress = () => { const handleCopyAddress = () => {
const address = `${subdomain}.servii.fr`; const address = `${subdomain}.servii.fr`;
navigator.clipboard.writeText(address) navigator.clipboard.writeText(address).then(r => r)
}; };
@ -116,6 +116,7 @@ const DashboardPage = ({ user }) => {
<CreateServer <CreateServer
user={user} user={user}
onCreateServer={handleCreateServerPage} onCreateServer={handleCreateServerPage}
onSubdomainUpdate={setSubdomain}
onCancel={handleCancelCreateServer} onCancel={handleCancelCreateServer}
noServers noServers
/> />
@ -134,19 +135,19 @@ const DashboardPage = ({ user }) => {
Créer un nouveau serveur Créer un nouveau serveur
</button> </button>
{servers.map((server) => ( {servers.map((server) => (
<ServerCard <DefaultServerCard
key={server.id} key={server.id}
status={server.running} status={server.running}
version={server.version} version={server.version}
name={server.name} name={server.name}
framework={server.framework} framework={server.framework}
maxPlayers={server.maxPlayers} maxPlayers={server.maxPlayers}
countPlayers={server.onlinePlayers} countPlayers={server.onlinePlayers}
onRunClick={() => handleRunServer(server.name)} onRunClick={() => handleRunServer(server.name)}
onStopClick={() => handleStopServer(server.name)} onStopClick={() => handleStopServer(server.name)}
onDeleteClick={() => handleDeleteServer(server.name)} onDeleteClick={() => handleDeleteServer(server.name)}
subdomain={subdomain} subdomain={subdomain}
/> />
))} ))}
</div> </div>
)} )}

View File

@ -1,7 +1,8 @@
import { getAuth } from 'firebase/auth'; import {auth} from './firebase.jsx'
import { Bounce, toast } from "react-toastify"; import {Bounce, toast} from "react-toastify";
const apiUrl: string = 'https://www.servii.fr/api'; const apiUrl: string = 'https://www.servii.fr/api';
//const apiUrl: string = 'http://localhost:3000';
interface ApiResponse { interface ApiResponse {
return_code: number; return_code: number;
@ -9,7 +10,6 @@ interface ApiResponse {
} }
interface BaseRequest { interface BaseRequest {
token: string;
} }
interface SubdomainRequest extends BaseRequest { interface SubdomainRequest extends BaseRequest {
@ -87,13 +87,14 @@ function toast_status(status: number, message: string) {
} }
class serviiApi { class serviiApi {
private static async call<T extends BaseRequest>(endpoint: serviiRequest, body: T): Promise<ApiResponse> { private static async call<T extends BaseRequest>(endpoint: serviiRequest, body: T, token: string): Promise<ApiResponse> {
const unreachable: string = "Couldn't find an available API"; const unreachable: string = "Couldn't find an available API";
try { try {
const response = await fetch(`${apiUrl}/${endpoint}`, { const response = await fetch(`${apiUrl}/${endpoint}`, {
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'SST': token,
}, },
body: JSON.stringify(body), body: JSON.stringify(body),
}); });
@ -121,81 +122,77 @@ class serviiApi {
constructor() {} constructor() {}
private static token(): string { private static async token(): Promise<string> {
const currentUser = getAuth().currentUser; try {
if (!currentUser) { return await auth.currentUser.getIdToken(true);
throw new Error('No user is currently logged in.'); } catch (error) {
throw new Error("No token retrieved.");
} }
return currentUser.uid;
} }
public static async setSubdomain(subdomain: string): Promise<ApiResponse> { public static async setSubdomain(subdomain: string): Promise<ApiResponse> {
const payload: SubdomainRequest = { token: this.token(), subdomain: subdomain }; const payload: SubdomainRequest = { subdomain: subdomain };
return this.call(serviiRequest.setSubdomain, payload); return this.call(serviiRequest.setSubdomain, payload, await this.token());
} }
public static async fetchServers(): Promise<ApiResponse> { public static async fetchServers(): Promise<ApiResponse> {
const payload: BaseRequest = { token: this.token() }; const payload: BaseRequest = { wait: "wtf" };
return this.call(serviiRequest.fetchServers, payload); return this.call(serviiRequest.fetchServers, payload, await this.token());
} }
public static async fetchLogs(name: string): Promise<ApiResponse> { public static async fetchLogs(name: string): Promise<ApiResponse> {
const payload: ServerRequest = { token: this.token(), name: name }; const payload: ServerRequest = { name: name };
return this.call(serviiRequest.fetchLogs, payload); return this.call(serviiRequest.fetchLogs, payload, await this.token());
} }
public static async fetchHistory(name: string): Promise<ApiResponse> { public static async fetchHistory(name: string): Promise<ApiResponse> {
const payload: ServerRequest = { token: this.token(), name: name }; const payload: ServerRequest = { name: name };
return this.call(serviiRequest.fetchHistory, payload); return this.call(serviiRequest.fetchHistory, payload, await this.token());
} }
public static async fetchPlayersStatus(name: string): Promise<ApiResponse> { public static async fetchPlayersStatus(name: string): Promise<ApiResponse> {
const payload: ServerRequest = { token: this.token(), name: name,}; const payload: ServerRequest = { name: name,};
return this.call(serviiRequest.fetchPlayersStatus, payload); return this.call(serviiRequest.fetchPlayersStatus, payload, await this.token());
} }
public static async accountCreate(): Promise<ApiResponse> { public static async accountCreate(): Promise<ApiResponse> {
const payload: BaseRequest = { token: this.token() }; const payload: BaseRequest = { token: this.token() };
return this.call(serviiRequest.accountCreate, payload); return this.call(serviiRequest.accountCreate, payload, await this.token());
} }
public static async serverCreate(name: string, version: string, framework: string): Promise<ApiResponse> { public static async serverCreate(name: string, version: string, framework: string): Promise<ApiResponse> {
const payload: ServerCreationRequest = { token: this.token(), name: name, version: version, framework: framework }; const payload: ServerCreationRequest = { name: name, version: version, framework: framework };
return this.call(serviiRequest.serverCreate, payload); return this.call(serviiRequest.serverCreate, payload, await this.token());
} }
public static async serverDelete(name: string): Promise<ApiResponse> { public static async serverDelete(name: string): Promise<ApiResponse> {
const payload: ServerRequest = { token: this.token(), name: name }; const payload: ServerRequest = { name: name };
return this.call(serviiRequest.serverDelete, payload); return this.call(serviiRequest.serverDelete, payload, await this.token());
} }
public static async accountDelete(): Promise<ApiResponse> { public static async accountDelete(): Promise<ApiResponse> {
const payload: BaseRequest = { token: this.token() }; const payload: BaseRequest = { token: this.token() };
return this.call(serviiRequest.accountDelete, payload); return this.call(serviiRequest.accountDelete, payload, await this.token());
} }
public static async serverRun(name: string): Promise<ApiResponse> { public static async serverRun(name: string): Promise<ApiResponse> {
const payload: ServerRequest = { token: this.token(), name: name }; const payload: ServerRequest = { name: name };
return this.call(serviiRequest.serverRun, payload); return this.call(serviiRequest.serverRun, payload, await this.token());
} }
public static async serverStop(name: string): Promise<ApiResponse> { public static async serverStop(name: string): Promise<ApiResponse> {
const payload: ServerRequest = { token: this.token(), name: name }; const payload: ServerRequest = { name: name };
return this.call(serviiRequest.serverStop, payload); return this.call(serviiRequest.serverStop, payload, await this.token());
} }
public static async updateProperties(name: string, props: [string, string][]): Promise<ApiResponse> { public static async updateProperties(name: string, props: [string, string][]): Promise<ApiResponse> {
const payload: UpdatePropertiesRequest = { const payload: UpdatePropertiesRequest = { name: name, props: props };
token: this.token(), return this.call(serviiRequest.updateProperty, payload, await this.token());
name: name,
props: props,
};
return this.call(serviiRequest.updateProperty, payload);
} }
public static async command(command: string, name: string): Promise<ApiResponse> { public static async command(command: string, name: string): Promise<ApiResponse> {
const payload: CommandRequest = { token: this.token(), command: command, name: name }; const payload: CommandRequest = { command: command, name: name };
return this.call(serviiRequest.command, payload); return this.call(serviiRequest.command, payload, await this.token());
} }
} }

View File

@ -37,4 +37,4 @@ const getUserSubdomain = async (userId) => {
} }
}; };
export { auth, googleProvider, signInWithPopup, getUserSubdomain }; export { auth, googleProvider, signInWithPopup, getUserSubdomain, app };