Skip to content

Commit 75369e1

Browse files
committed
refactor: Fixed some queries, updated Readme, for troubleshooting and testing
1 parent 12f1f80 commit 75369e1

File tree

9 files changed

+151
-84
lines changed

9 files changed

+151
-84
lines changed

.env.example

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Recommended to have an example env file for other users know what environment variables need to be set.
2+
3+
# Get your keys from the Clerk dashboard at https://dashboard.clerk.com
4+
CLERK_SECRET_KEY=sk_
5+
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_
6+
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
7+
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
8+
9+
# If using a local docker container (local-database.yml)
10+
DATABASE_URL=postgresql://postgres:postgres@localhost:5432/lockscript?schema=public

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ yarn-error.log*
3535
# typescript
3636
*.tsbuildinfo
3737
next-env.d.ts
38+
39+
#Intellij Editors
40+
.idea

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,47 @@ LockScript Vault is an open-source secure vault for passwords, cards, notes, and
1010
- Prisma
1111
- Zod
1212

13+
## Getting Started
14+
15+
Clone the repository
16+
```bash
17+
git clone [email protected]:Lockscript/Lockscript-Vault
18+
```
19+
```bash
20+
cd Lockscript-Vault
21+
```
22+
23+
Repo is using yarn as a package manager.
24+
25+
[How To Install Yarn](https://classic.yarnpkg.com/lang/en/docs/install)
26+
27+
Install dependencies
28+
```bash
29+
yarn install
30+
```
31+
32+
### Setup local database (if required)
33+
34+
Start Docker container (requires [Docker](https://docs.docker.com/engine/install/))
35+
36+
```bash
37+
docker compose -f ./local-database.yml up -d
38+
```
39+
Generate database client
40+
41+
```bash
42+
yarn run generate
43+
```
44+
```bash
45+
yarn run push
46+
```
47+
48+
### Start the dev server
49+
```bash
50+
yarn run dev
51+
```
52+
53+
1354
## How to contribute
1455

1556
We accept contributions from the community, but you must follow some rules:

local-database.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Use postgres/example user/password credentials
2+
version: '3.1'
3+
services:
4+
db:
5+
container_name: lockscript-postgres
6+
image: postgres
7+
restart: always
8+
ports:
9+
- "5432:5432"
10+
environment:
11+
POSTGRES_PASSWORD: postgres
12+
volumes:
13+
- postgres:/var/lib/postgres/data
14+
volumes:
15+
postgres:
16+
external: false
17+

src/app/(main)/page.tsx

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
import {VaultPage} from "@/components/vault/vault-page";
2-
import prismadb from "@/lib/prismadb";
3-
import {auth} from "@clerk/nextjs/server";
4-
import {instantiateVault} from "../actions";
2+
import {auth, currentUser} from "@clerk/nextjs/server";
3+
import {getPasswords, instantiateVault} from "../actions";
54

6-
export const dynamic = "force-dynamic";
7-
8-
const Page = async () => {
5+
export default async function Page(){
96
const { userId, redirectToSignIn } = await auth();
107

118
if (!userId) return redirectToSignIn();
129

13-
const user = await prismadb.user.findUnique({
14-
where: {
15-
id: userId,
16-
},
17-
include: {
18-
passwordItems: true,
19-
cardItems: true,
20-
pinItems: true,
21-
noteItems: true,
22-
},
23-
});
10+
const user = await getPasswords(userId)
2411

2512
if (!user) {
26-
instantiateVault();
13+
const clerkUser = await currentUser()
14+
if(!clerkUser) return redirectToSignIn()
15+
await instantiateVault(clerkUser.id, clerkUser.username as string);
2716
}
2817

2918
return <VaultPage user={user} />;
3019
};
31-
32-
export default Page;

src/app/actions.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,46 +94,29 @@ export async function createPasswordItem(
9494
throw new Error("Not authenticated");
9595
}
9696

97-
const user = await prismadb.user.findUnique({
98-
where: {
99-
id: userId,
100-
},
101-
include: {
102-
passwordItems: true,
103-
},
104-
});
105-
106-
if (!user) {
107-
throw new Error("User not found");
108-
}
109-
11097
const newPasswordItem = await prismadb.passwordItem.create({
11198
data: {
11299
username,
113100
website,
114101
password,
115102
updatedAt: new Date().toISOString(),
116103
createdAt: new Date().toISOString(),
117-
userId: user.id,
104+
user: {
105+
connect: {
106+
id: userId
107+
}
108+
}
118109
},
119110
});
120111

121112
return newPasswordItem;
122113
}
123114

124-
export async function instantiateVault() {
125-
const { userId } = await auth()
126-
127-
if (!userId) {
128-
throw new Error("Not authenticated");
129-
}
130-
131-
const user = await currentUser()
132-
115+
export async function instantiateVault(userId: string, username: string) {
133116
const vault = await prismadb.user.create({
134117
data: {
135118
id: userId,
136-
username: user?.username!,
119+
username: username,
137120
},
138121
include: {
139122
passwordItems: true,
@@ -145,3 +128,17 @@ export async function instantiateVault() {
145128

146129
return vault;
147130
}
131+
132+
export async function getPasswords(userId: string) {
133+
return prismadb.user.findUnique({
134+
where: {
135+
id: userId,
136+
},
137+
include: {
138+
passwordItems: true,
139+
cardItems: true,
140+
pinItems: true,
141+
noteItems: true,
142+
},
143+
});
144+
}

src/components/vault/dialogs/create-password-dialog.tsx

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
"use client"
2-
31
import {createPasswordItem} from "@/app/actions";
42
import {Button} from "@/components/ui/button";
53
import {Dialog,DialogContent,DialogFooter,DialogHeader,DialogTitle} from "@/components/ui/dialog";
64
import {Input} from "@/components/ui/input";
75
import {encrypt} from "@/utils/encryption";
86
import {useUser} from "@clerk/nextjs";
97
import {Loader2} from "lucide-react";
10-
import {useState} from "react";
8+
import {ChangeEvent, useState} from "react";
119
import toast from "react-hot-toast";
1210
import {z} from "zod";
1311

12+
const initialPasswordItemState = {
13+
name: "",
14+
username: "",
15+
website: "",
16+
password: "",
17+
}
18+
1419
export const CreatePasswordDialog = ({
1520
open,
16-
onClose,
21+
onClose,
1722
}: {
1823
open: boolean;
19-
onClose: () => void;
24+
onClose: ()=> void
2025
}) => {
21-
const [name, setName] = useState("");
22-
const [username, setUsername] = useState("");
23-
const [website, setWebsite] = useState("");
24-
const [password, setPassword] = useState("");
26+
27+
const [passwordItem, setPasswordItem] = useState(initialPasswordItemState)
28+
2529
const [loading, setLoading] = useState(false);
2630

2731
const { user: clerkuser } = useUser();
@@ -47,12 +51,7 @@ export const CreatePasswordDialog = ({
4751

4852
const handleSave = async () => {
4953
setLoading(true);
50-
const validationResult = passwordSchema.safeParse({
51-
name,
52-
username,
53-
website,
54-
password,
55-
});
54+
const validationResult = passwordSchema.safeParse(passwordItem);
5655

5756
if (!validationResult.success) {
5857
const errorMessage =
@@ -64,11 +63,12 @@ export const CreatePasswordDialog = ({
6463

6564
try {
6665
await createPasswordItem(
67-
encrypt(username, clerkuser),
68-
encrypt(website, clerkuser),
69-
encrypt(password, clerkuser)
66+
encrypt(passwordItem.username, clerkuser),
67+
encrypt(passwordItem.website, clerkuser),
68+
encrypt(passwordItem.password, clerkuser)
7069
);
7170
toast.success("Password created");
71+
setPasswordItem(initialPasswordItemState)
7272
onClose();
7373
} catch (error) {
7474
toast.error("Failed to create password");
@@ -77,6 +77,10 @@ export const CreatePasswordDialog = ({
7777
}
7878
};
7979

80+
const handleChange = (e: ChangeEvent<HTMLInputElement>) => {
81+
setPasswordItem(prevState => ({...prevState, [e.target.name]: e.target.value}))
82+
}
83+
8084
return (
8185
<Dialog open={open} onOpenChange={onClose}>
8286
<DialogContent>
@@ -87,50 +91,54 @@ export const CreatePasswordDialog = ({
8791
<div className="relative">
8892
<Input
8993
placeholder="Name"
90-
value={name}
91-
onChange={(e) => setName(e.target.value)}
94+
value={passwordItem.name}
95+
onChange={handleChange}
9296
maxLength={50}
97+
name="name"
9398
/>
94-
<div className="mt-1 text-sm text-gray-500">{name.length} / 50</div>
99+
<div className="mt-1 text-sm text-gray-500">{passwordItem.name.length} / 50</div>
95100
</div>
96101
<div className="relative">
97102
<Input
98103
placeholder="Username"
99-
value={username}
100-
onChange={(e) => setUsername(e.target.value)}
104+
value={passwordItem.username}
105+
onChange={handleChange}
101106
maxLength={30}
107+
name="username"
102108
/>
103109
<div className="mt-1 text-sm text-gray-500">
104-
{username.length} / 30
110+
{passwordItem.username.length} / 30
105111
</div>
106112
</div>
107113
<div className="relative">
108114
<Input
109115
placeholder="Website"
110-
value={website}
111-
onChange={(e) => setWebsite(e.target.value)}
116+
value={passwordItem.website}
117+
onChange={handleChange}
112118
maxLength={1024}
119+
name="website"
113120
/>
114121
<div className="mt-1 text-sm text-gray-500">
115-
{website.length} / 1024
122+
{passwordItem.website.length} / 1024
116123
</div>
117124
</div>
118125
<div className="relative">
119126
<Input
120127
placeholder="Password"
121-
value={password}
122-
onChange={(e) => setPassword(e.target.value)}
128+
value={passwordItem.password}
129+
onChange={handleChange}
123130
type="password"
131+
name="password"
124132
maxLength={128}
125133
/>
126134
<div className=" mt-1 text-sm text-gray-500">
127-
{password.length} / 128
135+
{passwordItem.password.length} / 128
128136
</div>
129137
</div>
130138
</div>
131139
<DialogFooter>
132140
<div className="mt-4 flex justify-end gap-2">
133-
<Button variant="outline" onClick={onClose}>
141+
<Button variant="outline" onClick={()=>onClose()}>
134142
Cancel
135143
</Button>
136144
<Button onClick={handleSave}>

0 commit comments

Comments
 (0)