Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/lib/ActionClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createSafeActionClient } from "next-safe-action";
import { getServerSession } from "next-auth";
//import { getServerSession } from "next-auth/next";
// when the user sign in
async function getUserInfo() {

/* mcok user object
const mockSession = {
user: {
id: "123",
name: "Test User",
email: "[email protected]",
role: "admin", //change to "user" or "admin" to test permissions
},
};
var session = mockSession;
*/


const session = await getServerSession();
if (!session?.user){
return null
}
return session.user; // i'm assuming the user object would look like this {id: "[0-9], role: "admin or null for regular user, ..."}
}

//admin authentication
async function getAdmin() {
const userInfo = await getUserInfo();
if (userInfo.role !== "admin"){
throw new Error("Not authorized");
}
return userInfo;
}

//Public Access (when not signed in)
export const publicAction = createSafeActionClient();


// Protected — must be logged in
export const protectedAction = createSafeActionClient({
async middleware() {
const user = await getUserInfo();
if (!user) throw new Error("You must be signed in.");
return { user };
},
});

// Admin — must be admin
export const adminAction = createSafeActionClient({
async middleware() {
const user = await getAdmin();
return { user };
},
});