mirror of
https://github.com/i701/sarlink-portal.git
synced 2025-06-29 13:43:58 +00:00
refactor: update authentication flow to use NextAuth, replace better-auth with axios for API calls, and clean up unused code
This commit is contained in:
@ -1,7 +0,0 @@
|
||||
import { phoneNumberClient } from "better-auth/client/plugins";
|
||||
import { createAuthClient } from "better-auth/react";
|
||||
|
||||
export const authClient = createAuthClient({
|
||||
baseURL: process.env.BETTER_AUTH_URL,
|
||||
plugins: [phoneNumberClient()],
|
||||
});
|
@ -1,5 +1,5 @@
|
||||
"use server";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { auth } from "@/app/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
"use server";
|
||||
import { headers } from "next/headers";
|
||||
import { cache } from "react";
|
||||
import { auth } from "./auth";
|
||||
import { auth } from "../app/auth";
|
||||
|
||||
const getCurrentUserCache = cache(async () => {
|
||||
const session = await auth.api.getSession({
|
||||
|
44
lib/auth.ts
44
lib/auth.ts
@ -1,44 +0,0 @@
|
||||
import { sendOtp } from "@/actions/auth-actions";
|
||||
import { betterAuth } from "better-auth";
|
||||
import { prismaAdapter } from "better-auth/adapters/prisma";
|
||||
import { phoneNumber } from "better-auth/plugins";
|
||||
import prisma from "./db";
|
||||
|
||||
export const auth = betterAuth({
|
||||
session: {
|
||||
cookieCache: {
|
||||
enabled: true,
|
||||
maxAge: 10 * 60, // Cache duration in seconds
|
||||
},
|
||||
},
|
||||
trustedOrigins: process.env.BETTER_AUTH_TRUSTED_ORIGINS?.split(",") || [
|
||||
"localhost:3000",
|
||||
],
|
||||
user: {
|
||||
additionalFields: {
|
||||
role: {
|
||||
type: "string",
|
||||
required: false,
|
||||
defaultValue: "USER",
|
||||
input: false, // don't allow user to set role
|
||||
},
|
||||
lang: {
|
||||
type: "string",
|
||||
required: false,
|
||||
defaultValue: "en",
|
||||
},
|
||||
},
|
||||
},
|
||||
database: prismaAdapter(prisma, {
|
||||
provider: "postgresql", // or "mysql", "postgresql", ...etc
|
||||
}),
|
||||
plugins: [
|
||||
phoneNumber({
|
||||
sendOTP: async ({ phoneNumber, code }) => {
|
||||
// Implement sending OTP code via SMS
|
||||
console.log("Send OTP in auth.ts", phoneNumber, code);
|
||||
await sendOtp(phoneNumber, code);
|
||||
},
|
||||
}),
|
||||
],
|
||||
});
|
17
lib/db.ts
17
lib/db.ts
@ -1,17 +0,0 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
const prismaClientSingleton = () => {
|
||||
return new PrismaClient();
|
||||
};
|
||||
|
||||
type PrismaClientSingleton = ReturnType<typeof prismaClientSingleton>;
|
||||
|
||||
const globalForPrisma = globalThis as unknown as {
|
||||
prisma: PrismaClientSingleton | undefined;
|
||||
};
|
||||
|
||||
const prisma = globalForPrisma.prisma ?? prismaClientSingleton();
|
||||
|
||||
export default prisma;
|
||||
|
||||
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;
|
38
lib/types/user.ts
Normal file
38
lib/types/user.ts
Normal file
@ -0,0 +1,38 @@
|
||||
import type { ISODateString } from "next-auth";
|
||||
|
||||
export interface Permission {
|
||||
id: number;
|
||||
name: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
export interface TAuthUser {
|
||||
expiry?: string;
|
||||
token?: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
export interface User {
|
||||
id: number;
|
||||
username: string;
|
||||
email: string;
|
||||
user_permissions: Permission[];
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
is_superuser: boolean;
|
||||
date_joined: string;
|
||||
last_login: string;
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
user?: {
|
||||
token?: string;
|
||||
name?: string | null;
|
||||
email?: string | null;
|
||||
image?: string | null;
|
||||
user?: User & {
|
||||
expiry?: string;
|
||||
};
|
||||
};
|
||||
expires: ISODateString;
|
||||
}
|
Reference in New Issue
Block a user