Skip to content

Commit

Permalink
Created a logic to store new User data into database, and logged in u…
Browse files Browse the repository at this point in the history
…ser with account with permissions
  • Loading branch information
Priyanshu9898 committed Dec 25, 2024
1 parent 51c16d3 commit 26df234
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
39 changes: 39 additions & 0 deletions actions/Dashboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// "use server";

// import { DB } from "@/lib/prisma";
// import { auth } from "@clerk/nextjs/server";

// export async function createAccount(data) {
// try {
// const { userId } = await auth();

// console.log(userId);

// if (!userId) {
// throw new Error("Unauthorized!!");

// }

// // Create account
// const user = await DB.user.findUnique({
// where: {
// clerkUserId: userId,
// },
// });

// if (!user) {
// throw new Error("User not found");
// }

// const account = await DB.account.create({
// data: {
// userId: user.id,
// },
// });

// return account;

// } catch (error) {
// console.error(error);
// }
// }
7 changes: 6 additions & 1 deletion components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ import { SignedIn, SignInButton, SignedOut, UserButton } from "@clerk/nextjs";
import { Button } from "./ui/button";
import Link from "next/link";
import { LayoutDashboard, PenBox } from "lucide-react";
import { checkUser } from "@/lib/checkUser";

const Navbar = async () => {
const user = await checkUser();

console.log(user);

const Navbar = () => {
return (
<>
<div className="container mx-auto px-2 md:px-10 z-50 fixed top-0 bg-white/80 backdrop-blur-md border-b">
Expand Down
37 changes: 37 additions & 0 deletions lib/checkUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { currentUser } from "@clerk/nextjs/server";
import { DB } from "./prisma";

export const checkUser = async () => {
try {
const user = await currentUser();

if (!user) {
return null;
}

const loggedInUser = await DB.user.findUnique({
where: {
clerkUserId: user.id,
},
});

if (loggedInUser) {
return loggedInUser;
}

const newUserName = `${user.firstName} ${user.lastName}`;

const newUser = await DB.user.create({
data: {
clerkUserId: user.id,
email: user.emailAddresses[0].emailAddress,
name: newUserName,
imageUrl: user.imageUrl,
},
});

return newUser;
} catch (error) {
console.error(error);
}
};

0 comments on commit 26df234

Please sign in to comment.