Skip to content

Commit

Permalink
Adds jwt header to chat requests (#3452)
Browse files Browse the repository at this point in the history
* add jwt as optional parameter

* Updated request headers

* Encapsulation
  • Loading branch information
hkirat authored Mar 27, 2023
1 parent 3052424 commit e0ea8f4
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 23 deletions.
13 changes: 10 additions & 3 deletions packages/db/src/api/friendships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import {
updateFriendship,
} from "../db/friends";

export const refreshFriendships = async (uuid: string) => {
export const refreshFriendships = async (uuid: string, jwt?: string) => {
const db = getDb(uuid);
try {
const res = await fetch(`${BACKEND_API_URL}/inbox/all?uuid=${uuid}`);
const res = await fetch(`${BACKEND_API_URL}/inbox/all?uuid=${uuid}`, {
headers: {
Authorization: `Bearer ${jwt}`,
},
});
const json = await res.json();
const chats: EnrichedInboxDb[] = json.chats;
if (!chats) {
Expand All @@ -34,10 +38,13 @@ export const refreshFriendships = async (uuid: string) => {
}
};

export const refreshGroups = async (uuid: string) => {
export const refreshGroups = async (uuid: string, jwt?: string) => {
try {
const response = await fetch(`${BACKEND_API_URL}/nft/bulk?uuid=${uuid}`, {
method: "GET",
headers: {
Authorization: `Bearer ${jwt}`,
},
});

const res = await response.json();
Expand Down
7 changes: 6 additions & 1 deletion packages/tamagui-core/src/chat/AuthenticatedSync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from "@coral-xyz/recoil";
import { useRecoilCallback, useSetRecoilState } from "recoil";

import { getAuthHeader } from "./getAuthHeader";
import {
BackgroundChatsSync,
refreshGroupsAndFriendships,
Expand Down Expand Up @@ -47,7 +48,7 @@ export const ChatSync = ({ uuid, jwt }: { uuid: string; jwt: string }) => {
useEffect(() => {
(async () => {
await Promise.all([
refreshGroupsAndFriendships(uuid).then(
refreshGroupsAndFriendships(uuid, jwt).then(
async () => await BackgroundChatsSync.getInstance().updateUuid(uuid)
),
SignalingManager.getInstance().updateUuid(uuid, jwt),
Expand All @@ -62,6 +63,7 @@ export const DbRecoilSync = ({ uuid }: { uuid: string }) => {
const updateChats = useUpdateChats();
const updateNotifications = useUpdateNotifications();
const updateUsers = useUpdateUsers();
const authenticatedUser = useAuthenticatedUser();

const setFriendshipsValue = useSetRecoilState(friendships({ uuid }));
const setRequestCountValue = useSetRecoilState(requestCount({ uuid }));
Expand All @@ -76,6 +78,9 @@ export const DbRecoilSync = ({ uuid }: { uuid: string }) => {
`${BACKEND_API_URL}/notifications/unreadCount`,
{
method: "GET",
headers: {
...getAuthHeader(authenticatedUser?.jwt),
},
}
);
try {
Expand Down
17 changes: 9 additions & 8 deletions packages/tamagui-core/src/chat/fetchMoreChatsFor.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import type {
MessageWithMetadata,
SubscriptionType} from "@coral-xyz/common";
import {
BACKEND_API_URL
} from "@coral-xyz/common";
import { bulkAddChats,oldestReceivedMessage } from "@coral-xyz/db";
import type { MessageWithMetadata, SubscriptionType } from "@coral-xyz/common";
import { BACKEND_API_URL } from "@coral-xyz/common";
import { bulkAddChats, oldestReceivedMessage } from "@coral-xyz/db";

import { getAuthHeader } from "./getAuthHeader";
import { SignalingManager } from "./SignalingManager";

export const fetchMoreChatsFor = async (
uuid: string,
room: string,
type: SubscriptionType,
nftMint?: string,
publicKey?: string // To avoid DB calls on the backend
publicKey?: string, // To avoid DB calls on the backend
jwt?: string
) => {
const oldestMessage = await oldestReceivedMessage(uuid, room, type);
const response = await fetch(
Expand All @@ -24,6 +22,9 @@ export const fetchMoreChatsFor = async (
}&mint=${nftMint}&publicKey=${publicKey}`,
{
method: "GET",
headers: {
...getAuthHeader(jwt),
},
}
);

Expand Down
12 changes: 12 additions & 0 deletions packages/tamagui-core/src/chat/getAuthHeader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const getAuthHeader = (
jwt?: string
): {
Authorization?: string;
} => {
if (jwt) {
return {
Authorization: `Bearer ${jwt}`,
};
}
return {};
};
7 changes: 6 additions & 1 deletion packages/tamagui-core/src/chat/refreshChatsFor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import {
resetUpdateTimestamp,
} from "@coral-xyz/db";

import { getAuthHeader } from "./getAuthHeader";
import { SignalingManager } from "./SignalingManager";

export const refreshChatsFor = async (
uuid: string,
room: string,
type: SubscriptionType,
nftMint?: string,
publicKey?: string // To avoid DB calls on the backend
publicKey?: string, // To avoid DB calls on the backend
jwt?: string
) => {
const lastMessage = await latestReceivedMessage(uuid, room, type);
const response = await fetch(
Expand All @@ -26,6 +28,9 @@ export const refreshChatsFor = async (
}&mint=${nftMint}&publicKey=${publicKey}`,
{
method: "GET",
headers: {
...getAuthHeader(jwt),
},
}
);

Expand Down
10 changes: 8 additions & 2 deletions packages/tamagui-core/src/chat/refreshGroupsAndFriendships.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ import { refreshFriendships, refreshGroups } from "@coral-xyz/db";

import { SignalingManager } from "./SignalingManager";

export const refreshGroupsAndFriendships = async (uuid: string) => {
export const refreshGroupsAndFriendships = async (
uuid: string,
jwt?: string
) => {
try {
await Promise.all([refreshFriendships(uuid), refreshGroups(uuid)]);
await Promise.all([
refreshFriendships(uuid, jwt),
refreshGroups(uuid, jwt),
]);
} catch (e) {
console.error("Error while refreshing friendships and groups");
console.error(e);
Expand Down
15 changes: 8 additions & 7 deletions packages/tamagui-core/src/chat/refreshIndividualChatsFor.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type {
MessageWithMetadata,
SubscriptionType} from "@coral-xyz/common";
import {
BACKEND_API_URL
} from "@coral-xyz/common";
import type { MessageWithMetadata, SubscriptionType } from "@coral-xyz/common";
import { BACKEND_API_URL } from "@coral-xyz/common";
import { bulkAddChats, latestReceivedMessage } from "@coral-xyz/db";

import { getAuthHeader } from "./getAuthHeader";
import { SignalingManager } from "./SignalingManager";

export const refreshIndividualChatsFor = async (
Expand All @@ -14,7 +11,8 @@ export const refreshIndividualChatsFor = async (
type: SubscriptionType,
clientGeneratedUuid: string,
nftMint?: string,
publicKey?: string // To avoid DB calls on the backend
publicKey?: string, // To avoid DB calls on the backend
jwt?: string
) => {
const lastMessage = await latestReceivedMessage(uuid, room, type);
const response = await fetch(
Expand All @@ -26,6 +24,9 @@ export const refreshIndividualChatsFor = async (
}&mint=${nftMint}&publicKey=${publicKey}&clientGeneratedUuid=${clientGeneratedUuid}`,
{
method: "GET",
headers: {
...getAuthHeader(jwt),
},
}
);

Expand Down
7 changes: 6 additions & 1 deletion packages/tamagui-core/src/chat/refreshUpdatesFor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ import {
processMessageUpdates,
} from "@coral-xyz/db";

import { getAuthHeader } from "./getAuthHeader";
import { SignalingManager } from "./SignalingManager";

export const refreshUpdatesFor = async (
uuid: string,
room: string,
type: SubscriptionType,
nftMint?: string,
publicKey?: string // To avoid DB calls on the backend
publicKey?: string, // To avoid DB calls on the backend
jwt?: string
) => {
const lastUpdate = await latestReceivedUpdate(uuid, room.toString(), type);

Expand All @@ -25,6 +27,9 @@ export const refreshUpdatesFor = async (
}`,
{
method: "GET",
headers: {
...getAuthHeader(jwt),
},
}
);

Expand Down

1 comment on commit e0ea8f4

@vercel
Copy link

@vercel vercel bot commented on e0ea8f4 Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.