Skip to content

Commit 3f83940

Browse files
authored
Merge pull request #5 from foyzulkarim/feature/user-management
Implement User Authentication, Search, and Follow
2 parents 966f6cb + 431ad73 commit 3f83940

File tree

16 files changed

+828
-60
lines changed

16 files changed

+828
-60
lines changed

package-lock.json

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

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"license": "ISC",
2424
"dependencies": {
2525
"compression": "^1.7.4",
26+
"connect-mongo": "^5.1.0",
2627
"cookie-parser": "^1.4.6",
2728
"cors": "^2.8.5",
2829
"cron": "^3.1.6",
@@ -39,15 +40,21 @@
3940
"passport": "^0.7.0",
4041
"passport-github2": "^0.1.12",
4142
"pm2": "^5.3.1",
43+
"short-uuid": "^5.2.0",
4244
"ulid": "^2.3.0",
45+
"validator": "^13.11.0",
4346
"winston": "^3.13.0",
4447
"winston-daily-rotate-file": "^5.0.0",
4548
"winston-loggly-bulk": "^3.3.0"
4649
},
4750
"devDependencies": {
51+
"@faker-js/faker": "^8.4.1",
4852
"jest": "^29.7.0",
4953
"mongodb-memory-server": "^9.1.8",
5054
"supertest": "^6.3.4",
5155
"zx": "^8.0.1"
56+
},
57+
"volta": {
58+
"node": "20.12.2"
5259
}
5360
}

scripts/dev/data-generators/user.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const mongoose = require('mongoose');
2+
const { faker } = require('@faker-js/faker');
3+
const short = require('short-uuid');
4+
const User = require('../../../src/domains/user/schema');
5+
6+
7+
async function populateUsers() {
8+
console.log('Populating users...');
9+
for(let i = 0; i < 100; i++) {
10+
const user = new User({
11+
githubId: short.generate(),
12+
nodeId: short.generate(),
13+
displayName: faker.person.fullName(),
14+
username: faker.internet.userName(),
15+
profileUrl: faker.internet.url(),
16+
avatarUrl: faker.image.avatar(),
17+
apiUrl: faker.internet.url(),
18+
company: faker.company.name(),
19+
blog: faker.internet.url(),
20+
location: faker.location.city(),
21+
email: faker.internet.email(),
22+
23+
bio: faker.lorem.sentence(),
24+
public_repos: faker.number.int({min: 0, max: 100}),
25+
public_gists: faker.number.int({min: 0, max: 100}),
26+
followers: faker.number.int({min: 0, max: 100}),
27+
following: faker.number.int(),
28+
created_at: faker.date.past(),
29+
updated_at: faker.date.recent(),
30+
isDemo: true,
31+
isVerified: false
32+
});
33+
34+
await user.save();
35+
console.log(`User ${i} created`);
36+
}
37+
38+
console.log('Data populated');
39+
}
40+
41+
mongoose.connect('mongodb://localhost:27017/commitstreams').then(() => {
42+
console.log('Connected to MongoDB');
43+
populateUsers().then(() => {
44+
mongoose.connection.close();
45+
});
46+
});

src/app.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ function defineRoutes(expressApp) {
1818

1919
domainRoutes(router);
2020

21-
expressApp.use('/api/v1', router);
21+
// Authentication Middleware
22+
function isAuthenticated(req, res, next) {
23+
if (req.isAuthenticated()) {
24+
// Passport's built-in method
25+
return next(); // User is authenticated, proceed
26+
} else {
27+
logger.warn('User is not authenticated');
28+
return res.status(401).json({ message: 'Unauthorized' });
29+
}
30+
}
31+
32+
expressApp.use('/api/v1', isAuthenticated, router);
2233
// health check
2334
expressApp.get('/health', (req, res) => {
2435
const healthCheck = {

0 commit comments

Comments
 (0)