Skip to content

profile api implimentation #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 47 additions & 39 deletions backend-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions backend-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
"@eslint/js": "^9.13.0",
"@types/bcryptjs": "^2.4.6",
"@types/cookie-parser": "^1.4.7",
"@types/express": "^5.0.0",
"@types/express": "^5.0.2",
"@types/http-errors": "^2.0.4",
"@types/jsonwebtoken": "^9.0.7",
"@types/node": "^22.8.1",
"@types/jsonwebtoken": "^9.0.9",
"@types/node": "^22.15.18",
"@types/nodemailer": "^6.4.16",
"@types/passport-github2": "^1.2.9",
"@types/passport-google-oauth20": "^2.0.16",
"@types/passport-jwt": "^4.0.1",
"eslint": "^9.13.0",
"globals": "^15.11.0",
"nodemon": "^3.1.7",
"nodemon": "^3.1.10",
"prisma": "^5.22.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
Expand All @@ -35,11 +35,11 @@
"bcryptjs": "^2.4.3",
"cookie-parser": "^1.4.7",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"dotenv": "^16.5.0",
"express": "^4.21.1",
"http-errors": "^2.0.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.7.3",
"mongoose": "^8.15.0",
"nodemailer": "^6.9.16",
"passport": "^0.7.0",
"passport-github2": "^0.1.12",
Expand Down
7 changes: 7 additions & 0 deletions backend-node/src/express.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'express';

declare module 'express' {
export interface Request {
user?: { id: string };
}
}
11 changes: 11 additions & 0 deletions backend-node/src/profile/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import mongoose from 'mongoose';

const UserSchema = new mongoose.Schema({
name: { type: String },
email: { type: String, required: true, unique: true },
avatarUrl: { type: String },
bio: { type: String },
password: { type: String, required: true },
});

export default mongoose.model('User', UserSchema);
20 changes: 20 additions & 0 deletions backend-node/src/profile/authMiddleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Request, Response, NextFunction } from 'express';
import jwt from 'jsonwebtoken';

const authMiddleware = (req: any, res: any, next: any) => {
const authHeader = req.headers.authorization;
if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(401).json({ message: 'No token provided' });
}

const token = authHeader.split(' ')[1];
try {
const decoded = jwt.verify(token, process.env.JWT_SECRET || 'your_jwt_secret') as { id: string };
req.user = { id: decoded.id };
next();
} catch (err) {
return res.status(401).json({ message: 'Invalid token' });
}
};

export default authMiddleware;
29 changes: 29 additions & 0 deletions backend-node/src/profile/profileController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Request, Response } from 'express';
import User from './User';

// GET /api/v1/profile
export const getProfile = async (req: any, res: any) => {
try {
const user = await User.findById(req.user?.id).select('-password');
if (!user) return res.status(404).json({ message: 'User not found' });
res.json(user);
} catch (err) {
res.status(500).json({ message: 'Server error' });
}
};

// PUT /api/v1/profile
export const updateProfile = async (req: any, res: any) => {
try {
const updates = {
name: req.body.name,
bio: req.body.bio,
avatarUrl: req.body.avatarUrl,
};
const user = await User.findByIdAndUpdate(req.user?.id, updates, { new: true }).select('-password');
if (!user) return res.status(404).json({ message: 'User not found' });
res.json(user);
} catch (err) {
res.status(500).json({ message: 'Server error' });
}
};
10 changes: 10 additions & 0 deletions backend-node/src/profile/profileRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Router } from 'express';
import { getProfile, updateProfile } from './profileController';
import authMiddleware from './authMiddleware';

const router = Router();

router.get('/', authMiddleware, getProfile);
router.put('/', authMiddleware, updateProfile);

export default router;