mirror of
https://github.com/ThisIsBenny/wishlist-app.git
synced 2025-04-19 23:37:41 +00:00
allow wishlist to be not public
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
parent
618c501221
commit
d085d4b762
9 changed files with 51 additions and 21 deletions
|
@ -0,0 +1,16 @@
|
||||||
|
-- RedefineTables
|
||||||
|
PRAGMA foreign_keys=OFF;
|
||||||
|
CREATE TABLE "new_Wishlist" (
|
||||||
|
"id" TEXT NOT NULL PRIMARY KEY,
|
||||||
|
"public" BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
"title" TEXT NOT NULL,
|
||||||
|
"imageSrc" TEXT NOT NULL,
|
||||||
|
"slugUrlText" TEXT NOT NULL,
|
||||||
|
"description" TEXT NOT NULL DEFAULT ''
|
||||||
|
);
|
||||||
|
INSERT INTO "new_Wishlist" ("description", "id", "imageSrc", "slugUrlText", "title") SELECT "description", "id", "imageSrc", "slugUrlText", "title" FROM "Wishlist";
|
||||||
|
DROP TABLE "Wishlist";
|
||||||
|
ALTER TABLE "new_Wishlist" RENAME TO "Wishlist";
|
||||||
|
CREATE UNIQUE INDEX "Wishlist_slugUrlText_key" ON "Wishlist"("slugUrlText");
|
||||||
|
PRAGMA foreign_key_check;
|
||||||
|
PRAGMA foreign_keys=ON;
|
|
@ -12,6 +12,7 @@ datasource db {
|
||||||
|
|
||||||
model Wishlist {
|
model Wishlist {
|
||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
|
public Boolean @default(true)
|
||||||
title String
|
title String
|
||||||
imageSrc String
|
imageSrc String
|
||||||
slugUrlText String @unique
|
slugUrlText String @unique
|
||||||
|
|
|
@ -31,6 +31,7 @@ const wishlistData: Prisma.WishlistCreateInput[] = [
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
title: 'Wedding',
|
title: 'Wedding',
|
||||||
|
public: false,
|
||||||
imageSrc:
|
imageSrc:
|
||||||
'https://unsplash.com/photos/8vaQKYnawHw/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjQ0MDQ4MTIy&force=true&w=200',
|
'https://unsplash.com/photos/8vaQKYnawHw/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjQ0MDQ4MTIy&force=true&w=200',
|
||||||
description: 'We are getting married',
|
description: 'We are getting married',
|
||||||
|
|
|
@ -8,16 +8,11 @@ export default {
|
||||||
if (!process.env.API_KEY) {
|
if (!process.env.API_KEY) {
|
||||||
throw new Error('ENV API_KEY is not set!')
|
throw new Error('ENV API_KEY is not set!')
|
||||||
}
|
}
|
||||||
|
app.decorateRequest('isAuthenticated', false)
|
||||||
app.addHook(
|
app.addHook(
|
||||||
'onRequest',
|
'onRequest',
|
||||||
(request: FastifyRequest, reply: FastifyReply, done) => {
|
(request: FastifyRequest, reply: FastifyReply, done) => {
|
||||||
//@ts-expect-error: custom attribute
|
if (request.headers.authorization) {
|
||||||
if (!reply.context.config.protected) {
|
|
||||||
return done()
|
|
||||||
}
|
|
||||||
if (!request.headers.authorization) {
|
|
||||||
return done(error)
|
|
||||||
}
|
|
||||||
const authHeader = request.headers.authorization.split(' ')
|
const authHeader = request.headers.authorization.split(' ')
|
||||||
request.log.debug(authHeader)
|
request.log.debug(authHeader)
|
||||||
if (
|
if (
|
||||||
|
@ -26,10 +21,15 @@ export default {
|
||||||
authHeader[1]
|
authHeader[1]
|
||||||
) {
|
) {
|
||||||
if (authHeader[1] === process.env.API_KEY) {
|
if (authHeader[1] === process.env.API_KEY) {
|
||||||
return done()
|
request.isAuthenticated = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (reply.context.config.protected && !request.isAuthenticated) {
|
||||||
done(error)
|
done(error)
|
||||||
|
} else {
|
||||||
|
done()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,6 +5,15 @@ import cors from 'fastify-cors'
|
||||||
import { fastify as defaultConfig } from './'
|
import { fastify as defaultConfig } from './'
|
||||||
import auth from './auth'
|
import auth from './auth'
|
||||||
|
|
||||||
|
declare module 'fastify' {
|
||||||
|
interface FastifyRequest {
|
||||||
|
isAuthenticated: boolean
|
||||||
|
}
|
||||||
|
interface FastifyContextConfig {
|
||||||
|
protected?: boolean
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default async (opts: FastifyContextConfig = {}) => {
|
export default async (opts: FastifyContextConfig = {}) => {
|
||||||
const app = Fastify({
|
const app = Fastify({
|
||||||
...defaultConfig,
|
...defaultConfig,
|
||||||
|
|
|
@ -2,8 +2,9 @@ import { prisma } from '../../services'
|
||||||
import { Wishlist, WishlistItem } from '@/types'
|
import { Wishlist, WishlistItem } from '@/types'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
getAll: async (): Promise<Wishlist[]> => {
|
getAll: async (where?: any): Promise<Wishlist[]> => {
|
||||||
return (await prisma.client.wishlist.findMany({
|
return (await prisma.client.wishlist.findMany({
|
||||||
|
where,
|
||||||
include: { items: false },
|
include: { items: false },
|
||||||
})) as Wishlist[]
|
})) as Wishlist[]
|
||||||
},
|
},
|
||||||
|
|
|
@ -13,8 +13,9 @@ export const getAll = <RouteOptions>{
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
handler: async () => {
|
handler: async (request) => {
|
||||||
return await wishlist.getAll()
|
const where = request.isAuthenticated ? {} : { public: true }
|
||||||
|
return await wishlist.getAll(where)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ export const requestInterceptor = client.interceptors.request.use(
|
||||||
}
|
}
|
||||||
isLoading.value = true
|
isLoading.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
config.headers.Authorization = token.value ? `Bearer ${token.value}` : ''
|
config.headers.Authorization = token.value ? `API-Key ${token.value}` : ''
|
||||||
|
|
||||||
return config
|
return config
|
||||||
},
|
},
|
||||||
|
|
|
@ -9,6 +9,7 @@ export interface WishlistItem {
|
||||||
}
|
}
|
||||||
export interface Wishlist {
|
export interface Wishlist {
|
||||||
id?: string
|
id?: string
|
||||||
|
public: boolean
|
||||||
title: string
|
title: string
|
||||||
description: string
|
description: string
|
||||||
imageSrc: string
|
imageSrc: string
|
||||||
|
|
Loading…
Add table
Reference in a new issue