Skip to content

Commit 52a11b8

Browse files
committed
Add login and logout endpoints
1 parent 2e89e6f commit 52a11b8

File tree

5 files changed

+53
-1
lines changed

5 files changed

+53
-1
lines changed

backend/user/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@
1616
"license": "ISC",
1717
"description": "",
1818
"dependencies": {
19+
"bcrypt": "^5.1.1",
1920
"drizzle-orm": "^0.33.0",
2021
"express": "^4.21.0",
22+
"jsonwebtoken": "^9.0.2",
2123
"pino": "^9.4.0",
2224
"pino-http": "^10.3.0",
2325
"postgres": "^3.4.4"
2426
},
2527
"devDependencies": {
28+
"@types/bcrypt": "^5.0.2",
2629
"@types/express": "^4.17.21",
30+
"@types/jsonwebtoken": "^9.0.6",
2731
"@types/node": "^22.5.5",
2832
"drizzle-kit": "^0.24.2",
2933
"nodemon": "^3.1.4",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Request, Response } from 'express';
2+
import { LoginCredentials } from './types/auth-types';
3+
import { db, users } from '../../lib/db';
4+
import { eq } from 'drizzle-orm';
5+
import bcrypt from 'bcrypt';
6+
import jwt from 'jsonwebtoken';
7+
8+
export async function login(req: Request, res: Response) {
9+
const { username, password }: LoginCredentials = req.body;
10+
const userArray = await db.select().from(users).where(eq(users.username, username));
11+
if (userArray.length === 0) {
12+
return res.status(404).json('Account does not exist');
13+
}
14+
15+
const user = userArray[0];
16+
const checkPassword = bcrypt.compareSync(password, user.password);
17+
if (!checkPassword) {
18+
return res.status(401).json('Incorrect Password');
19+
}
20+
21+
const { password: _userPassword, ...userDetails } = user;
22+
const jwtToken = jwt.sign({ id: user.id }, 'key');
23+
return res.cookie('jwtToken', jwtToken, { httpOnly: true }).status(200).json(userDetails);
24+
}
25+
26+
export async function logout(_req: Request, res: Response) {
27+
return res
28+
.clearCookie('jwtToken', {
29+
secure: true,
30+
sameSite: 'none',
31+
})
32+
.status(200)
33+
.json('User has been logged out.');
34+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface LoginCredentials {
2+
username: string;
3+
password: string;
4+
}

backend/user/src/routes/auth.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import express from 'express';
2+
import { login, logout } from '../controllers/auth/auth-controller';
3+
4+
const router = express.Router();
5+
6+
router.post('/login', login);
7+
router.post('/logout', logout);
8+
9+
export default router;

backend/user/src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import express, { json } from 'express';
22
import pino from 'pino-http';
33
import { db, users } from './lib/db';
4+
import authRoutes from './routes/auth';
45

56
const app = express();
67
app.use(pino());
78
app.use(json());
8-
9+
app.use('/auth', authRoutes);
910
app.get('/', async (_req, res) => {
1011
res.json({
1112
message: 'OK',

0 commit comments

Comments
 (0)