Skip to content

Commit 1c71d3e

Browse files
committed
feat: rename frontend env var and add auth header
1 parent b6f0db4 commit 1c71d3e

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

apps/frontend/.env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Replace with the corresponding url, credentials and endpoint
2-
NEXT_PUBLIC_API_URL="http://localhost:8080/"
2+
NEXT_PUBLIC_QUESTION_SERVICE_URL="http://localhost:8080/"
33
NEXT_PUBLIC_USER_SERVICE_URL="http://localhost:3001/"

apps/frontend/Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
FROM node:18-alpine AS base
22

3-
# For now we can specify it here, else we can use --build-arg in our docker build command
4-
ARG NEXT_PUBLIC_API_URL
5-
ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL}
6-
73
# Install dependencies only when needed
84
FROM base AS deps
95
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.

apps/frontend/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pnpm install
2323
Then, follow the `.env.example` file and create a `.env` file in the current directory. Replace the necessary values within.
2424

2525
```bash
26-
NEXT_PUBLIC_API_URL=http://localhost:8080
26+
NEXT_PUBLIC_QUESTION_SERVICE_URL=http://localhost:8080
2727
```
2828

2929
First, run the development server:
@@ -42,7 +42,7 @@ You can start editing the page by modifying `app/page.tsx`. The page auto-update
4242
# Navigate to the frontend app directory
4343
cd apps/frontend
4444
# Build dockerfile (Ensure that your docker daemon is running beforehand)
45-
docker build -t frontend --build-arg NEXT_PUBLIC_API_URL="http://localhost:8080/" -f Dockerfile .
45+
docker build -t frontend -e NEXT_PUBLIC_QUESTION_SERVICE_URL="http://localhost:8080/" -f Dockerfile .
4646
```
4747

4848
Run the backend server locally and visit http://localhost:3000/ to see the frontend application working

apps/frontend/src/app/services/question.ts

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { getToken } from "./login-store";
2+
3+
const QUESTION_SERVICE_URL = process.env.NEXT_PUBLIC_QUESTION_SERVICE_URL;
4+
15
export interface Question {
26
id: number;
37
docRefId: string;
@@ -37,7 +41,7 @@ export const GetQuestions = async (
3741
categories?: string[],
3842
title?: string
3943
): Promise<QuestionListResponse> => {
40-
let query_url = `${process.env.NEXT_PUBLIC_API_URL}questions`;
44+
let query_url = `${QUESTION_SERVICE_URL}questions`;
4145
let query_params = "";
4246

4347
if (currentPage) {
@@ -73,15 +77,26 @@ export const GetQuestions = async (
7377
}
7478

7579
query_url += query_params;
76-
const response = await fetch(query_url);
80+
const response = await fetch(query_url, {
81+
method: "GET",
82+
headers: {
83+
'Authorization': `Bearer ${getToken()}`,
84+
}
85+
});
7786
const data = await response.json();
7887
return data;
7988
};
8089

8190
// Get single question
8291
export const GetSingleQuestion = async (docRef: string): Promise<Question> => {
8392
const response = await fetch(
84-
`${process.env.NEXT_PUBLIC_API_URL}questions/${docRef}`
93+
`${QUESTION_SERVICE_URL}questions/${docRef}`,
94+
{
95+
method: "GET",
96+
headers: {
97+
'Authorization': `Bearer ${getToken()}`,
98+
}
99+
}
85100
);
86101
const data = await response.json();
87102
return data;
@@ -91,10 +106,11 @@ export const GetSingleQuestion = async (docRef: string): Promise<Question> => {
91106
export const CreateQuestion = async (
92107
question: NewQuestion
93108
): Promise<Question> => {
94-
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}questions`, {
109+
const response = await fetch(`${QUESTION_SERVICE_URL}questions`, {
95110
method: "POST",
96111
headers: {
97112
"Content-Type": "application/json",
113+
'Authorization': `Bearer ${getToken()}`,
98114
},
99115
body: JSON.stringify(question),
100116
});
@@ -113,11 +129,12 @@ export const EditQuestion = async (
113129
docRefId: string
114130
): Promise<Question> => {
115131
const response = await fetch(
116-
`${process.env.NEXT_PUBLIC_API_URL}questions/${docRefId}`,
132+
`${QUESTION_SERVICE_URL}questions/${docRefId}`,
117133
{
118134
method: "PUT",
119135
headers: {
120136
"Content-Type": "application/json",
137+
'Authorization': `Bearer ${getToken()}`,
121138
},
122139
body: JSON.stringify(question),
123140
}
@@ -135,9 +152,12 @@ export const EditQuestion = async (
135152
// Delete single question (TODO: Ryan)
136153
export async function DeleteQuestion(docRef: String): Promise<void> {
137154
const res = await fetch(
138-
`${process.env.NEXT_PUBLIC_API_URL}questions/${docRef}`,
155+
`${QUESTION_SERVICE_URL}questions/${docRef}`,
139156
{
140157
method: "DELETE",
158+
headers: {
159+
'Authorization': `Bearer ${getToken()}`,
160+
},
141161
}
142162
);
143163
// error handling later

0 commit comments

Comments
 (0)