This authentication system supports signup for different user types with appropriate validation and security measures.
- Student: Must use GUC email (@student.guc.edu.eg) and provide GUC ID
- Staff: Must use GUC email (@guc.edu.eg) and provide GUC ID
- TA: Must use GUC email (@guc.edu.eg) and provide GUC ID
- Professor: Must use GUC email (@guc.edu.eg) and provide GUC ID
- Vendor: Can use any email and must provide company name
POST /api/auth/signup
{
"email": "user@student.guc.edu.eg",
"password": "password123",
"firstName": "John",
"lastName": "Doe",
"userType": "Student",
"gucId": "34-1234"
}Multipart form-data fields and files:
- Fields:
email,password,userType=Vendor,companyName- Note:
firstNameandlastNameare not required for vendors
- Files (required):
vendorLogo(image)vendorTaxCard(image or PDF)
Example using curl:
curl -X POST http://localhost:5000/api/auth/signup \
-H "Content-Type: multipart/form-data" \
-F email=vendor@company.com \
-F password=password123 \
-F userType=Vendor \
-F companyName="Tech Solutions Inc" \
-F vendorLogo=@/path/to/logo.png \
-F vendorTaxCard=@/path/to/taxcard.pdf{
"success": true,
"message": "User created. Verification email sent.",
"user": {
"id": "user_id",
"email": "user@student.guc.edu.eg",
"firstName": "John",
"lastName": "Doe",
"userType": "Student",
"gucId": "34-1234",
"companyName": null,
"isVerified": false,
"createdAt": "2024-01-01T00:00:00.000Z"
},
"token": null
}- 400: Validation errors
- 409: User already exists
- 500: Server error
POST /api/auth/login
{
"email": "user@student.guc.edu.eg",
"password": "password123"
}{
"success": true,
"message": "Login successful",
"user": {
"id": "user_id",
"email": "user@student.guc.edu.eg",
"firstName": "John",
"lastName": "Doe",
"userType": "Student",
"gucId": "34-1234",
"companyName": null,
"isVerified": false,
"createdAt": "2024-01-01T00:00:00.000Z"
},
"token": "jwt_token_here"
}GET /api/auth/verify-email?token=...
- Marks the Student account as verified if the token is valid and not expired
- Redirects to the frontend login page (
FRONTEND_URL+/login)
- 302 Redirect to login on success
- 400 Invalid/expired link
- Must be a valid email format
- GUC users (Student, Staff, TA, Professor) must use:
@student.guc.edu.egfor students@guc.edu.egfor staff, TAs, and professors
- Vendors can use any valid email
- Minimum 6 characters
- All users: email, password, firstName, lastName, userType
- GUC users: gucId (required)
- Vendors: companyName (required)
- Passwords are automatically hashed using bcrypt before saving
- Salt rounds: 10
- Tokens expire after 7 days
- Include user ID, email, and user type
- Use for authenticating protected routes
authenticateToken: Verifies JWT tokensrequireRole: Checks user permissions for specific roles
Add these to your .env file:
MONGO_URI=mongodb+srv://username:password@cluster.mongodb.net/bindly
JWT_SECRET=your-super-secret-jwt-key-here
PORT=5000
FRONTEND_URL=http://localhost:3000
API_BASE_URL=http://localhost:5000
# SMTP settings
SMTP_HOST=smtp.yourprovider.com
SMTP_PORT=587
SMTP_SECURE=false
SMTP_USER=your_smtp_user
SMTP_PASS=your_smtp_password
MAIL_FROM="Bindly <no-reply@bindly.app>"
- Uploaded files are served at
/uploads/...from the backend server.
const { authenticateToken, requireRole } = require('./middleware/authMiddleware');
// Protect a route
router.get('/profile', authenticateToken, (req, res) => {
res.json({ user: req.user });
});
// Require specific role
router.get('/admin', authenticateToken, requireRole(['Staff', 'Professor']), (req, res) => {
res.json({ message: 'Admin access granted' });
});// Signup request
const signupData = {
email: 'john.doe@student.guc.edu.eg',
password: 'password123',
firstName: 'John',
lastName: 'Doe',
userType: 'Student',
gucId: '34-1234'
};
fetch('/api/auth/signup', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(signupData)
})
.then(response => response.json())
.then(data => {
if (data.success) {
localStorage.setItem('token', data.token);
// Redirect to dashboard
}
});- Install dependencies:
npm install bcryptjs express-validator jsonwebtoken-
Add environment variables to
.envfile -
Start the server:
npm run devYou can test the API using tools like Postman or curl:
# Test signup
curl -X POST http://localhost:5000/api/auth/signup \
-H "Content-Type: application/json" \
-d '{
"email": "test@student.guc.edu.eg",
"password": "password123",
"firstName": "Test",
"lastName": "User",
"userType": "Student",
"gucId": "34-1234"
}'