Skip to content

Commit cdb9c25

Browse files
committed
Allow for base uri to change
1 parent 1c9ed32 commit cdb9c25

File tree

11 files changed

+134
-98
lines changed

11 files changed

+134
-98
lines changed

frontend/app/auth/auth-context.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
3333
// Login using locally stored JWT token
3434
useEffect(() => {
3535
if (token) {
36-
fetch(`${userServiceUri}/auth/verify-token`, {
36+
fetch(`${userServiceUri(window.location.hostname)}/auth/verify-token`, {
3737
method: "GET",
3838
headers: {
3939
Authorization: `Bearer ${token}`,
@@ -52,16 +52,19 @@ const AuthProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
5252

5353
// Login using email and password
5454
const login = async (email: string, password: string): Promise<User> => {
55-
const response = await fetch(`${userServiceUri}/auth/login`, {
56-
method: "POST",
57-
headers: {
58-
"Content-Type": "application/json",
59-
},
60-
body: JSON.stringify({
61-
email,
62-
password,
63-
}),
64-
});
55+
const response = await fetch(
56+
`${userServiceUri(window.location.hostname)}/auth/login`,
57+
{
58+
method: "POST",
59+
headers: {
60+
"Content-Type": "application/json",
61+
},
62+
body: JSON.stringify({
63+
email,
64+
password,
65+
}),
66+
}
67+
);
6568

6669
if (!response.ok) {
6770
throw new Error("Not OK");

frontend/components/admin-user-management/admin-user-management.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default function AdminUserManagement() {
4444
const auth = useAuth();
4545

4646
const { data, isLoading, mutate } = useSWR(
47-
`${userServiceUri}/users`,
47+
`${userServiceUri(window.location.hostname)}/users`,
4848
fetcher
4949
);
5050

@@ -68,12 +68,15 @@ export default function AdminUserManagement() {
6868
throw new Error("No authentication token found");
6969
}
7070

71-
const response = await fetch(`${userServiceUri}/users/${userId}`, {
72-
method: "DELETE",
73-
headers: {
74-
Authorization: `Bearer ${token}`,
75-
},
76-
});
71+
const response = await fetch(
72+
`${userServiceUri(window.location.hostname)}/users/${userId}`,
73+
{
74+
method: "DELETE",
75+
headers: {
76+
Authorization: `Bearer ${token}`,
77+
},
78+
}
79+
);
7780

7881
if (!response.ok) {
7982
throw new Error("Failed to delete user");

frontend/components/forget-password.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ const ForgetPassword: React.FC = () => {
3535
}
3636

3737
try {
38-
const response = await fetch(`${userServiceUri}/users/forget-password`, {
39-
method: "POST",
40-
headers: {
41-
"Content-Type": "application/json",
42-
},
43-
body: JSON.stringify({ email }),
44-
});
38+
const response = await fetch(
39+
`${userServiceUri(window.location.hostname)}/users/forget-password`,
40+
{
41+
method: "POST",
42+
headers: {
43+
"Content-Type": "application/json",
44+
},
45+
body: JSON.stringify({ email }),
46+
}
47+
);
4548

4649
if (!response.ok) {
4750
const errorData = await response.json();

frontend/components/questions/questions-listing.tsx

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default function QuestionListing() {
5656
const [search, setSearch] = useState(searchParams.get("search") || "");
5757

5858
const { data, isLoading, mutate } = useSWR(
59-
`${questionServiceUri}/questions?category=${encodeURIComponent(category)}&complexity=${encodeURIComponent(complexity)}&search=${encodeURIComponent(search)}`,
59+
`${questionServiceUri(window.location.hostname)}/questions?category=${encodeURIComponent(category)}&complexity=${encodeURIComponent(complexity)}&search=${encodeURIComponent(search)}`,
6060
fetcher,
6161
{
6262
keepPreviousData: true,
@@ -145,7 +145,7 @@ export default function QuestionListing() {
145145
try {
146146
const token = localStorage.getItem("jwtToken");
147147
const response = await fetch(
148-
`${questionServiceUri}/questions/batch-upload`,
148+
`${questionServiceUri(window.location.hostname)}/questions/batch-upload`,
149149
{
150150
method: "POST",
151151
headers: {
@@ -190,7 +190,7 @@ export default function QuestionListing() {
190190

191191
try {
192192
const response = await fetch(
193-
`${questionServiceUri}/questions/${selectedQuestion.id}`,
193+
`${questionServiceUri(window.location.hostname)}/questions/${selectedQuestion.id}`,
194194
{
195195
method: "DELETE",
196196
}
@@ -263,18 +263,21 @@ export default function QuestionListing() {
263263

264264
const handleCreate = async (newQuestion: Question) => {
265265
try {
266-
const response = await fetch(`${questionServiceUri}/questions`, {
267-
method: "POST",
268-
headers: {
269-
"Content-Type": "application/json",
270-
},
271-
body: JSON.stringify({
272-
title: newQuestion.title,
273-
description: newQuestion.description,
274-
category: newQuestion.category,
275-
complexity: newQuestion.complexity,
276-
}),
277-
});
266+
const response = await fetch(
267+
`${questionServiceUri(window.location.hostname)}/questions`,
268+
{
269+
method: "POST",
270+
headers: {
271+
"Content-Type": "application/json",
272+
},
273+
body: JSON.stringify({
274+
title: newQuestion.title,
275+
description: newQuestion.description,
276+
category: newQuestion.category,
277+
complexity: newQuestion.complexity,
278+
}),
279+
}
280+
);
278281

279282
if (!response.ok) {
280283
if (response.status == 409) {

frontend/components/user-settings/user-settings.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default function UserSettings({ userId }: { userId: string }) {
5555
const fileInputRef = useRef<HTMLInputElement>(null);
5656

5757
const { data, error, isLoading, mutate } = useSWR(
58-
`${userServiceUri}/users/${userId}`,
58+
`${userServiceUri(window.location.hostname)}/users/${userId}`,
5959
fetcher
6060
);
6161
const [user, setUser] = useState<User | null>(null);
@@ -140,14 +140,17 @@ export default function UserSettings({ userId }: { userId: string }) {
140140
}
141141

142142
try {
143-
const response = await fetch(`${userServiceUri}/users/${userId}`, {
144-
method: "PATCH",
145-
headers: {
146-
Authorization: `Bearer ${token}`,
147-
"Content-Type": "application/json",
148-
},
149-
body: JSON.stringify(user),
150-
});
143+
const response = await fetch(
144+
`${userServiceUri(window.location.hostname)}/users/${userId}`,
145+
{
146+
method: "PATCH",
147+
headers: {
148+
Authorization: `Bearer ${token}`,
149+
"Content-Type": "application/json",
150+
},
151+
body: JSON.stringify(user),
152+
}
153+
);
151154
if (!response.ok) {
152155
throw new Error("Failed to save changes");
153156
} else {
@@ -178,13 +181,16 @@ export default function UserSettings({ userId }: { userId: string }) {
178181
}
179182

180183
try {
181-
const response = await fetch(`${userServiceUri}/users/${userId}`, {
182-
method: "DELETE",
183-
headers: {
184-
Authorization: `Bearer ${token}`,
185-
"Content-Type": "application/json",
186-
},
187-
});
184+
const response = await fetch(
185+
`${userServiceUri(window.location.hostname)}/users/${userId}`,
186+
{
187+
method: "DELETE",
188+
headers: {
189+
Authorization: `Bearer ${token}`,
190+
"Content-Type": "application/json",
191+
},
192+
}
193+
);
188194
if (!response.ok) throw new Error("Failed to delete account");
189195

190196
console.log("Account deleted successfully!");
@@ -238,7 +244,7 @@ export default function UserSettings({ userId }: { userId: string }) {
238244

239245
try {
240246
const response = await fetch(
241-
`${userServiceUri}/users/${userId}/change-password`,
247+
`${userServiceUri(window.location.hostname)}/users/${userId}/change-password`,
242248
{
243249
method: "PATCH",
244250
headers: {

frontend/lib/api-uri.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
export const userServiceUri: string = `http://host.docker.internal:${process.env.NEXT_PUBLIC_USER_SVC_PORT}`;
2-
export const questionServiceUri: string = `http://host.docker.internal:${process.env.NEXT_PUBLIC_QUESTION_SVC_PORT}`;
3-
export const matchingServiceUri: string = `http://host.docker.internal:${process.env.NEXT_PUBLIC_MATCHING_SVC_PORT}`;
1+
const constructUri = (baseUri: string, port: string | undefined) =>
2+
`http://${process.env.NEXT_PUBLIC_BASE_URI || baseUri}:${port}`;
3+
4+
export const userServiceUri: (baseUri: string) => string = (baseUri) =>
5+
constructUri(baseUri, process.env.NEXT_PUBLIC_USER_SVC_PORT);
6+
export const questionServiceUri: (baseUri: string) => string = (baseUri) =>
7+
constructUri(baseUri, process.env.NEXT_PUBLIC_QUESTION_SVC_PORT);
8+
export const matchingServiceUri: (baseUri: string) => string = (baseUri) =>
9+
constructUri(baseUri, process.env.NEXT_PUBLIC_MATCHING_SVC_PORT);

frontend/lib/reset-password.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { userServiceUri } from "@/lib/api-uri";
22

33
export const resetPassword = async (token: string, password: string) => {
4-
const response = await fetch(`${userServiceUri}/users/reset-password`, {
5-
method: "POST",
6-
headers: {
7-
"Content-Type": "application/json",
8-
},
9-
body: JSON.stringify({
10-
token,
11-
password,
12-
}),
13-
});
4+
const response = await fetch(
5+
`${userServiceUri(window.location.hostname)}/users/reset-password`,
6+
{
7+
method: "POST",
8+
headers: {
9+
"Content-Type": "application/json",
10+
},
11+
body: JSON.stringify({
12+
token,
13+
password,
14+
}),
15+
}
16+
);
1417
return response;
1518
};

frontend/lib/signup.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@ export const signUp = async (
55
email: string,
66
password: string
77
) => {
8-
const response = await fetch(`${userServiceUri}/users`, {
9-
method: "POST",
10-
headers: {
11-
"Content-Type": "application/json",
12-
},
13-
body: JSON.stringify({
14-
username,
15-
email,
16-
password,
17-
}),
18-
});
8+
const response = await fetch(
9+
`${userServiceUri(window.location.hostname)}/users`,
10+
{
11+
method: "POST",
12+
headers: {
13+
"Content-Type": "application/json",
14+
},
15+
body: JSON.stringify({
16+
username,
17+
email,
18+
password,
19+
}),
20+
}
21+
);
1922
return response;
2023
};

frontend/lib/update-question.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Question } from "@/lib/schemas/question-schema";
33

44
export const updateQuestion = async (question: Question) => {
55
const response = await fetch(
6-
`${questionServiceUri}/questions/${question.id}`,
6+
`${questionServiceUri(window.location.hostname)}/questions/${question.id}`,
77
{
88
method: "PUT",
99
headers: {

frontend/lib/update-user-privilege.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@ export const updateUserPrivilege = async (
77
) => {
88
const body = { isAdmin };
99

10-
const response = await fetch(`${userServiceUri}/users/${id}/privilege`, {
11-
method: "PATCH",
12-
headers: {
13-
Authorization: `Bearer ${jwtToken}`,
14-
"Content-Type": "application/json",
15-
},
16-
body: JSON.stringify(body),
17-
});
10+
const response = await fetch(
11+
`${userServiceUri(window.location.hostname)}/users/${id}/privilege`,
12+
{
13+
method: "PATCH",
14+
headers: {
15+
Authorization: `Bearer ${jwtToken}`,
16+
"Content-Type": "application/json",
17+
},
18+
body: JSON.stringify(body),
19+
}
20+
);
1821
return response;
1922
};

0 commit comments

Comments
 (0)