Skip to content

Commit

Permalink
feat: Follow nuxt-s3 authorization guidelines https://nuxt-s3.bg.tn/u…
Browse files Browse the repository at this point in the history
  • Loading branch information
becem-gharbi committed Nov 15, 2023
1 parent 253acb1 commit 50e3602
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions components/Account/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async function updateAccount() {
if (model.value.file) {
const url = await upload(model.value.file, {
url: model.value.picture,
prefix: `${user.value?.id}/`
});
model.value.picture = url;
Expand Down
11 changes: 9 additions & 2 deletions server/middleware/s3.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { getKey } from '#s3'

export default defineEventHandler((event) => {
const isS3Mutation = getRequestURL(event).pathname.includes("s3/mutation");
const { pathname } = getRequestURL(event);
const isS3Mutation = pathname.startsWith("/api/s3/mutation");

if (isS3Mutation) {
const userId = event.context.auth?.userId;

if (!userId) {
const key = getKey(event)

const userIdFromKey = key.split('/')[0]

if (!userId || userId !== userIdFromKey) {
throw new Error("unauthorized");
}
}
Expand Down

0 comments on commit 50e3602

Please sign in to comment.