Skip to content

Commit a266862

Browse files
committed
add basic event bus and handle USER_CREATED
1 parent 66e2379 commit a266862

File tree

5 files changed

+43
-10
lines changed

5 files changed

+43
-10
lines changed

examples_new/microservices/auth/package-lock.json

Lines changed: 17 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples_new/microservices/auth/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"license": "ISC",
2020
"dependencies": {
2121
"@chronosrx/common": "^1.0.2",
22+
"axios": "^1.6.2",
2223
"bcryptjs": "^2.4.3",
2324
"cookie-parser": "^1.4.6",
2425
"dotenv": "^16.3.1",

examples_new/microservices/auth/src/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import dotenv from 'dotenv';
44
dotenv.config();
55
import { NotFoundError, errorHandler } from '@chronosrx/common';
66
import authRouter from './routes/auth-router';
7+
import eventRouter from './routes/event-router';
78
import cookieParser from 'cookie-parser';
89

910
const app = express();
1011

1112
app.use(express.json());
12-
app.use(cookieParser())
13+
app.use(cookieParser());
1314

1415
// app.get('/', (req, res) => {
1516
// console.log('💥 Test Route');
1617
// res.status(200).send({ msg: '💥 Test route' });
1718
// });
1819

1920
app.use('/api/auth', authRouter);
21+
app.use('/events', eventRouter);
2022

2123
app.use('*', (req, res) => {
2224
throw new NotFoundError();

examples_new/microservices/auth/src/controllers/auth-controller.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { BadRequestError, CurrentUserRequest } from '@chronosrx/common';
21
import { Request, Response } from 'express';
2+
import axios from 'axios';
3+
import { BadRequestError, CurrentUserRequest, Events } from '@chronosrx/common';
34
import { User } from '../models/user';
45
import { attachCookie } from '../util/attachCookie';
56

@@ -27,6 +28,13 @@ export const signup = async (req: Request, res: Response) => {
2728
await newUser.save();
2829

2930
// TODO PUBLISH AN EVENT TO THE EVENT BUS - type USER_CREATED, with data of user - user.id & username
31+
// console.log('Publishing event USER_CREATED');
32+
await axios.post('http://localhost:3005/', {
33+
event: {
34+
type: Events.USER_CREATED,
35+
payload: newUser,
36+
},
37+
});
3038

3139
// create a JWT w/ userId store on it
3240
// note: createJwt method created on the userSchema
@@ -98,6 +106,5 @@ export const getCurrentUser = async (req: CurrentUserRequest, res: Response) =>
98106
// if it does exist - use req.currentUser to find user in database by id
99107
const user = await User.findById(req.currentUser);
100108
// send back 200 with object with property currentUser set to the user from the database
101-
res.status(200).send({currentUser: user});
102-
109+
res.status(200).send({ currentUser: user });
103110
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import express from 'express';
2+
3+
const router = express.Router();
4+
5+
router.post('/', (req, res) => {
6+
const { event } = req.body;
7+
// console.log('Event received:', event);
8+
9+
res.send({ message: 'Event received' });
10+
});
11+
12+
export default router;

0 commit comments

Comments
 (0)