Skip to content

Commit

Permalink
feat(notifications): add auth based on new token storage
Browse files Browse the repository at this point in the history
  • Loading branch information
manuandru committed May 24, 2024
1 parent 7abac4b commit e552601
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useAppStore } from "@/stores/app";
import { useFriendStore } from "@/stores/friend";
import { useNotificationStore } from "@/stores/notifications";
import { useUsersStatusStore } from "@/stores/status";
import { AxiosController } from "@/controllers/axios-controller";

class NotificationService {
callbacks: Record<string, (message: any) => Promise<void>> = {};
Expand Down Expand Up @@ -222,10 +223,20 @@ function createNotificationService() {
}

export function useNotificationService() {
const userStorage = localStorage.getItem("user");
const user = userStorage ? JSON.parse(userStorage) : null;
const accessToken = user ? user.jwt : null;

if (!accessToken) {
throw new Error("User not logged in");
}
const notificationService = createNotificationService();
const notificationSocket = io({
transports: ["websocket"],
path: "/notification",
extraHeaders: {
Authorization: `Bearer ${accessToken}`,
},
});

notificationSocket.on("connect", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export class NotificationSocketServer {
path: "/notification",
});
this.io.on("connection", async (socket) => {
const jwt = this.getTokenFromHeadersCookie(
socket.handshake.headers.cookie
);
// const jwt = this.getTokenFromHeadersCookie(
let jwt = socket.handshake.headers.authorization;
console.log(jwt);
// );
await this.validateTokenOrDisconnect(socket, jwt);
const username = decodeAccessToken(jwt!)?.username;
if (username) {
Expand Down

0 comments on commit e552601

Please sign in to comment.