Env Update
This commit is contained in:
1 parent
f2b9993427
commit
9282777ac9
9 files changed
+60
-21
No files matched your search
@@ -1,7 +1,6 @@
|
||||
import { getAccessToken, setAccessToken } from './token-store';
|
||||
import { refreshRequest } from './api';
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3001/api/v1';
|
||||
import { getApiUrl } from '../config/runtime-config';
|
||||
|
||||
export class ApiError extends Error {
|
||||
status: number;
|
||||
@@ -14,7 +13,8 @@ export class ApiError extends Error {
|
||||
|
||||
async function rawFetch(path: string, init: RequestInit): Promise<Response> {
|
||||
const token = getAccessToken();
|
||||
return fetch(`${API_URL}${path}`, {
|
||||
const apiUrl = await getApiUrl();
|
||||
return fetch(`${apiUrl}${path}`, {
|
||||
...init,
|
||||
credentials: 'include',
|
||||
headers: {
|
||||
@@ -60,7 +60,8 @@ export async function apiFetchBlob(path: string, fallbackFilename: string): Prom
|
||||
|
||||
async function rawFetchFormData(path: string, formData: FormData): Promise<Response> {
|
||||
const token = getAccessToken();
|
||||
return fetch(`${API_URL}${path}`, {
|
||||
const apiUrl = await getApiUrl();
|
||||
return fetch(`${apiUrl}${path}`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
credentials: 'include',
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:3001/api/v1';
|
||||
import { getApiUrl } from '../config/runtime-config';
|
||||
|
||||
export interface AccessTokenResponse {
|
||||
accessToken: string;
|
||||
@@ -16,7 +16,8 @@ export function isMfaRequired(response: LoginResponse): response is MfaRequiredR
|
||||
}
|
||||
|
||||
export async function loginRequest(email: string, password: string): Promise<LoginResponse> {
|
||||
const response = await fetch(`${API_URL}/auth/login`, {
|
||||
const apiUrl = await getApiUrl();
|
||||
const response = await fetch(`${apiUrl}/auth/login`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -29,7 +30,8 @@ export async function loginRequest(email: string, password: string): Promise<Log
|
||||
}
|
||||
|
||||
export async function verifyMfaRequest(mfaToken: string, code: string): Promise<AccessTokenResponse> {
|
||||
const response = await fetch(`${API_URL}/auth/mfa/verify`, {
|
||||
const apiUrl = await getApiUrl();
|
||||
const response = await fetch(`${apiUrl}/auth/mfa/verify`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
@@ -50,7 +52,8 @@ export async function verifyMfaRequest(mfaToken: string, code: string): Promise<
|
||||
let inFlightRefresh: Promise<AccessTokenResponse | null> | null = null;
|
||||
|
||||
async function performRefresh(): Promise<AccessTokenResponse | null> {
|
||||
const response = await fetch(`${API_URL}/auth/refresh`, {
|
||||
const apiUrl = await getApiUrl();
|
||||
const response = await fetch(`${apiUrl}/auth/refresh`, {
|
||||
method: 'POST',
|
||||
credentials: 'include',
|
||||
});
|
||||
@@ -70,7 +73,8 @@ export function refreshRequest(): Promise<AccessTokenResponse | null> {
|
||||
}
|
||||
|
||||
export async function logoutRequest(): Promise<void> {
|
||||
await fetch(`${API_URL}/auth/logout`, { method: 'POST', credentials: 'include' });
|
||||
const apiUrl = await getApiUrl();
|
||||
await fetch(`${apiUrl}/auth/logout`, { method: 'POST', credentials: 'include' });
|
||||
}
|
||||
|
||||
export interface CurrentUserProfile {
|
||||
@@ -85,7 +89,8 @@ export interface CurrentUserProfile {
|
||||
}
|
||||
|
||||
export async function getCurrentUserRequest(accessToken: string): Promise<CurrentUserProfile> {
|
||||
const response = await fetch(`${API_URL}/auth/me`, {
|
||||
const apiUrl = await getApiUrl();
|
||||
const response = await fetch(`${apiUrl}/auth/me`, {
|
||||
credentials: 'include',
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { RuntimeConfig } from '@/app/api/runtime-config/route';
|
||||
|
||||
let cachedApiUrl: Promise<string> | null = null;
|
||||
|
||||
async function fetchApiUrl(): Promise<string> {
|
||||
const response = await fetch('/api/runtime-config');
|
||||
if (!response.ok) {
|
||||
throw new Error('Runtime-Konfiguration konnte nicht geladen werden.');
|
||||
}
|
||||
const config = (await response.json()) as RuntimeConfig;
|
||||
return config.apiUrl;
|
||||
}
|
||||
|
||||
export function getApiUrl(): Promise<string> {
|
||||
if (!cachedApiUrl) {
|
||||
cachedApiUrl = fetchApiUrl().catch((error: unknown) => {
|
||||
cachedApiUrl = null;
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
return cachedApiUrl;
|
||||
}
|
||||
Reference in new issue
Block a user