Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Update member manage UI #24

Merged
merged 9 commits into from
Sep 23, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: Add new server action to get user by email
DW225 committed Sep 23, 2024
commit 0453d37dd48ae5ad41856035f90e0da9903e9069
8 changes: 5 additions & 3 deletions lib/actions/authenticatedDBActions.ts
Original file line number Diff line number Diff line change
@@ -3,18 +3,18 @@
import type { NewBoard, NewPost } from "@/db/schema";
import { createBoard, deleteBoard } from "@/lib/db/board";
import { createPost, fetchPostsByBoardID } from "@/lib/db/post";
import { findUserIdByKindeID } from "@/lib/db/user";
import { findUserByEmail, findUserIdByKindeID } from "@/lib/db/user";
import { getKindeServerSession } from "@kinde-oss/kinde-auth-nextjs/server";
import { redirect } from "next/navigation";

export async function authenticatedAction<T>(
async function authenticatedAction<T>(
action: () => Promise<T>
): Promise<T | null> {
const { isAuthenticated } = getKindeServerSession();

if (!isAuthenticated()) {
console.warn("Not authenticated");
redirect("/api/auth/login");
redirect("/");
}

return await action();
@@ -48,3 +48,5 @@ export const authenticatedDeleteBoard = async (
) => authenticatedAction(() => deleteBoard(boardId, userId));

export const authenticatedFindUserIdByKindeID = async (kindeId: string) => authenticatedAction(() => findUserIdByKindeID(kindeId));

export const authenticatedFindUserByEmail = async (email: string) => authenticatedAction(() => findUserByEmail(email));
16 changes: 13 additions & 3 deletions lib/db/user.ts
Original file line number Diff line number Diff line change
@@ -7,7 +7,8 @@ export async function findUserIdByKindeID(kindeId: string) {
const result = await db
.select({ id: userTable.id })
.from(userTable)
.where(eq(userTable.kinde_id, kindeId)).limit(1);
.where(eq(userTable.kinde_id, kindeId))
.limit(1);
if (result.length > 0) {
return result[0].id;
} else {
@@ -37,6 +38,15 @@ export const deleteUser = async (userID: string) => {
const result = await db
.delete(userTable)
.where(or(eq(userTable.id, userID), eq(userTable.kinde_id, userID)))
.returning({ deletedId: userTable.id });;
return result.length > 0 ? result[0] : 'No user deleted';
.returning({ deletedId: userTable.id });
return result.length > 0 ? result[0] : "No user deleted";
};

export const findUserByEmail = async (email: string) => {
const result = await db
.select()
.from(userTable)
.where(eq(userTable.email, email))
.limit(1);
return result.length > 0 ? result[0] : undefined;
};