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 { 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

View file

@ -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',

View file

@ -8,28 +8,28 @@ 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) { const authHeader = request.headers.authorization.split(' ')
return done() request.log.debug(authHeader)
} if (
if (!request.headers.authorization) { authHeader[0] &&
return done(error) authHeader[0].trim().toLowerCase() === 'api-key' &&
} authHeader[1]
const authHeader = request.headers.authorization.split(' ') ) {
request.log.debug(authHeader) if (authHeader[1] === process.env.API_KEY) {
if ( request.isAuthenticated = true
authHeader[0] && }
authHeader[0].trim().toLowerCase() === 'api-key' &&
authHeader[1]
) {
if (authHeader[1] === process.env.API_KEY) {
return done()
} }
} }
done(error) 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 { 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,

View file

@ -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[]
}, },

View file

@ -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)
}, },
} }

View file

@ -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
}, },

View file

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