Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7999b02e8 | ||
|
|
4bc74f26d1 | ||
|
|
e6b7ec7401 | ||
|
|
373adcb49d |
7
src/app/logout/route.ts
Normal file
7
src/app/logout/route.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { logout } from '@/lib/auth/server';
|
||||
import { redirect, RedirectType } from 'next/navigation';
|
||||
|
||||
export async function GET() {
|
||||
await logout();
|
||||
redirect('/login', RedirectType.replace);
|
||||
}
|
||||
@@ -7,6 +7,6 @@ export default async function HomePage() {
|
||||
if (session.token && session.user) {
|
||||
redirect('/dashboard');
|
||||
} else {
|
||||
redirect('/login');
|
||||
redirect('/logout');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use server';
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
import { redirect, RedirectType } from 'next/navigation';
|
||||
import { loginUser } from '@/lib/api/server/auth';
|
||||
import { login, logout } from '@/lib/auth/server';
|
||||
|
||||
@@ -42,6 +42,5 @@ export async function loginAction(prevState: LoginResult, formData: FormData) {
|
||||
}
|
||||
|
||||
export async function logoutAction() {
|
||||
await logout();
|
||||
redirect('/login');
|
||||
redirect('/logout');
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ export async function loginUser(username: string, password: string) {
|
||||
return { token, user: userResponse };
|
||||
}
|
||||
|
||||
export async function getCurrentUser(token: string) {
|
||||
return fetchServerAPI<User>(`${authRoute}/me`, token);
|
||||
export async function getCurrentUser(token: string): Promise<User> {
|
||||
const response = await fetchServerAPI<User>(`${authRoute}/me`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
import { logout } from '@/lib/auth/server';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
const BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
type ApiResponse<T> = {
|
||||
data?: T;
|
||||
error?: string;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
export async function fetchServerAPI<T>(
|
||||
endpoint: string,
|
||||
token: string,
|
||||
method: string = 'GET',
|
||||
body?: object
|
||||
): Promise<T> {
|
||||
): Promise<ApiResponse<T>> {
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`
|
||||
@@ -22,8 +27,8 @@ export async function fetchServerAPI<T>(
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status == 401) {
|
||||
await logout();
|
||||
redirect('/login');
|
||||
redirect('/logout');
|
||||
return { error: 'unauthorized' };
|
||||
}
|
||||
throw new Error(
|
||||
`API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint} - ${token}`
|
||||
@@ -31,8 +36,8 @@ export async function fetchServerAPI<T>(
|
||||
}
|
||||
|
||||
if (response.headers.get('Content-Type')?.includes('application/json')) {
|
||||
return response.json();
|
||||
return { data: await response.json() };
|
||||
}
|
||||
|
||||
return response.text() as T;
|
||||
return { message: await response.text() };
|
||||
}
|
||||
|
||||
@@ -6,7 +6,8 @@ import type {
|
||||
EventConfig,
|
||||
EventRules,
|
||||
ServerSettings,
|
||||
ConfigFile
|
||||
ConfigFile,
|
||||
Config
|
||||
} from '@/lib/types/config';
|
||||
|
||||
const serverRoute = '/server';
|
||||
@@ -15,22 +16,27 @@ export async function getServerConfigurations(
|
||||
token: string,
|
||||
serverId: string
|
||||
): Promise<Configurations> {
|
||||
return fetchServerAPI<Configurations>(`${serverRoute}/${serverId}/config`, token);
|
||||
const response = await fetchServerAPI<Configurations>(`${serverRoute}/${serverId}/config`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function getServerConfiguration(
|
||||
token: string,
|
||||
serverId: string,
|
||||
configType: ConfigFile
|
||||
): Promise<Configuration | AssistRules | EventConfig | EventRules | ServerSettings> {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/config/${configType}`, token);
|
||||
): Promise<Config> {
|
||||
const response = await fetchServerAPI<Config>(
|
||||
`${serverRoute}/${serverId}/config/${configType}`,
|
||||
token
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function updateServerConfiguration(
|
||||
token: string,
|
||||
serverId: string,
|
||||
configType: ConfigFile,
|
||||
config: Configuration | AssistRules | EventConfig | EventRules | ServerSettings,
|
||||
config: Config,
|
||||
restart = false
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/config/${configType}`, token, 'PUT', {
|
||||
|
||||
@@ -4,21 +4,29 @@ import { Track, CarModel, CupCategory, DriverCategory, SessionType } from '@/lib
|
||||
const lookupRoute = '/lookup';
|
||||
|
||||
export async function getTracks(token: string): Promise<Track[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/tracks`, token);
|
||||
const response = await fetchServerAPI<Track[]>(`${lookupRoute}/tracks`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function getCarModels(token: string): Promise<CarModel[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/car-models`, token);
|
||||
const response = await fetchServerAPI<CarModel[]>(`${lookupRoute}/car-models`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function getCupCategories(token: string): Promise<CupCategory[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/cup-categories`, token);
|
||||
const response = await fetchServerAPI<CupCategory[]>(`${lookupRoute}/cup-categories`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function getDriverCategories(token: string): Promise<DriverCategory[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/driver-categories`, token);
|
||||
const response = await fetchServerAPI<DriverCategory[]>(
|
||||
`${lookupRoute}/driver-categories`,
|
||||
token
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function getSessionTypes(token: string): Promise<SessionType[]> {
|
||||
return fetchServerAPI(`${lookupRoute}/session-types`, token);
|
||||
const response = await fetchServerAPI<SessionType[]>(`${lookupRoute}/session-types`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
@@ -24,28 +24,35 @@ export async function getUsers(token: string, params: UserListParams = {}): Prom
|
||||
const queryString = searchParams.toString();
|
||||
const endpoint = `${membershipRoute}${queryString ? `?${queryString}` : ''}`;
|
||||
|
||||
return fetchServerAPI(endpoint, token);
|
||||
const response = await fetchServerAPI<User[]>(endpoint, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function createUser(
|
||||
token: string,
|
||||
userData: { username: string; password: string; role: string }
|
||||
) {
|
||||
return fetchServerAPI(membershipRoute, token, 'POST', userData);
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(membershipRoute, token, 'POST', userData);
|
||||
}
|
||||
|
||||
export async function getUserById(token: string, userId: string): Promise<User> {
|
||||
return fetchServerAPI(`${membershipRoute}/${userId}`, token);
|
||||
const response = await fetchServerAPI<User>(`${membershipRoute}/${userId}`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function updateUser(token: string, userId: string, userData: Partial<User>) {
|
||||
return fetchServerAPI(`${membershipRoute}/${userId}`, token, 'PUT', userData);
|
||||
export async function updateUser(
|
||||
token: string,
|
||||
userId: string,
|
||||
userData: Partial<User>
|
||||
): Promise<void> {
|
||||
await fetchServerAPI(`${membershipRoute}/${userId}`, token, 'PUT', userData);
|
||||
}
|
||||
|
||||
export async function deleteUser(token: string, userId: string) {
|
||||
return fetchServerAPI(`${membershipRoute}/${userId}`, token, 'DELETE');
|
||||
export async function deleteUser(token: string, userId: string): Promise<void> {
|
||||
await fetchServerAPI(`${membershipRoute}/${userId}`, token, 'DELETE');
|
||||
}
|
||||
|
||||
export async function getRoles(token: string): Promise<Role[]> {
|
||||
return fetchServerAPI(`${membershipRoute}/roles`, token);
|
||||
const response = await fetchServerAPI<Role[]>(`${membershipRoute}/roles`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
@@ -1,30 +1,31 @@
|
||||
import { fetchServerAPI } from './base';
|
||||
import { Server } from '@/lib/types/server';
|
||||
import { Server, ServiceStatus } from '@/lib/types/server';
|
||||
|
||||
const serverRoute = '/server';
|
||||
|
||||
export async function getServers(token: string): Promise<Server[]> {
|
||||
const response = await fetchServerAPI<Server[]>(serverRoute, token);
|
||||
return response;
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function getServer(token: string, serverId: string): Promise<Server> {
|
||||
const response = await fetchServerAPI<Server>(`${serverRoute}/${serverId}`, token);
|
||||
return response;
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
export async function restartService(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service/restart`, token, 'POST');
|
||||
export async function restartService(token: string, serverId: string): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/service/restart`, token, 'POST');
|
||||
}
|
||||
|
||||
export async function startService(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service/start`, token, 'POST');
|
||||
export async function startService(token: string, serverId: string): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/service/start`, token, 'POST');
|
||||
}
|
||||
|
||||
export async function stopService(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service/stop`, token, 'POST');
|
||||
export async function stopService(token: string, serverId: string): Promise<void> {
|
||||
await fetchServerAPI(`${serverRoute}/${serverId}/service/stop`, token, 'POST');
|
||||
}
|
||||
|
||||
export async function getServiceStatus(token: string, serverId: string) {
|
||||
return fetchServerAPI(`${serverRoute}/${serverId}/service`, token);
|
||||
export async function getServiceStatus(token: string, serverId: string): Promise<ServiceStatus> {
|
||||
const response = await fetchServerAPI<ServiceStatus>(`${serverRoute}/${serverId}/service`, token);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@ export async function getServerStatistics(
|
||||
startDate: string,
|
||||
endDate: string
|
||||
): Promise<StateHistoryStats> {
|
||||
return fetchServerAPI<StateHistoryStats>(
|
||||
const response = await fetchServerAPI<StateHistoryStats>(
|
||||
`${serverRoute}/${serverId}/state-history/statistics?start_date=${startDate}&end_date=${endDate}`,
|
||||
token
|
||||
);
|
||||
return response.data!;
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ export async function requireAuth() {
|
||||
const session = await getSession();
|
||||
|
||||
if (!session.token || !session.user) {
|
||||
redirect('/login');
|
||||
redirect('/logout');
|
||||
}
|
||||
|
||||
return session;
|
||||
|
||||
Reference in New Issue
Block a user