1
1
"use server" ;
2
2
3
+ import { getAccessToken } from "@/lib/auth" ;
3
4
import { CategoriesResponse , CategoriesResponseSchema } from "@/types/Category" ;
4
5
import {
5
6
Question ,
@@ -17,12 +18,15 @@ import { cache } from "react";
17
18
18
19
export async function getQuestion ( slug : string ) : Promise < QuestionResponse > {
19
20
try {
21
+ const access_token = await getAccessToken ( ) ;
22
+
20
23
const res = await fetch (
21
24
process . env . PUBLIC_API_URL + `/api/questions/${ slug } ` ,
22
25
{
23
26
method : "GET" ,
24
27
headers : {
25
28
"Content-Type" : "application/json" ,
29
+ Authorization : `Bearer ${ access_token } ` ,
26
30
} ,
27
31
}
28
32
) ;
@@ -44,13 +48,16 @@ export async function getQuestions(): Promise<QuestionsResponse> {
44
48
} ) ;
45
49
46
50
try {
51
+ const access_token = await getAccessToken ( ) ;
52
+
47
53
const res : Response = await fetch (
48
54
process . env . PUBLIC_API_URL + `/api/questions?${ query } ` ,
49
55
{
50
56
cache : "no-cache" ,
51
57
method : "GET" ,
52
58
headers : {
53
59
"Content-Type" : "application/json" ,
60
+ Authorization : `Bearer ${ access_token } ` ,
54
61
} ,
55
62
}
56
63
) ;
@@ -69,13 +76,16 @@ export async function getQuestions(): Promise<QuestionsResponse> {
69
76
export const getQuestionCategories = cache (
70
77
async function ( ) : Promise < CategoriesResponse > {
71
78
try {
79
+ const access_token = await getAccessToken ( ) ;
80
+
72
81
const res : Response = await fetch (
73
82
process . env . PUBLIC_API_URL + `/api/questions/categories` ,
74
83
{
75
84
cache : "no-cache" ,
76
85
method : "GET" ,
77
86
headers : {
78
87
"Content-Type" : "application/json" ,
88
+ Authorization : `Bearer ${ access_token } ` ,
79
89
} ,
80
90
}
81
91
) ;
@@ -96,12 +106,15 @@ export async function createQuestion(
96
106
question : NewQuestion
97
107
) : Promise < QuestionResponse > {
98
108
try {
109
+ const access_token = await getAccessToken ( ) ;
110
+
99
111
const res = await fetch (
100
112
process . env . PUBLIC_API_URL + "/api/questions/create" ,
101
113
{
102
114
method : "POST" ,
103
115
headers : {
104
116
"Content-Type" : "application/json" ,
117
+ Authorization : `Bearer ${ access_token } ` ,
105
118
} ,
106
119
body : JSON . stringify ( question ) ,
107
120
}
@@ -122,10 +135,13 @@ export async function createQuestion(
122
135
123
136
export async function deleteQuestion ( questionId : string ) : Promise < void > {
124
137
try {
138
+ const access_token = await getAccessToken ( ) ;
139
+
125
140
await fetch ( process . env . PUBLIC_API_URL + `/api/questions/${ questionId } ` , {
126
141
method : "DELETE" ,
127
142
headers : {
128
143
"Content-Type" : "application/json" ,
144
+ Authorization : `Bearer ${ access_token } ` ,
129
145
} ,
130
146
} ) ;
131
147
@@ -137,13 +153,16 @@ export async function editQuestion(
137
153
question : Question
138
154
) : Promise < QuestionResponse > {
139
155
try {
156
+ const access_token = await getAccessToken ( ) ;
157
+
140
158
const updatedQuestion = NewQuestionSchema . parse ( question ) ;
141
159
const res = await fetch (
142
160
process . env . PUBLIC_API_URL + `/api/questions/${ question . _id } ` ,
143
161
{
144
162
method : "PATCH" ,
145
163
headers : {
146
164
"Content-Type" : "application/json" ,
165
+ Authorization : `Bearer ${ access_token } ` ,
147
166
} ,
148
167
body : JSON . stringify ( updatedQuestion ) ,
149
168
}
0 commit comments