Skip to content

Commit 9740f7b

Browse files
committed
✨ Implement admin JWT authentication and enhance user login with username or email
1 parent d6f3609 commit 9740f7b

File tree

20 files changed

+121
-70
lines changed

20 files changed

+121
-70
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ DATABASE_URL=
66
APP_SECRET=supersecret
77
ADMIN_KEY="admin"
88

9+
# First startup only
10+
ADMIN_USERNAME=admin
11+
ADMIN_EMAIL=[email protected]
12+
ADMIN_PASSWORD=[email protected]
13+
914
# S3
1015
FILESYSTEM=local#local or s3
1116
S3_ENDPOINT=endpoint

bun.lock

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"axios": "^1.8.4",
2222
"class-transformer": "^0.5.1",
2323
"class-validator": "^0.14.1",
24-
"dotenv": "^16.4.7",
2524
"fastify": "5.2.1",
2625
"jsdom": "^26.0.0",
2726
"jszip": "^3.10.1",

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
"axios": "^1.8.4",
3030
"class-transformer": "^0.5.1",
3131
"class-validator": "^0.14.1",
32-
"dotenv": "^16.4.7",
3332
"fastify": "5.2.1",
3433
"jsdom": "^26.0.0",
3534
"jszip": "^3.10.1",

prisma/migrations/20250328135221_user_avatar/migration.sql renamed to prisma/migrations/20250404071740_user_avatar/migration.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
/*
22
Warnings:
33
4+
- A unique constraint covering the columns `[username]` on the table `users` will be added. If there are existing duplicate values, this will fail.
45
- A unique constraint covering the columns `[avatar_id]` on the table `users` will be added. If there are existing duplicate values, this will fail.
56
67
*/
78
-- AlterTable
89
ALTER TABLE "users" ADD COLUMN "avatar_id" INTEGER;
910

11+
-- CreateIndex
12+
CREATE UNIQUE INDEX "users_username_key" ON "users"("username");
13+
1014
-- CreateIndex
1115
CREATE UNIQUE INDEX "users_avatar_id_key" ON "users"("avatar_id");
1216

prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ datasource db {
1515

1616
model Users {
1717
id String @id @db.VarChar(36)
18-
username String @db.VarChar(30)
18+
username String @unique @db.VarChar(30)
1919
email String @unique @db.VarChar(255)
2020
password String
2121
jwt_id String @db.VarChar(64)

prisma/seed.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import {PrismaClient, Users} from "@prisma/client";
2-
import * as dotenv from "dotenv";
32
import WebtoonGenres from "./../src/modules/webtoon/webtoon/models/enums/webtoon-genres";
43
import ImageTypes from "./../src/modules/webtoon/webtoon/models/enums/image-types";
54
import {MiscService} from "../src/modules/misc/misc.service";
65

7-
dotenv.config();
8-
96
// initialize Prisma Client
107
const prisma = new PrismaClient();
118
const miscService = new MiscService();
@@ -21,9 +18,9 @@ async function main(){
2118
const users_values = [
2219
{
2320
id: "0195dc7c-f315-7881-b35b-da9cbb6ee4a0",
24-
username: "admin",
25-
26-
password: miscService.hashPassword("[email protected]"),
21+
username: process.env.ADMIN_USERNAME || "admin",
22+
email: process.env.ADMIN_EMAIL || "[email protected]",
23+
password: miscService.hashPassword(process.env.ADMIN_PASSWORD || "[email protected]"),
2724
jwt_id: miscService.generateRandomBytes(32),
2825
admin: true,
2926
} as Users,

src/app.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ import {FastifyAdapter, NestFastifyApplication} from "@nestjs/platform-fastify";
22
import {LoggerMiddleware} from "./common/middlewares/logger.middleware";
33
import {SwaggerTheme, SwaggerThemeNameEnum} from "swagger-themes";
44
import {DocumentBuilder, SwaggerModule} from "@nestjs/swagger";
5+
import {FastifyListenOptions} from "fastify/types/instance";
56
import fastifyHelmet from "@fastify/helmet";
7+
import {RawServerDefault} from "fastify";
68
import {NestFactory} from "@nestjs/core";
79
import {AppModule} from "./app.module";
810
import {Logger} from "@nestjs/common";
911
import * as process from "process";
10-
import * as dotenv from "dotenv";
11-
import {FastifyListenOptions} from "fastify/types/instance";
12-
import {RawServerDefault} from "fastify";
13-
14-
dotenv.config();
1512

1613
const logger: Logger = new Logger("App");
1714

src/modules/file/file.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {Controller, Post, UseGuards} from "@nestjs/common";
22
import {ApiBearerAuth, ApiTags} from "@nestjs/swagger";
3-
import {AdminGuard} from "../webtoon/admin/guard/admin.guard";
43
import {FileService} from "./file.service";
4+
import {AuthGuard} from "@nestjs/passport";
55

66
@Controller("file")
77
@ApiTags("File")
8-
@UseGuards(AdminGuard)
8+
@UseGuards(AuthGuard("admin-jwt"))
99
export class FileController{
1010
constructor(
1111
private readonly fileService: FileService,

src/modules/misc/misc.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import axios, {AxiosInstance} from "axios";
33
import {Injectable} from "@nestjs/common";
44
import {createHash, randomBytes} from "crypto";
55
import * as JSZip from "jszip";
6-
import * as sharp from "sharp";
6+
import sharp from "sharp";
77
import * as fs from "fs";
88

99
@Injectable()

src/modules/users/models/dto/login.dto.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import {IsEmail, IsNotEmpty, IsString} from "class-validator";
1+
import {IsNotEmpty, IsString} from "class-validator";
22

33
export class LoginDto{
4-
@IsEmail()
54
@IsString()
65
@IsNotEmpty()
7-
email: string;
6+
usernameOrEmail: string;
87

98
@IsString()
109
@IsNotEmpty()

0 commit comments

Comments
 (0)