Skip to content

Commit

Permalink
feat: docs/api updates
Browse files Browse the repository at this point in the history
  • Loading branch information
geromegrignon committed Sep 30, 2024
1 parent dd28b1d commit af48c65
Show file tree
Hide file tree
Showing 17 changed files with 4,124 additions and 108 deletions.
Empty file.
43 changes: 43 additions & 0 deletions apps/api/server/routes/api/auth/login.post.ts
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,
};
}
}
});
5 changes: 5 additions & 0 deletions apps/api/server/routes/api/auth/logout.post.ts
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";
});
58 changes: 58 additions & 0 deletions apps/api/server/routes/api/auth/signup.post.ts
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,
}
});

15 changes: 15 additions & 0 deletions apps/api/server/routes/api/profile/[id].get.ts
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,
}
})
});
27 changes: 27 additions & 0 deletions apps/api/server/routes/api/profile/[id].put.ts
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;
});
90 changes: 0 additions & 90 deletions apps/api/server/routes/api/users/index.post.ts

This file was deleted.

3 changes: 0 additions & 3 deletions apps/api/server/routes/api/users/login.post.ts

This file was deleted.

6 changes: 2 additions & 4 deletions apps/api/server/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import jwt from "jsonwebtoken";
import {LoginPayload} from "~/models/login-payload.model";

export const useCheckAuth = (mode: 'optional' | 'required') => (event) => {
const {authorization} = getHeaders(event);
const token = authorization?.split(' ')[1];
const token = getCookie(event, 'auth_token');

if (!token && mode === 'required') {
throw createError({
Expand All @@ -14,7 +12,7 @@ export const useCheckAuth = (mode: 'optional' | 'required') => (event) => {
}

if (token) {
const verified = jwt.verify(token, process.env.JWT_SECRET) as LoginPayload;
const verified = jwt.verify(token, process.env.JWT_SECRET);

if (!verified) {
throw createError({
Expand Down
4 changes: 4 additions & 0 deletions apps/api/server/utils/hash-password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ import bcrypt from 'bcryptjs';
export const useHashPassword = (password: string) => {
return bcrypt.hash(password, 10);
}

export const useDecrypt = (input: string, password: string) => {
return bcrypt.compare(input, password);
}
3 changes: 2 additions & 1 deletion apps/documentation/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {defineConfig} from 'astro/config';
import starlight from '@astrojs/starlight';

import tailwind from "@astrojs/tailwind";

import react from "@astrojs/react";

// https://astro.build/config
export default defineConfig({
integrations: [starlight({
Expand Down
Loading

0 comments on commit af48c65

Please sign in to comment.