Skip to content
This repository was archived by the owner on Jun 29, 2025. It is now read-only.

Commit 84d29df

Browse files
committed
feat: allow unauthenticated uploads
1 parent 41c3baf commit 84d29df

File tree

17 files changed

+340
-249
lines changed

17 files changed

+340
-249
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ APP_URL=http://localhost:3000
55
SHOW_HOME_PAGE=true
66
ALLOW_REGISTRATION=true
77
MAX_FILE_SIZE=1000000000
8+
ALLOW_UNAUTHENTICATED_SHARES=false
89

910
# SECURITY
1011
JWT_SECRET=long-random-string

backend/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
APP_URL=http://localhost:3000
33
ALLOW_REGISTRATION=true
44
MAX_FILE_SIZE=5000000000
5+
ALLOW_UNAUTHENTICATED_SHARES=false
56

67
# SECURITY
78
JWT_SECRET=random-string

backend/prisma/schema.prisma

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ model Share {
4040
views Int @default(0)
4141
expiration DateTime
4242
43-
creatorId String
44-
creator User @relation(fields: [creatorId], references: [id])
43+
creatorId String?
44+
creator User? @relation(fields: [creatorId], references: [id])
4545
security ShareSecurity?
4646
files File[]
4747
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
import { ExecutionContext } from "@nestjs/common";
12
import { AuthGuard } from "@nestjs/passport";
3+
import { Observable } from "rxjs";
24

35
export class JwtGuard extends AuthGuard("jwt") {
46
constructor() {
57
super();
68
}
9+
canActivate(
10+
context: ExecutionContext
11+
): boolean | Promise<boolean> | Observable<boolean> {
12+
return process.env.ALLOW_UNAUTHENTICATED_SHARES == "true" ? true : super.canActivate(context);
13+
}
714
}

backend/src/auth/strategy/jwt.strategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class JwtStrategy extends PassportStrategy(Strategy) {
1111
super({
1212
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
1313
secretOrKey: config.get("JWT_SECRET"),
14+
1415
});
1516
}
1617

backend/src/share/guard/shareOwner.guard.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ export class ShareOwnerGuard implements CanActivate {
2828

2929
if (!share) throw new NotFoundException("Share not found");
3030

31+
if(!share.creatorId) return true;
32+
3133
return share.creatorId == (request.user as User).id;
3234
}
3335
}

backend/src/share/share.service.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class ShareService {
2424
private jwtService: JwtService
2525
) {}
2626

27-
async create(share: CreateShareDTO, user: User) {
27+
async create(share: CreateShareDTO, user?: User) {
2828
if (!(await this.isShareIdAvailable(share.id)).isAvailable)
2929
throw new BadRequestException("Share id already in use");
3030

@@ -58,7 +58,7 @@ export class ShareService {
5858
data: {
5959
...share,
6060
expiration: expirationDate,
61-
creator: { connect: { id: user.id } },
61+
creator: { connect: user ? { id: user.id } : undefined },
6262
security: { create: share.security },
6363
},
6464
});
@@ -154,6 +154,8 @@ export class ShareService {
154154
});
155155

156156
if (!share) throw new NotFoundException("Share not found");
157+
if (!share.creatorId)
158+
throw new ForbiddenException("Anonymous shares can't be deleted");
157159

158160
await this.fileService.deleteAllFiles(shareId);
159161
await this.prisma.share.delete({ where: { id: shareId } });

frontend/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
SHOW_HOME_PAGE=true
22
ALLOW_REGISTRATION=true
33
MAX_FILE_SIZE=1000000000
4+
ALLOW_UNAUTHENTICATED_SHARES=false

frontend/next.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const nextConfig = {
55
ALLOW_REGISTRATION: process.env.ALLOW_REGISTRATION,
66
SHOW_HOME_PAGE: process.env.SHOW_HOME_PAGE,
77
MAX_FILE_SIZE: process.env.MAX_FILE_SIZE,
8+
ALLOW_UNAUTHENTICATED_SHARES: process.env.ALLOW_UNAUTHENTICATED_SHARES
89
}
910
}
1011

frontend/src/components/share/CreateUploadModalBody.tsx

Lines changed: 0 additions & 219 deletions
This file was deleted.

0 commit comments

Comments
 (0)