forked from AvantikaSharma2307/CodeSphereLegder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
142 lines (122 loc) · 4.15 KB
/
server.js
File metadata and controls
142 lines (122 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 📁 backend/index.js
const express = require('express');
const passport = require('passport');
const GitHubStrategy = require('passport-github2').Strategy;
const jwt = require('jsonwebtoken');
const cookieParser = require('cookie-parser');
const dotenv = require('dotenv');
const cors = require('cors');
const { Sequelize, DataTypes } = require('sequelize');
dotenv.config();
const app = express();
app.use(express.json());
app.use(cors({
origin: 'http://localhost:5173',
credentials: true,
}));
app.use(cookieParser());
app.use(passport.initialize());
const sequelize = new Sequelize(process.env.MYSQL_DB, process.env.MYSQL_USER, process.env.MYSQL_PASSWORD, {
host: process.env.MYSQL_HOST,
dialect: 'mysql',
});
// ✅ Updated User model - removed experience, projects, skills
const User = sequelize.define('User', {
githubId: { type: DataTypes.STRING, unique: true },
username: DataTypes.STRING,
name: DataTypes.STRING,
avatarUrl: DataTypes.STRING,
});
const Job = sequelize.define('Job', {
title: DataTypes.STRING,
description: DataTypes.TEXT,
});
const Notification = sequelize.define('Notification', {
message: DataTypes.STRING,
type: DataTypes.STRING,
});
User.hasMany(Job, { as: 'jobs', foreignKey: 'maintainerId' });
Job.belongsTo(User, { as: 'maintainer', foreignKey: 'maintainerId' });
User.hasMany(Notification, { as: 'notifications', foreignKey: 'toUserId' });
Notification.belongsTo(User, { as: 'toUser', foreignKey: 'toUserId' });
Notification.belongsTo(User, { as: 'fromUser', foreignKey: 'fromUserId' });
Notification.belongsTo(Job, { as: 'relatedJob', foreignKey: 'relatedJobId' });
sequelize.sync();
passport.use(new GitHubStrategy({
clientID: process.env.GITHUB_CLIENT_ID,
clientSecret: process.env.GITHUB_CLIENT_SECRET,
callbackURL: 'http://localhost:5000/auth/github/callback',
}, async (accessToken, refreshToken, profile, done) => {
const { id, username, displayName, photos } = profile;
let user = await User.findOne({ where: { githubId: id } });
if (!user) {
user = await User.create({
githubId: id,
username,
name: displayName,
avatarUrl: photos[0]?.value,
});
}
done(null, user);
}));
app.get('/auth/github', passport.authenticate('github', { scope: ['user:email'] }));
app.get('/auth/github/callback',
passport.authenticate('github', { session: false }),
(req, res) => {
const token = jwt.sign({ userId: req.user.id }, process.env.JWT_SECRET, { expiresIn: '1d' });
res.cookie('token', token, {
httpOnly: true,
sameSite: 'Lax',
secure: false,
});
res.redirect('http://localhost:5173/dashboard/profile');
}
);
const authenticate = (req, res, next) => {
const token = req.cookies.token;
if (!token) return res.status(401).json({ error: 'Unauthorized' });
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.userId = decoded.userId;
next();
} catch {
res.status(401).json({ error: 'Invalid token' });
}
};
app.get('/api/me', authenticate, async (req, res) => {
const user = await User.findByPk(req.userId);
res.json(user);
});
// 🔥 Removed: PUT /api/me endpoint since profile fields are removed
app.post('/api/jobs', authenticate, async (req, res) => {
const job = await Job.create({ ...req.body, maintainerId: req.userId });
res.json(job);
});
app.get('/api/jobs', async (req, res) => {
const jobs = await Job.findAll({ include: [{ model: User, as: 'maintainer' }] });
res.json(jobs);
});
app.post('/api/jobs/:id/apply', authenticate, async (req, res) => {
const job = await Job.findByPk(req.params.id);
await Notification.create({
toUserId: job.maintainerId,
fromUserId: req.userId,
type: 'application',
message: 'Someone applied to your job',
relatedJobId: job.id,
});
res.json({ success: true });
});
app.get('/api/notifications', authenticate, async (req, res) => {
const notifications = await Notification.findAll({
where: { toUserId: req.userId },
include: [
{ model: User, as: 'fromUser' },
{ model: Job, as: 'relatedJob' },
]
});
res.json(notifications);
});
app.listen(5000, () => {
console.log('✅ Backend running on http://localhost:5000 with MySQL');
});