Implementing get Request with Prisma

I impleented login route using prisma and also Redis is being used for session management but when a user is logged in another device and the other user opens the website in another device without logging in the returned user is the one who was authenticated from the other device, How can i fix this below is the source codes
import { NextResponse,type NextRequest } from "next/server";
import prisma from "@/prisma/client";
import {Redis} from "@upstash/redis"
import bcrypt from "bcrypt";

export const revalidate = 0;

const redis = Redis.fromEnv();

export async function POST(req: NextRequest, res: NextResponse) {
try {
const body = await req.json();
const { email, password } = body;

if (!email || !password) {
return new NextResponse("Email and password are required", { status: 400 });
}

const user = await prisma.user.findFirst({
where: {
email: email as string,
role: "USER",
ustate:"NON_BLOCKED",
}, select: {
id: true,
email: true,
name: true,
image: true,
role:true,
hashedPassword:true
},
});

if (!user || !user.hashedPassword) {
return new NextResponse("User not found", { status: 404 });
}

const passwordMatch = await bcrypt.compare(password, user.hashedPassword);

if (!passwordMatch) {
return new NextResponse("Invalid credentials", { status: 401 });
}

const member = await redis.set("user",user)
console.log(member)


const response = {
message: "Authenticated!",
};

return new Response(JSON.stringify(response), {
status: 200,
});


} catch (error) {
console.error("Error fetching user:", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
import { NextResponse,type NextRequest } from "next/server";
import prisma from "@/prisma/client";
import {Redis} from "@upstash/redis"
import bcrypt from "bcrypt";

export const revalidate = 0;

const redis = Redis.fromEnv();

export async function POST(req: NextRequest, res: NextResponse) {
try {
const body = await req.json();
const { email, password } = body;

if (!email || !password) {
return new NextResponse("Email and password are required", { status: 400 });
}

const user = await prisma.user.findFirst({
where: {
email: email as string,
role: "USER",
ustate:"NON_BLOCKED",
}, select: {
id: true,
email: true,
name: true,
image: true,
role:true,
hashedPassword:true
},
});

if (!user || !user.hashedPassword) {
return new NextResponse("User not found", { status: 404 });
}

const passwordMatch = await bcrypt.compare(password, user.hashedPassword);

if (!passwordMatch) {
return new NextResponse("Invalid credentials", { status: 401 });
}

const member = await redis.set("user",user)
console.log(member)


const response = {
message: "Authenticated!",
};

return new Response(JSON.stringify(response), {
status: 200,
});


} catch (error) {
console.error("Error fetching user:", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
1 Reply
Revaycolizer
RevaycolizerOP16mo ago
export async function GET(req: Request, res: NextResponse) {

const member = await redis.get("user")

const useri = JSON.stringify(member)

const usr = JSON.parse(useri)

if(!usr || !member){
return new Response("No user found", {status:404})
}

try {

const user = await prisma.user.findFirst({
where:{
email: usr.email,
ustate: "NON_BLOCKED",
},
select:{
email:true,
role:true,
image:true,
name:true,
}
})

return NextResponse.json(user);

} catch (error) {
console.error("Error retrieving user token:", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
export async function GET(req: Request, res: NextResponse) {

const member = await redis.get("user")

const useri = JSON.stringify(member)

const usr = JSON.parse(useri)

if(!usr || !member){
return new Response("No user found", {status:404})
}

try {

const user = await prisma.user.findFirst({
where:{
email: usr.email,
ustate: "NON_BLOCKED",
},
select:{
email:true,
role:true,
image:true,
name:true,
}
})

return NextResponse.json(user);

} catch (error) {
console.error("Error retrieving user token:", error);
return new NextResponse("Internal Error", { status: 500 });
}
}
Want results from more Discord servers?
Add your server