allow wishlist to be not public

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2022-02-12 22:26:38 +01:00
parent 618c501221
commit d085d4b762
9 changed files with 51 additions and 21 deletions

View file

@ -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;

View file

@ -12,6 +12,7 @@ datasource db {
model Wishlist {
id String @id @default(uuid())
public Boolean @default(true)
title String
imageSrc String
slugUrlText String @unique

View file

@ -31,6 +31,7 @@ const wishlistData: Prisma.WishlistCreateInput[] = [
},
{
title: 'Wedding',
public: false,
imageSrc:
'https://unsplash.com/photos/8vaQKYnawHw/download?ixid=MnwxMjA3fDB8MXxhbGx8fHx8fHx8fHwxNjQ0MDQ4MTIy&force=true&w=200',
description: 'We are getting married',

View file

@ -8,16 +8,11 @@ export default {
if (!process.env.API_KEY) {
throw new Error('ENV API_KEY is not set!')
}
app.decorateRequest('isAuthenticated', false)
app.addHook(
'onRequest',
(request: FastifyRequest, reply: FastifyReply, done) => {
//@ts-expect-error: custom attribute
if (!reply.context.config.protected) {
return done()
}
if (!request.headers.authorization) {
return done(error)
}
if (request.headers.authorization) {
const authHeader = request.headers.authorization.split(' ')
request.log.debug(authHeader)
if (
@ -26,10 +21,15 @@ export default {
authHeader[1]
) {
if (authHeader[1] === process.env.API_KEY) {
return done()
request.isAuthenticated = true
}
}
}
if (reply.context.config.protected && !request.isAuthenticated) {
done(error)
} else {
done()
}
}
)
},

View file

@ -5,6 +5,15 @@ import cors from 'fastify-cors'
import { fastify as defaultConfig } from './'
import auth from './auth'
declare module 'fastify' {
interface FastifyRequest {
isAuthenticated: boolean
}
interface FastifyContextConfig {
protected?: boolean
}
}
export default async (opts: FastifyContextConfig = {}) => {
const app = Fastify({
...defaultConfig,

View file

@ -2,8 +2,9 @@ import { prisma } from '../../services'
import { Wishlist, WishlistItem } from '@/types'
export default {
getAll: async (): Promise<Wishlist[]> => {
getAll: async (where?: any): Promise<Wishlist[]> => {
return (await prisma.client.wishlist.findMany({
where,
include: { items: false },
})) as Wishlist[]
},

View file

@ -13,8 +13,9 @@ export const getAll = <RouteOptions>{
},
},
},
handler: async () => {
return await wishlist.getAll()
handler: async (request) => {
const where = request.isAuthenticated ? {} : { public: true }
return await wishlist.getAll(where)
},
}

View file

@ -33,7 +33,7 @@ export const requestInterceptor = client.interceptors.request.use(
}
isLoading.value = true
error.value = null
config.headers.Authorization = token.value ? `Bearer ${token.value}` : ''
config.headers.Authorization = token.value ? `API-Key ${token.value}` : ''
return config
},

View file

@ -9,6 +9,7 @@ export interface WishlistItem {
}
export interface Wishlist {
id?: string
public: boolean
title: string
description: string
imageSrc: string