Skip to content
Merged
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
175 changes: 174 additions & 1 deletion package-lock.json

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

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
"dependencies": {
"bcryptjs": "^3.0.3",
"cors": "^2.8.6",
"dotenv": "^17.4.2",
"express": "^5.2.1",
"jsonwebtoken": "^9.0.3"
"jsonwebtoken": "^9.0.3",
"pg": "^8.21.0"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.6",
"@types/cors": "^2.8.19",
"@types/express": "^5.0.6",
"@types/jsonwebtoken": "^9.0.10",
"@types/pg": "^8.20.0",
"@types/supertest": "^7.2.0",
"supertest": "^7.2.2",
"tsx": "^4.22.4",
Expand Down
11 changes: 11 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'dotenv/config'

export const config = {
db: {
host: process.env.DB_HOST ?? 'localhost',
port: Number(process.env.DB_PORT ?? 5432),
database: process.env.DB_NAME ?? 'taskforge',
user: process.env.DB_USER ?? 'postgres',
password: process.env.DB_PASSWORD ?? (() => { throw new Error('DB_PASSWORD no está definida') })(),

Check failure on line 9 in src/config.ts

View workflow job for this annotation

GitHub Actions / test (20)

tests/health.test.ts

Error: DB_PASSWORD no está definida ❯ src/config.ts:9:61 ❯ src/config.ts:9:103 ❯ src/db/pool.ts:2:1 ❯ src/repositories/userRepository.ts:1:1

Check failure on line 9 in src/config.ts

View workflow job for this annotation

GitHub Actions / test (20)

tests/auth.test.ts

Error: DB_PASSWORD no está definida ❯ src/config.ts:9:61 ❯ src/config.ts:9:103 ❯ src/db/pool.ts:2:1 ❯ src/repositories/userRepository.ts:1:1

Check failure on line 9 in src/config.ts

View workflow job for this annotation

GitHub Actions / test (22)

tests/health.test.ts

Error: DB_PASSWORD no está definida ❯ src/config.ts:9:61 ❯ src/config.ts:9:103 ❯ src/db/pool.ts:2:1

Check failure on line 9 in src/config.ts

View workflow job for this annotation

GitHub Actions / test (22)

tests/auth.test.ts

Error: DB_PASSWORD no está definida ❯ src/config.ts:9:61 ❯ src/config.ts:9:103 ❯ src/db/pool.ts:2:1
}
}
8 changes: 8 additions & 0 deletions src/db/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pg from 'pg'
import { config } from '../config.js'

const { Pool } = pg

export const pool = new Pool(config.db)


23 changes: 23 additions & 0 deletions src/repositories/userRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { pool } from '../db/pool.js'

export interface User {
id: string
email: string
password: string
name: string
createdAt: Date
}

export async function createUser(email: string, password: string, name: string): Promise<User> {
const result = await pool.query(
'INSERT INTO users (email, password, name) VALUES ($1, $2, $3) RETURNING *', [email, password, name]
)
return result.rows[0]
}

export async function findByEmail(email: string): Promise<User | undefined> {
const result = await pool.query(
'SELECT * FROM users WHERE email = $1', [email]
)
return result.rows[0] ?? undefined
}
21 changes: 0 additions & 21 deletions src/repositories/userStore.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/routes/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import { loginUser } from '../services/auth.js'

const router = Router()

router.post('/register', (req: Request, res: Response) => {
router.post('/register', async (req: Request, res: Response) => {
const { email, password, name } = req.body
const result = registerUser(email, password, name)
const result = await registerUser(email, password, name)
res.json(result)
})

router.post('/login', (req: Request, res: Response) => {
router.post('/login', async (req: Request, res: Response) => {
const { email, password } = req.body
const result = loginUser(email, password)
const result = await loginUser(email, password)
res.json(result)
})

Expand Down
12 changes: 6 additions & 6 deletions src/services/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import bcrypt from 'bcryptjs'
import jwt from 'jsonwebtoken'
import { createUser, findByEmail } from '../repositories/userStore.js'
import { createUser, findByEmail } from '../repositories/userRepository.js'
import { AppError } from '../errors/AppError.js'


Expand All @@ -11,23 +11,23 @@ export function generateTokens(userId: string) {
return { accessToken, refreshToken }
}

export function registerUser(email: string, password: string, name: string) {
const existing = findByEmail(email)
export async function registerUser(email: string, password: string, name: string) {
const existing = await findByEmail(email)

if (existing) {
throw new AppError(409, 'CONFLICT', 'Email alredy registered')
}

const hashedPassword = bcrypt.hashSync(password, 10)
const user = createUser(email, hashedPassword, name)
const user = await createUser(email, hashedPassword, name)
const token = generateTokens(user.id)

return { ...token, user }

}

export function loginUser(email: string, password: string) {
const user = findByEmail(email)
export async function loginUser(email: string, password: string) {
const user = await findByEmail(email)

if (!user) {
throw new AppError(401, 'UNAUTHORIZED', 'Invalid email or password')
Expand Down
7 changes: 6 additions & 1 deletion tests/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { describe, it, expect } from 'vitest'
import { describe, it, expect, beforeAll } from 'vitest'
import supertest from 'supertest'
import { app } from '../src/app.js'
import { pool } from '../src/db/pool.js'

beforeAll(async () => {
await pool.query('TRUNCATE TABLE users CASCADE')
})

describe('POST /auth/register', () => {
it('registra un usuario y devuelve token + user', async () => {
Expand Down
Loading