-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd28b1d
commit af48c65
Showing
17 changed files
with
4,124 additions
and
108 deletions.
There are no files selected for viewing
Empty file.
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,43 @@ | ||
import {z} from "zod"; | ||
import {useDecrypt} from "~/utils/hash-password"; | ||
|
||
const userSchema = z.object({ | ||
email: z.string().email("This is not a valid email"), | ||
password: z.string().min(8).max(20), | ||
}); | ||
|
||
export default defineEventHandler(async (event) => { | ||
const {email, password} = await readValidatedBody(event, userSchema.parse); | ||
|
||
const user = await usePrisma().user.findUnique({ | ||
where: { | ||
email, | ||
}, | ||
select: { | ||
id: true, | ||
email: true, | ||
username: true, | ||
password: true, | ||
image: true, | ||
}, | ||
}); | ||
|
||
if (user) { | ||
const match = await useDecrypt(password, user.password); | ||
|
||
if (match) { | ||
setCookie(event, 'auth_token', useGenerateToken(user.id), { | ||
secure: true, | ||
httpOnly: true, | ||
sameSite: 'none', | ||
}); | ||
|
||
return { | ||
email: user.email, | ||
username: user.username, | ||
bio: user.bio, | ||
image: user.image, | ||
}; | ||
} | ||
} | ||
}); |
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,5 @@ | ||
export default defineEventHandler(async (event) => { | ||
deleteCookie(event, 'auth_token'); | ||
|
||
return "Logged out"; | ||
}); |
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,58 @@ | ||
import {z} from "zod"; | ||
|
||
const userSchema = z.object({ | ||
username: z.string().min(3).max(20), | ||
email: z.string().email("This is not a valid email"), | ||
password: z.string().min(8).max(20), | ||
}); | ||
|
||
export default defineEventHandler(async (event) => { | ||
const {username, email, password} = await readValidatedBody(event, userSchema.parse); | ||
|
||
const existingUser = await usePrisma().user.findUnique({ | ||
where: { | ||
OR: [ | ||
{ email }, | ||
{ username }, | ||
] | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
|
||
if (existingUser) { | ||
return createError({ | ||
status: 422, | ||
statusMessage: 'Unprocessable Content', | ||
data: "User already exists" | ||
}); | ||
} | ||
|
||
const hashedPassword = await useHashPassword(password); | ||
|
||
const newUser = await usePrisma().user.create({ | ||
data: { | ||
username, | ||
email, | ||
password: hashedPassword | ||
}, | ||
select: { | ||
id: true, | ||
email: true, | ||
username: true, | ||
image: true | ||
} | ||
}); | ||
|
||
setCookie(event, 'auth_token', useGenerateToken(newUser.id)); | ||
setResponseStatus(event, 201); | ||
|
||
return { | ||
id: newUser.id, | ||
username: newUser.username, | ||
email: newUser.email, | ||
image: newUser.image, | ||
} | ||
}); | ||
|
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,15 @@ | ||
export default defineEventHandler(async (event) => { | ||
const id = getRouterParam(event, 'id'); | ||
|
||
const profile = await usePrisma().profile.findUnique({ | ||
where: { | ||
id | ||
}, | ||
select: { | ||
id: true, | ||
username: true, | ||
bio: true, | ||
image: true, | ||
} | ||
}) | ||
}); |
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,27 @@ | ||
import {z} from "zod"; | ||
|
||
const profileSchema = z.object({ | ||
image: z.string().url().optional(), | ||
bio: z.string().optional(), | ||
}); | ||
|
||
export default defineEventHandler(async (event) => { | ||
useCheckAuth('required'); | ||
|
||
const id = getRouterParam(event, 'id'); | ||
const body = readValidatedBody(event, profileSchema.parse); | ||
|
||
const updatedProfile = await usePrisma().update({ | ||
where: { | ||
id | ||
}, | ||
select: { | ||
id: true, | ||
username: true, | ||
bio: true, | ||
image: true, | ||
} | ||
}); | ||
|
||
return updatedProfile; | ||
}); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.