Websocket auth, pass access token in gke configs

This commit is contained in:
Hristo 2024-05-10 19:32:35 -04:00
parent 4e20c4ac56
commit c56a058a74
6 changed files with 49 additions and 11 deletions

View file

@ -7,6 +7,7 @@ import Chat from './Chat';
import EmptyChat from './EmptyChat';
import { toast } from 'sonner';
import { clientFetch } from '@/lib/utils';
import { getAccessKey } from '@/lib/config';
export type Message = {
id: string;
@ -98,7 +99,14 @@ const useSocket = (url: string) => {
wsURL.search = searchParams.toString();
const ws = new WebSocket(wsURL.toString());
let protocols: any[] = [];
const secretToken = getAccessKey();
if (secretToken) {
protocols = ["Authorization", `${secretToken}`];
};
const ws = new WebSocket(wsURL.toString(), protocols);
ws.onopen = () => {
console.log('[DEBUG] open');

View file

@ -24,14 +24,14 @@ export const formatTimeDifference = (date1: Date, date2: Date): string => {
export const clientFetch = async (path: string, payload: any): Promise<any> => {
let headers = payload.headers;
const url = `${getBackendURL()}${path}`;
const secret_token = getAccessKey();
const secretToken = getAccessKey();
if (secret_token) {
if (secretToken) {
if (headers == null) {
headers = {};
};
headers['Authorization'] = `Bearer ${secret_token}`;
headers['Authorization'] = `Bearer ${secretToken}`;
payload.headers = headers;
};