A RESTful API and GraphQL endpoint for a polling application where users can create polls, vote on them, and view results. Now includes email verification for enhanced security.
- User authentication with email verification
- Create polls with multiple options
- Vote on polls (one vote per user per poll)
- View poll results with statistics
- User profiles and statistics
- JWT-based authentication
- Email notifications (verification and welcome emails)
- Both REST API and GraphQL support
- Node.js with Express
- Prisma ORM with PostgreSQL
- JWT for authentication
- bcryptjs for password hashing
- Apollo Server for GraphQL
- Nodemailer for email functionality
- Install dependencies:
npm install- Set up environment variables in
.env:
DATABASE_URL=your_postgresql_connection_string
JWT_SECRET=your_jwt_secret_key
PORT=3001
# Email Configuration
EMAIL_USER=your_email@gmail.com
EMAIL_PASS=your_app_password- Run Prisma migrations:
npm run prisma:migrate- Generate Prisma client:
npm run prisma:generate- Start the server:
npm run dev # Development mode with nodemon
npm start # Production mode- Register: User provides name, email, and password
- Email Sent: Verification code sent to user's email
- Verify: User enters the 6-digit verification code
- Welcome Email: Welcome email sent upon successful verification
- Login: User can now login with verified email
Register a new user and send verification email.
Body:
{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}Response:
{
"success": true,
"message": "User registered successfully. Please check your email for verification code.",
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"isVerified": false,
"createdAt": "2024-01-01T00:00:00.000Z"
},
"emailSent": true
}Verify user's email with verification code.
Body:
{
"email": "john@example.com",
"verificationCode": "123456"
}Response:
{
"success": true,
"message": "Email verified successfully"
}Resend verification code to user's email.
Body:
{
"email": "john@example.com"
}Response:
{
"success": true,
"message": "Verification code resent successfully"
}Login user (email must be verified).
Body:
{
"email": "john@example.com",
"password": "password123"
}Response:
{
"success": true,
"message": "Login successful",
"user": {
"id": 1,
"name": "John Doe",
"email": "john@example.com",
"isVerified": true,
"createdAt": "2024-01-01T00:00:00.000Z"
},
"token": "jwt_token_here"
}Error Response (Unverified Email):
{
"success": false,
"error": "Please verify your email before logging in",
"needsVerification": true
}Create a new poll (requires authentication).
Headers:
Authorization: Bearer your_jwt_token
Body:
{
"question": "What's your favorite food?",
"options": ["Pizza", "Burger", "Sushi", "Pasta"]
}Get all polls.
Get a specific poll by ID.
Vote on a poll (requires authentication).
Headers:
Authorization: Bearer your_jwt_token
Body:
{
"optionId": 1
}Get poll results with percentages.
Get current user's polls (requires authentication).
Get current user's votes (requires authentication).
Delete a poll (only by creator, requires authentication).
Get current user's profile (requires authentication).
Get current user's statistics (requires authentication).
Get all users.
- URL:
http://localhost:3001/graphql - Playground: Available in development mode
mutation Register($input: RegisterInput!) {
register(input: $input) {
success
message
}
}mutation VerifyEmail($input: VerifyEmailInput!) {
verifyEmail(input: $input) {
success
message
}
}mutation ResendVerification($input: ResendVerificationInput!) {
resendVerification(input: $input) {
success
message
}
}mutation Login($input: LoginInput!) {
login(input: $input) {
success
message
token
user {
id
name
email
isVerified
}
}
}The app uses Gmail SMTP for sending emails. Configure your Gmail account:
- Enable 2-Factor Authentication
- Generate an App Password
- Use the App Password in the
EMAIL_PASSenvironment variable
The API returns appropriate HTTP status codes and error messages:
400- Bad Request (validation errors, unverified email)401- Unauthorized (invalid credentials)403- Forbidden (email not verified, insufficient permissions)404- Not Found500- Internal Server Error
- Password hashing with bcryptjs
- JWT token authentication
- Email verification required for login
- Input validation and sanitization
- CORS protection
- Use
npm run devfor development with auto-reload - Use
npm run prisma:studioto open Prisma Studio for database management - Use
npm run prisma:migratewhen you change the schema - Check
/graphqlendpoint for GraphQL playground in development mode
- User: User accounts with email verification
- Poll: Poll questions created by users
- Option: Multiple choice options for each poll
- Vote: User votes on poll options (one vote per user per poll)
New fields in User model:
isVerified: Boolean flag for email verification statusverificationCode: 6-digit code for email verification