Dental Care
This commit is contained in:
4
app/api/auth/[...all]/route.ts
Normal file
4
app/api/auth/[...all]/route.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { auth } from "@/lib/auth-session/auth";
|
||||
import { toNextJsHandler } from "better-auth/next-js";
|
||||
|
||||
export const { GET, POST } = toNextJsHandler(auth.handler);
|
||||
33
app/api/auth/resend-verification/route.ts
Normal file
33
app/api/auth/resend-verification/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { auth } from "@/lib/auth-session/auth";
|
||||
|
||||
/**
|
||||
* POST /api/auth/resend-verification
|
||||
* Resends the email verification link to the user
|
||||
*/
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const body = await req.json();
|
||||
const { email } = body;
|
||||
|
||||
if (!email) {
|
||||
return NextResponse.json({ error: "Email is required" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Use Better Auth's sendVerificationEmail method
|
||||
await auth.api.sendVerificationEmail({
|
||||
body: { email },
|
||||
});
|
||||
|
||||
return NextResponse.json(
|
||||
{ message: "Verification email sent successfully" },
|
||||
{ status: 200 }
|
||||
);
|
||||
} catch (error) {
|
||||
console.error("Failed to resend verification email:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Failed to send verification email" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
50
app/api/auth/session/route.ts
Normal file
50
app/api/auth/session/route.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { auth } from "@/lib/auth-session/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/types/prisma";
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: await headers(),
|
||||
});
|
||||
|
||||
if (!session) {
|
||||
return NextResponse.json({ error: "No session found" }, { status: 401 });
|
||||
}
|
||||
|
||||
// Fetch the full user object from database to get the role
|
||||
// This is necessary because session cache doesn't include additional fields
|
||||
if (session.user) {
|
||||
const dbUser = await prisma.user.findUnique({
|
||||
where: { id: session.user.id },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
image: true,
|
||||
role: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (dbUser) {
|
||||
// Merge the role from database into the session user object
|
||||
session.user = {
|
||||
...session.user,
|
||||
role: dbUser.role || "patient", // Default to patient if no role set
|
||||
};
|
||||
} else {
|
||||
// Fallback if user not found in database
|
||||
session.user.role = "patient";
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json(session);
|
||||
} catch (error) {
|
||||
console.error("Session fetch error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Internal server error" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user