Authenticate /pages/api route

Hi, i am trying to protect my websocket connections by verifying if the user/client is logged in or not. Usually i'd use the getCurrentUser function:
"use server";
import { auth } from "@/lib/auth";
import { NextApiRequest } from "next";
import { NextRequest } from "next/server";

export async function getCurrentUser(req: NextRequest) {
const session = await auth.api.getSession(req);

if (!session || !session.user) {
return null;
}

return session.user;
}
"use server";
import { auth } from "@/lib/auth";
import { NextApiRequest } from "next";
import { NextRequest } from "next/server";

export async function getCurrentUser(req: NextRequest) {
const session = await auth.api.getSession(req);

if (!session || !session.user) {
return null;
}

return session.user;
}
within my socket.ts file, but this doesnt work because i receive a NextApiRequest as req and not a NextRequest. Im having trouble finding good documentation for complete noobs to follow, only little snippets where i dont really know what is meant. So if somebody could help me out i'd really appreciate it.
1 Reply
Punch Energy
Punch EnergyOP3mo ago
This is my api route where i want to authenticate my clients: /pages/api/socket.ts
import { Server, ServerOptions } from "socket.io";
import type { NextApiRequest, NextApiResponse } from "next";
import { getCurrentUser } from "@/lib/session"; // -> doesnt work because it expects NextRequest and not NextApiRequest

export const config = {
api: {
bodyParser: false,
},
};

type SocketResponse = NextApiResponse & {
socket: { server: ServerOptions & { io: Server } };
};

const SocketHandler = async (req: NextApiRequest, res: SocketResponse) => {
if (!res.socket.server.io) {
// Create a new Socket.IO server
const io = new Server(res.socket.server, {
path: "/api/socket",
addTrailingSlash: false,
});

// Store the Socket.IO server instance
res.socket.server.io = io;

// Socket.IO event handlers
io.on("connection", (socket) => {
socket.emit("test", "hello from server");
socket.on("updateList", (msg) => {
console.log("Received updateList:", msg);
// handle update
io.emit("updateList");
});

socket.on("disconnect", () => {
console.log("Websocket: Client disconnected:", socket.id);
});
});
}

res.end();
};

export default SocketHandler;
import { Server, ServerOptions } from "socket.io";
import type { NextApiRequest, NextApiResponse } from "next";
import { getCurrentUser } from "@/lib/session"; // -> doesnt work because it expects NextRequest and not NextApiRequest

export const config = {
api: {
bodyParser: false,
},
};

type SocketResponse = NextApiResponse & {
socket: { server: ServerOptions & { io: Server } };
};

const SocketHandler = async (req: NextApiRequest, res: SocketResponse) => {
if (!res.socket.server.io) {
// Create a new Socket.IO server
const io = new Server(res.socket.server, {
path: "/api/socket",
addTrailingSlash: false,
});

// Store the Socket.IO server instance
res.socket.server.io = io;

// Socket.IO event handlers
io.on("connection", (socket) => {
socket.emit("test", "hello from server");
socket.on("updateList", (msg) => {
console.log("Received updateList:", msg);
// handle update
io.emit("updateList");
});

socket.on("disconnect", () => {
console.log("Websocket: Client disconnected:", socket.id);
});
});
}

res.end();
};

export default SocketHandler;

Did you find this page helpful?