Added Connection Component, Store, JWT logic
This commit is contained in:
17
apps/dispatch/app/(dispatch)/_components/Connection.tsx
Normal file
17
apps/dispatch/app/(dispatch)/_components/Connection.tsx
Normal file
@@ -0,0 +1,17 @@
|
||||
import { useSession } from "next-auth/react";
|
||||
import { connectionStore } from "../../_store/connectionStore";
|
||||
import { useEffect } from "react";
|
||||
|
||||
export const Connection = () => {
|
||||
const session = useSession();
|
||||
const cStore = connectionStore((state) => state);
|
||||
const uid = session.data?.user?.id;
|
||||
|
||||
useEffect(() => {
|
||||
if (uid) {
|
||||
cStore.connect(uid);
|
||||
}
|
||||
}, [uid]);
|
||||
|
||||
return <div>{cStore.isConnected ? "Connected" : "Not Connected"}</div>;
|
||||
};
|
||||
@@ -6,10 +6,10 @@ import Link from "next/link";
|
||||
import { connectionStore } from "../../_store/connectionStore";
|
||||
import { useEffect } from "react";
|
||||
import { socket } from "../socket";
|
||||
import { useSession } from "next-auth/react";
|
||||
import { Connection } from "./Connection";
|
||||
|
||||
export default function Navbar() {
|
||||
const connected = connectionStore((state) => state);
|
||||
|
||||
return (
|
||||
<div className="navbar bg-base-100 shadow-sm">
|
||||
<div className="flex-1">
|
||||
@@ -51,6 +51,9 @@ export default function Navbar() {
|
||||
<path d="M21.64,13a1,1,0,0,0-1.05-.14,8.05,8.05,0,0,1-3.37.73A8.15,8.15,0,0,1,9.08,5.49a8.59,8.59,0,0,1,.25-2A1,1,0,0,0,8,2.36,10.14,10.14,0,1,0,22,14.05,1,1,0,0,0,21.64,13Zm-9.5,6.69A8.14,8.14,0,0,1,7.08,5.22v.27A10.15,10.15,0,0,0,17.22,15.63a9.79,9.79,0,0,0,2.1-.22A8.11,8.11,0,0,1,12.14,19.73Z" />
|
||||
</svg>
|
||||
</label>
|
||||
<div>
|
||||
<Connection />
|
||||
</div>
|
||||
<div className="dropdown dropdown-end">
|
||||
<div tabIndex={0} role="button" className="btn btn-ghost">
|
||||
...
|
||||
|
||||
@@ -14,6 +14,7 @@ export default async function RootLayout({
|
||||
children: React.ReactNode;
|
||||
}>) {
|
||||
const session = await getServerSession();
|
||||
console.log(session);
|
||||
if (!session) {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { io } from "socket.io-client";
|
||||
|
||||
export const socket = io(process.env.NEXT_PUBLIC_DISPATCH_SERVER_URL, {
|
||||
auth: (cb) => {
|
||||
cb({ token: "jwt" });
|
||||
},
|
||||
autoConnect: false,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user