Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

DATABASE_URL="prisma+postgres://localhost:51213/?api_key=eyJkYXRhYmFzZVVybCI6InBvc3RncmVzOi8vcG9zdGdyZXM6cG9zdGdyZXNAbG9jYWxob3N0OjUxMjE0L3RlbXBsYXRlMT9zc2xtb2RlPWRpc2FibGUmY29ubmVjdGlvbl9saW1pdD0xMCZjb25uZWN0X3RpbWVvdXQ9MCZtYXhfaWRsZV9jb25uZWN0aW9uX2xpZmV0aW1lPTAmcG9vbF90aW1lb3V0PTAmc29ja2V0X3RpbWVvdXQ9MCIsIm5hbWUiOiJkZWZhdWx0Iiwic2hhZG93RGF0YWJhc2VVcmwiOiJwb3N0Z3JlczovL3Bvc3RncmVzOnBvc3RncmVzQGxvY2FsaG9zdDo1MTIxNS90ZW1wbGF0ZTE_c3NsbW9kZT1kaXNhYmxlJmNvbm5lY3Rpb25fbGltaXQ9MTAmY29ubmVjdF90aW1lb3V0PTAmbWF4X2lkbGVfY29ubmVjdGlvbl9saWZldGltZT0wJnBvb2xfdGltZW91dD0wJnNvY2tldF90aW1lb3V0PTAifQ"
ACCESS_TOKEN_SECRET=71b4c7d01c2cb2f2dd4ec05e0d991489c63194e1a9725e450342090468c262e5be5d9bd61813496a6942a9845fd039f1bafb736bbc97715ac210e807b119cdfb
REFRESH_TOKEN_SECRET=71b4c7d01c2cb2f2dd4ec05e0d991489c63194e1a9725e450342090468c262e5be5d9bd61813496a6942a9845fd039f1bafb736bbc97715ac210e807b119cdfb
REFRESH_TOKEN_SECRET=71b4c7d01c2cb2f2dd4ec05e0d991489c63194e1a9725e450342090468c262e5be5d9bd61813496a6942a9845fd039f1bafb736bbc97715ac210e807b119cdfb
JWT_SECRET=71b4c7d01c2cb2f2dd4ec05e0d991489c63194e1a9725e450342090468c262e5
12 changes: 6 additions & 6 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
DATABASE_URL=
PORT=
DATABASE_URL="file:./dev.db"
PORT=3000
REDIS_URI_WITH_AUTH=
APP_ENV=development
SERVER_TIME_ZONE=
SERVER_TIME_ZONE=Asia/Qatar
CORS_ORIGIN=*
RATE_LIMIT_WINDOW=
RATE_LIMIT_MAX=
JWT_SECRET=
RATE_LIMIT_WINDOW=1 minute
RATE_LIMIT_MAX=120
JWT_SECRET=my_super_secret_key_12345
JWT_ACCESS_EXPIRES_IN=15m
JWT_REFRESH_EXPIRES_IN=7d
47 changes: 46 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@fastify/rate-limit": "^10.3.0",
"@fastify/sensible": "^6.0.0",
"@prisma/client": "^5.19.1",
"bcrypt": "^6.0.0",
"bcryptjs": "^3.0.3",
"chalk": "^5.6.2",
"dotenv": "^17.4.2",
Expand All @@ -34,6 +35,7 @@
"zod": "^4.3.6"
},
"devDependencies": {
"@types/bcrypt": "^6.0.0",
"@types/bcryptjs": "^2.4.6",
"@types/jsonwebtoken": "^9.0.10",
"@types/node": "^25.0.3",
Expand Down
Binary file added prisma/dev.db
Binary file not shown.
48 changes: 48 additions & 0 deletions prisma/migrations/20260507135532_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- CreateTable
CREATE TABLE "Users" (
"id" TEXT NOT NULL PRIMARY KEY,
"email" TEXT NOT NULL,
"firstName" TEXT,
"lastName" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "UserAuths" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"passwordHash" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "UserAuths_userId_fkey" FOREIGN KEY ("userId") REFERENCES "Users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "UserVerifications" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"token" TEXT NOT NULL,
"deviceId" TEXT NOT NULL,
"expiresAt" DATETIME NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "UserVerifications_userId_fkey" FOREIGN KEY ("userId") REFERENCES "Users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "UserTokens" (
"id" TEXT NOT NULL PRIMARY KEY,
"userId" TEXT NOT NULL,
"resetPasswordToken" TEXT,
"resetPasswordExpires" DATETIME,
"refreshToken" TEXT NOT NULL,
"accessToken" TEXT NOT NULL,
"deviceId" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
CONSTRAINT "UserTokens_userId_fkey" FOREIGN KEY ("userId") REFERENCES "Users" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateIndex
CREATE UNIQUE INDEX "Users_email_key" ON "Users"("email");
4 changes: 4 additions & 0 deletions prisma/migrations/20260510205009_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- AlterTable
ALTER TABLE "Users" ADD COLUMN "bio" TEXT;
ALTER TABLE "Users" ADD COLUMN "phone" TEXT;
ALTER TABLE "Users" ADD COLUMN "profilePicture" TEXT;
14 changes: 9 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
provider = "sqlite"
url = "file:./dev.db"
}

model Users {
Expand All @@ -13,6 +13,8 @@ model Users {
firstName String?
lastName String?
phone String?
profilePicture String?
bio String?
company String?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
Expand All @@ -26,8 +28,8 @@ model Users {
model UserAuths {
id String @id @default(uuid())
userId String @unique
passwordHash String
recognisedDevices String[] @default([])
passwordHash String
recognisedDevices String @default("")
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user Users @relation(fields: [userId], references: [id])
Expand All @@ -46,7 +48,9 @@ model UserVerifications {

model UserTokens {
id String @id @default(uuid())
userId String @unique
userId String @unique
resetPasswordToken String?
resetPasswordExpires DateTime?
refreshToken String
accessToken String
deviceId String
Expand Down
27 changes: 25 additions & 2 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import authService from "../services/auth.service";
import AuthService from "../services/auth.service";
import {FastifyReply, FastifyRequest} from "fastify";
import {sendResponse} from "../helpers";
import {LoginRequest, SignupRequest, VerifyDeviceChangeRequest, RefreshTokenRequest} from "../schemas";
import {LoginRequest, SignupRequest,ForgotPasswordRequest, ResetPasswordRequest,VerifyDeviceChangeRequest,RefreshTokenRequest} from "../schemas";
//import {LoginRequest, SignupRequest, VerifyDeviceChangeRequest, RefreshTokenRequest} from "../schemas";

class AuthController {
constructor() {
Expand Down Expand Up @@ -31,16 +32,36 @@ class AuthController {
});
return sendResponse(reply, result);
}
public static async forgotPassword(request: FastifyRequest, reply: FastifyReply) {
const { email } = ForgotPasswordRequest.parse(request.body ?? {});
const result = await AuthService.forgotPassword({
deviceId: <string>request.headers['x-device-id'],
email,
});
return sendResponse(reply, result);
}

public static async verifyDeviceChange(request: FastifyRequest, reply: FastifyReply) {
public static async verifyDeviceChange(request: FastifyRequest, reply: FastifyReply) {
const {otp} = VerifyDeviceChangeRequest.parse(request.body ?? {});
const result = await AuthService.verifyDeviceChange({
deviceId: <string>request.headers['x-device-id'],
otp,
userId: ""
});
return sendResponse(reply, result);
}


public static async resetPassword(request: FastifyRequest, reply: FastifyReply) {
const { token, newPassword } = ResetPasswordRequest.parse(request.body ?? {});
const result = await AuthService.resetPassword({
deviceId: <string>request.headers['x-device-id'],
token,
newPassword,
});
return sendResponse(reply, result);
}

public static async refreshToken(request: FastifyRequest, reply: FastifyReply) {
const {refreshToken} = RefreshTokenRequest.parse(request.body ?? {});
const result = await AuthService.refreshToken({
Expand All @@ -50,6 +71,8 @@ class AuthController {
return sendResponse(reply, result);
}



}

export const AuthenticationController = AuthController;
Expand Down
Loading