-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a logic to store new User data into database, and logged in u…
…ser with account with permissions
- Loading branch information
1 parent
51c16d3
commit 26df234
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |