API's: employee onboarding, authentication through JWT
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -0,0 +1,50 @@
|
|||||||
|
.env
|
||||||
|
|
||||||
|
# Node dependencies
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Environment files
|
||||||
|
.env.*
|
||||||
|
.env.local
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.envrc
|
||||||
|
|
||||||
|
# OS files
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Build / coverage
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
coverage/
|
||||||
|
|
||||||
|
# Databases
|
||||||
|
*.sqlite
|
||||||
|
*.sqlite3
|
||||||
|
*.db
|
||||||
|
dump.sql
|
||||||
|
|
||||||
|
# Keys and certificates
|
||||||
|
*.pem
|
||||||
|
*.key
|
||||||
|
*.crt
|
||||||
|
*.p12
|
||||||
|
|
||||||
|
# IDE / editor
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# Secrets and credentials
|
||||||
|
secrets.env
|
||||||
|
credentials.json
|
||||||
|
|
||||||
|
# Temporary
|
||||||
|
tmp/
|
||||||
|
temp/
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
const { Pool } = require('pg');
|
||||||
|
require('dotenv').config();
|
||||||
|
const pool = new Pool({
|
||||||
|
host: process.env.PG_HOST,
|
||||||
|
port: process.env.PG_PORT,
|
||||||
|
user: process.env.PG_USER,
|
||||||
|
password: process.env.PG_PASS,
|
||||||
|
database: process.env.PG_DB,
|
||||||
|
max: 10,
|
||||||
|
idleTimeoutMillis: 30000
|
||||||
|
});
|
||||||
|
|
||||||
|
pool.query('SELECT 1')
|
||||||
|
.then(() => console.log('PostgreSQL connection OK'))
|
||||||
|
.catch(err => {
|
||||||
|
console.error('PostgreSQL connection FAILED');
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
query: (text, params) => pool.query(text, params),
|
||||||
|
pool
|
||||||
|
};
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const bcrypt = require('bcryptjs');
|
||||||
|
const jwt = require('jsonwebtoken');
|
||||||
|
const db = require('./db');
|
||||||
|
require('dotenv').config();
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
const JWT_SECRET = process.env.JWT_SECRET;
|
||||||
|
|
||||||
|
|
||||||
|
app.post('/login', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const { userCode, password } = req.body;
|
||||||
|
if (!userCode || !password) return res.status(400).json({ error: 'userCode and password required' });
|
||||||
|
|
||||||
|
const result = await db.query('SELECT id, usercode, password FROM workers.users WHERE usercode = $1', [userCode]);
|
||||||
|
const user = result.rows[0];
|
||||||
|
if (!user) return res.status(401).json({ error: 'invalid_credentials' });
|
||||||
|
|
||||||
|
const ok = await bcrypt.compare(password, user.password);
|
||||||
|
if (!ok) return res.status(401).json({ error: 'invalid_credentials' });
|
||||||
|
|
||||||
|
const payload = { userId: user.id, userCode: user.usercode };
|
||||||
|
const token = jwt.sign(payload, JWT_SECRET, { expiresIn: '1h' });
|
||||||
|
|
||||||
|
res.json({ token, user: { id: user.id, userCode: user.userCode, job: user.job } });
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).json({ error: 'internal_error' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get('/me', async (req, res) => {
|
||||||
|
const auth = req.headers.authorization;
|
||||||
|
if (!auth || !auth.startsWith('Bearer ')) return res.status(401).json({ error: 'missing_token' });
|
||||||
|
const token = auth.slice(7);
|
||||||
|
try {
|
||||||
|
const payload = jwt.verify(token, JWT_SECRET);
|
||||||
|
res.json({ user: payload });
|
||||||
|
} catch (err) {
|
||||||
|
res.status(401).json({ error: 'invalid_token' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const PORT = process.env.PORT || 5000;
|
||||||
|
app.listen(PORT, () => console.log(`Auth API listening on ${PORT}`));
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
Generated
+1129
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "auth_api",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Authentication API",
|
||||||
|
"license": "ISC",
|
||||||
|
"author": "",
|
||||||
|
"type": "commonjs",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node index.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"bcryptjs": "^2.4.3",
|
||||||
|
"dotenv": "^16.0.3",
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"jsonwebtoken": "^9.0.0",
|
||||||
|
"pg": "^8.11.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
.env
|
||||||
|
|
||||||
|
# Node dependencies
|
||||||
|
node_modules/
|
||||||
|
|
||||||
|
# Logs
|
||||||
|
npm-debug.log*
|
||||||
|
yarn-debug.log*
|
||||||
|
yarn-error.log*
|
||||||
|
*.log
|
||||||
|
|
||||||
|
# Environment files
|
||||||
|
.env.*
|
||||||
|
.env.local
|
||||||
|
.env.development.local
|
||||||
|
.env.test.local
|
||||||
|
.env.production.local
|
||||||
|
.envrc
|
||||||
|
|
||||||
|
# OS files
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# Build / coverage
|
||||||
|
dist/
|
||||||
|
build/
|
||||||
|
coverage/
|
||||||
|
|
||||||
|
# Databases
|
||||||
|
*.sqlite
|
||||||
|
*.sqlite3
|
||||||
|
*.db
|
||||||
|
dump.sql
|
||||||
|
|
||||||
|
# Keys and certificates
|
||||||
|
*.pem
|
||||||
|
*.key
|
||||||
|
*.crt
|
||||||
|
*.p12
|
||||||
|
|
||||||
|
# IDE / editor
|
||||||
|
.vscode/
|
||||||
|
.idea/
|
||||||
|
|
||||||
|
# Secrets and credentials
|
||||||
|
secrets.env
|
||||||
|
credentials.json
|
||||||
|
|
||||||
|
# Temporary
|
||||||
|
tmp/
|
||||||
|
temp/
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
const { Pool } = require('pg');
|
||||||
|
require('dotenv').config();
|
||||||
|
const pool = new Pool({
|
||||||
|
host: process.env.PG_HOST,
|
||||||
|
port: process.env.PG_PORT,
|
||||||
|
user: process.env.PG_USER,
|
||||||
|
password: process.env.PG_PASS,
|
||||||
|
database: process.env.PG_DB,
|
||||||
|
max: 10,
|
||||||
|
idleTimeoutMillis: 30000
|
||||||
|
});
|
||||||
|
|
||||||
|
pool.query('SELECT 1')
|
||||||
|
.then(() => console.log('PostgreSQL connection OK'))
|
||||||
|
.catch(err => {
|
||||||
|
console.error('PostgreSQL connection FAILED');
|
||||||
|
console.error(err);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
query: (text, params) => pool.query(text, params),
|
||||||
|
pool
|
||||||
|
};
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const bcrypt = require('bcryptjs');
|
||||||
|
const db = require('./db');
|
||||||
|
require('dotenv').config();
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
app.use(express.json());
|
||||||
|
|
||||||
|
// Employee onboarding
|
||||||
|
app.post('/createemployee', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const {firstname, lastname, job, password} = req.body;
|
||||||
|
if (!firstname || !lastname || !job || !password) return res.status(400).json({error: 'Firstname, Lastname, Job title and password are required'});
|
||||||
|
|
||||||
|
const resultCreateEmployee = await db.query('INSERT INTO workers.employees (firstname, lastname, job) VALUES ($1, $2, $3) RETURNING id', [firstname, lastname, job]);
|
||||||
|
const newEmployee = resultCreateEmployee.rows[0];
|
||||||
|
const encryptedpassword = await bcrypt.hash(password, 12);
|
||||||
|
const resultCreateUser = await db.query('INSERT INTO workers.users (employee, password) VALUES ($1, $2) RETURNING usercode', [newEmployee.id, encryptedpassword]);
|
||||||
|
const newUser = resultCreateUser.rows[0];
|
||||||
|
return res.status(201).json({ employee: firstname, code: newUser.usercoden });
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).json({ error: 'internal_error' });
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
app.get('/health', async (_req, res, _next) => {
|
||||||
|
try {
|
||||||
|
return res.status(200).json({endpoint: 'reachable'})
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
res.status(500).json({endpoint: 'unreachable'})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const PORT = process.env.PORT || 4000;
|
||||||
|
app.listen(PORT, () => console.log(`Auth API listening on ${PORT}`));
|
||||||
|
|
||||||
|
module.exports = app;
|
||||||
Generated
+1165
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "employee_api",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "",
|
||||||
|
"license": "ISC",
|
||||||
|
"author": "",
|
||||||
|
"type": "commonjs",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"start": "node index.js",
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"bcrypt": "^6.0.0",
|
||||||
|
"bcryptjs": "^3.0.3",
|
||||||
|
"dotenv": "^17.4.2",
|
||||||
|
"express": "^5.2.1",
|
||||||
|
"jsonwebtoken": "^9.0.3",
|
||||||
|
"pg": "^8.20.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
const { pool } = require('pg');
|
const { Pool } = require('pg');
|
||||||
require('dotenv').config();
|
require('dotenv').config();
|
||||||
const pool = new Pool({
|
const pool = new Pool({
|
||||||
host: process.env.PG_HOST,
|
host: process.env.PG_HOST,
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"type": "commonjs",
|
"type": "commonjs",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"start": "node index.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
Reference in New Issue
Block a user