TomD
TomD
WWasp-lang
Created by TomD on 6/25/2024 in #đŸ™‹questions
use auth when requesting a custom api without the api wrapper
Hello ! I'm having a small trouble, I've seen the example here to implement streaming capabilities. https://github.com/wasp-lang/wasp/tree/main/examples/streaming/src/client I tried to do the same thing and it is working. I am now adding authentification and I can't make it work, indeed since I'm using "fetch" and not the API wrapper I don't get the context.user to do auth. The problem, when I try to access "context.user" it doesn't exist and I cannot make it work. Do you have any ideas ? See the code below.
export async function fetchChatResponseStream(
apiUrl: string,
path: string,
onData: (chunk: string) => void,
controller: AbortController,
method: string = 'POST',
requestBody: ChatResponseStreamBody
) {
const response = await fetch(apiUrl + path, {
method: method,
signal: controller.signal,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});

const reader = response.body?.pipeThrough(new TextDecoderStream()).getReader();
while (reader) {
const { done, value } = await reader.read();
if (done) {
return;
}
onData(value.toString());
}
}
export async function fetchChatResponseStream(
apiUrl: string,
path: string,
onData: (chunk: string) => void,
controller: AbortController,
method: string = 'POST',
requestBody: ChatResponseStreamBody
) {
const response = await fetch(apiUrl + path, {
method: method,
signal: controller.signal,
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});

const reader = response.body?.pipeThrough(new TextDecoderStream()).getReader();
while (reader) {
const { done, value } = await reader.read();
if (done) {
return;
}
onData(value.toString());
}
}
export const chatResponse: ChatResponse = async (request, response, context) => {
if (!context.user) {
response.status(401).json({ error: "Unauthorized" });
return;
}
};
export const chatResponse: ChatResponse = async (request, response, context) => {
if (!context.user) {
response.status(401).json({ error: "Unauthorized" });
return;
}
};
14 replies