working on healthcheck api and auth api (#2)
Reviewed-on: #2 Co-authored-by: Joseph Nelson <joseph.nelson4456@gmail.com> Co-committed-by: Joseph Nelson <joseph.nelson4456@gmail.com>
This commit was merged in pull request #2.
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
import express from 'express'
|
||||
import jwt from 'jsonwebtoken'
|
||||
import bcrypt from 'bcryptjs'
|
||||
import { database } from '../../config'
|
||||
|
||||
const { Pool } = require('pg')
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
// Database connection pool
|
||||
const pool = new Pool(database)
|
||||
|
||||
// Route to handle login and issue JWT token
|
||||
router.post('/login', async (req, res) => {
|
||||
const { username, password } = req.body
|
||||
|
||||
try {
|
||||
const client = await pool.connect()
|
||||
const queryText = 'SELECT * FROM users WHERE username = $1'
|
||||
const result = await client.query(queryText, [username])
|
||||
|
||||
if (!result.rows.length) {
|
||||
return res.status(401).json({ message: 'Invalid credentials' })
|
||||
}
|
||||
|
||||
const user = result.rows[0]
|
||||
|
||||
// Compare passwords
|
||||
const passwordMatch = await bcrypt.compare(password, user.password)
|
||||
|
||||
if (!passwordMatch) {
|
||||
return res.status(401).json({ message: 'Invalid credentials' })
|
||||
}
|
||||
|
||||
// Issue JWT token
|
||||
const token = jwt.sign(
|
||||
{ id: user.id, username: user.username },
|
||||
process.env.JWT_SECRET,
|
||||
{ expiresIn: '1h' }
|
||||
)
|
||||
|
||||
res.json({ token })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(500).json({ message: 'Internal server error' })
|
||||
}
|
||||
})
|
||||
|
||||
// Route to refresh JWT token
|
||||
router.post('/refresh-token', async (req, res) => {
|
||||
const { refreshToken } = req.body
|
||||
|
||||
if (!refreshToken) {
|
||||
return res.status(401).json({ message: 'Refresh token is required' })
|
||||
}
|
||||
|
||||
try {
|
||||
// Verify the refresh token
|
||||
const decoded = jwt.verify(refreshToken, process.env.JWT_SECRET)
|
||||
|
||||
// Create a new access token (JWT)
|
||||
const token = jwt.sign(
|
||||
{ id: decoded.id, username: decoded.username },
|
||||
process.env.JWT_SECRET,
|
||||
{ expiresIn: '1h' }
|
||||
)
|
||||
|
||||
res.json({ token })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
res.status(403).json({ message: 'Invalid refresh token' })
|
||||
}
|
||||
})
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,7 @@
|
||||
import express from 'express'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', (req, res) => res.json({ status: 'UP' }))
|
||||
|
||||
export default router
|
||||
@@ -0,0 +1,19 @@
|
||||
// src/routes/healthcheck/test.js
|
||||
|
||||
import request from 'supertest'
|
||||
import app from '../../app'
|
||||
|
||||
describe('Health Check Endpoint', () => {
|
||||
it('should respond with status up', async () => {
|
||||
const response = await request(app).get('/healthcheck')
|
||||
|
||||
expect(response.status).toBe(200)
|
||||
expect(response.body).toEqual({ status: 'UP' })
|
||||
})
|
||||
|
||||
it('should return a JSON response', async () => {
|
||||
const response = await request(app).get('/healthcheck')
|
||||
|
||||
expect(response.type).toBe('application/json')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user