skidy
skidy
Explore posts from servers
BABetter Auth
Created by skidy on 4/12/2025 in #help
redirect after signout on a protected page
In Next.js, how do we redirect the user if they sign out while on a protected page? middleware below only gets executed on initial load
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);

if (!sessionCookie) {
return NextResponse.redirect(new URL("/", request.url));
}

return NextResponse.next();
}

export const config = {
matcher: ["/settings"],
};
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";

export async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);

if (!sessionCookie) {
return NextResponse.redirect(new URL("/", request.url));
}

return NextResponse.next();
}

export const config = {
matcher: ["/settings"],
};
2 replies
BABetter Auth
Created by skidy on 4/11/2025 in #help
getSession null
why useSession works but using getSession i get null
11 replies
PPrisma
Created by skidy on 3/31/2025 in #help-and-questions
Custom error becomes normal error
what's the probably reason why my custom classes throws normal error in nextjs ?
export default class ServerError extends Error {
issues: IError;

constructor(message: string, issues: IError) {
super(message);
this.name = "ServerError";
this.issues = issues;

Object.setPrototypeOf(this, ServerError.prototype);
}
}
export default class ServerError extends Error {
issues: IError;

constructor(message: string, issues: IError) {
super(message);
this.name = "ServerError";
this.issues = issues;

Object.setPrototypeOf(this, ServerError.prototype);
}
}
@/lib/db.ts
export const prisma =
globalForPrisma.prisma ||
new PrismaClient().$extends({
query: {
user: {
create: ({ args, query }) => {
if (args.data.username) {
args.data.usernameUpdatedAt = new Date();
}

return query(args);
},
update: async ({ args, query }) => {
if (args.data.username) {
const oldUser = await prisma.user.findUnique({
where: args.where,
select: {
usernameUpdatedAt: true,
},
});

if (oldUser?.usernameUpdatedAt) {
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);

if (oldUser.usernameUpdatedAt > oneWeekAgo) {
// throws the error
throw new ServerError(
"Username can only be updated once a week",
{
errors: [
{
code: "user/username-too-frequent",
message: "Username can only be updated once a week",
},
],
},
);
}

args.data.usernameUpdatedAt = new Date();
} else {
args.data.usernameUpdatedAt = new Date();
}
}

return query(args);
},
},
},
});
@/lib/db.ts
export const prisma =
globalForPrisma.prisma ||
new PrismaClient().$extends({
query: {
user: {
create: ({ args, query }) => {
if (args.data.username) {
args.data.usernameUpdatedAt = new Date();
}

return query(args);
},
update: async ({ args, query }) => {
if (args.data.username) {
const oldUser = await prisma.user.findUnique({
where: args.where,
select: {
usernameUpdatedAt: true,
},
});

if (oldUser?.usernameUpdatedAt) {
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);

if (oldUser.usernameUpdatedAt > oneWeekAgo) {
// throws the error
throw new ServerError(
"Username can only be updated once a week",
{
errors: [
{
code: "user/username-too-frequent",
message: "Username can only be updated once a week",
},
],
},
);
}

args.data.usernameUpdatedAt = new Date();
} else {
args.data.usernameUpdatedAt = new Date();
}
}

return query(args);
},
},
},
});
3 replies
PPrisma
Created by skidy on 3/3/2025 in #help-and-questions
Case insensitive for unique constraint
how do i make the name case insensitive
@@unique([userId, name])
@@unique([userId, name])
6 replies
PPrisma
Created by skidy on 2/24/2025 in #help-and-questions
prisma validation
what's the best approach for validating data before creation if prisma currently can't validate nested writes?
7 replies