Skip to content

Commit 3f63e97

Browse files
committed
feat(user): implement comprehensive profile management endpoints
- Added endpoints for updating user profile (PUT /profile), updating email (PUT /profile/email), updating password (PUT /profile/password), and deleting account (DELETE /profile). - Implemented corresponding DTOs for profile updates, email changes, password updates, and account deletion. - Enhanced user service and repository methods to handle profile updates, email verification, password changes, and account deletion with necessary security checks. - Integrated activity logging for profile updates, email changes, and account deletions. - Updated API documentation to reflect new endpoints and their usage, including detailed request/response examples. - Regenerated Swagger documentation to include the new profile management features.
1 parent 120c7c9 commit 3f63e97

File tree

12 files changed

+1808
-17
lines changed

12 files changed

+1808
-17
lines changed

cmd/api/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,14 @@ func main() {
118118
protected := r.Group("/")
119119
protected.Use(middleware.AuthMiddleware())
120120
{
121+
// User profile routes
121122
protected.GET("/profile", userHandler.GetProfile)
123+
protected.PUT("/profile", userHandler.UpdateProfile)
124+
protected.DELETE("/profile", userHandler.DeleteAccount)
125+
protected.PUT("/profile/email", userHandler.UpdateEmail)
126+
protected.PUT("/profile/password", userHandler.UpdatePassword)
127+
128+
// Auth routes
122129
protected.GET("/auth/validate", userHandler.ValidateToken)
123130
protected.POST("/logout", userHandler.Logout)
124131

docs/API.md

Lines changed: 170 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,51 +5,204 @@
55
### Register
66
- `POST /register`
77
- Request: `{ "email": "[email protected]", "password": "..." }`
8-
- Response: `{ "success": true, "data": { "token": "..." } }`
8+
- Response: `{ "message": "User registered successfully. Please check your email for verification." }`
99

1010
### Login
1111
- `POST /login`
1212
- Request: `{ "email": "[email protected]", "password": "..." }`
13-
- Response: `{ "success": true, "data": { "token": "..." } }`
13+
- Response: `{ "access_token": "...", "refresh_token": "..." }`
14+
- If 2FA enabled: `{ "message": "2FA verification required", "temp_token": "..." }`
1415

1516
### Logout
1617
- `POST /logout`
1718
- Header: `Authorization: Bearer <access_token>`
18-
- Request: `{ "refresh_token": "..." }`
19+
- Request: `{ "refresh_token": "...", "access_token": "..." }`
1920
- Response: `{ "message": "Successfully logged out" }`
2021

2122
### Refresh Token
2223
- `POST /refresh-token`
2324
- Request: `{ "refresh_token": "..." }`
24-
- Response: `{ "success": true, "data": { "token": "..." } }`
25+
- Response: `{ "access_token": "...", "refresh_token": "..." }`
2526

2627
### Forgot Password
2728
- `POST /forgot-password`
2829
- Request: `{ "email": "[email protected]" }`
29-
- Response: `{ "success": true }`
30+
- Response: `{ "message": "If an account with that email exists, a password reset link has been sent." }`
3031

3132
### Reset Password
3233
- `POST /reset-password`
3334
- Request: `{ "token": "...", "new_password": "..." }`
34-
- Response: `{ "success": true }`
35+
- Response: `{ "message": "Password has been reset successfully." }`
3536

3637
### Email Verification
3738
- `GET /verify-email?token=...`
38-
- Response: `{ "success": true }`
39-
40-
### Social Login
41-
- `GET /auth/{provider}/login`
42-
- `GET /auth/{provider}/callback`
43-
44-
### Protected Profile
45-
- `GET /profile`
46-
- Header: `Authorization: Bearer <token>`
47-
- Response: `{ "success": true, "data": { ...user... } }`
39+
- Response: `{ "message": "Email verified successfully!" }`
4840

4941
### Token Validation (for external services)
5042
- `GET /auth/validate`
5143
- Header: `Authorization: Bearer <token>`
5244
- Response: `{ "valid": true, "userID": "uuid", "email": "[email protected]" }`
5345

46+
## Social Authentication Endpoints
47+
48+
### Google OAuth2
49+
- `GET /auth/google/login` - Initiate Google login
50+
- `GET /auth/google/callback` - Google callback handler
51+
52+
### Facebook OAuth2
53+
- `GET /auth/facebook/login` - Initiate Facebook login
54+
- `GET /auth/facebook/callback` - Facebook callback handler
55+
56+
### GitHub OAuth2
57+
- `GET /auth/github/login` - Initiate GitHub login
58+
- `GET /auth/github/callback` - GitHub callback handler
59+
60+
## User Profile Endpoints (Protected)
61+
62+
All profile endpoints require JWT authentication via `Authorization: Bearer <token>` header.
63+
64+
### Get Profile
65+
- `GET /profile`
66+
- Response: User profile with social accounts
67+
```json
68+
{
69+
"id": "uuid",
70+
"email": "[email protected]",
71+
"email_verified": true,
72+
"name": "John Doe",
73+
"first_name": "John",
74+
"last_name": "Doe",
75+
"profile_picture": "https://...",
76+
"locale": "en-US",
77+
"two_fa_enabled": false,
78+
"created_at": "2023-01-01T00:00:00Z",
79+
"updated_at": "2023-01-01T00:00:00Z",
80+
"social_accounts": [...]
81+
}
82+
```
83+
84+
### Update Profile
85+
- `PUT /profile`
86+
- Request (all fields optional):
87+
```json
88+
{
89+
"name": "John Doe",
90+
"first_name": "John",
91+
"last_name": "Doe",
92+
"profile_picture": "https://example.com/avatar.jpg",
93+
"locale": "en-US"
94+
}
95+
```
96+
- Response: Updated user profile (same format as GET /profile)
97+
98+
### Update Email
99+
- `PUT /profile/email`
100+
- Request:
101+
```json
102+
{
103+
"email": "[email protected]",
104+
"password": "currentpassword"
105+
}
106+
```
107+
- Response: `{ "message": "Email updated successfully. Please check your new email for verification." }`
108+
- Note: Email verification required for new email address
109+
110+
### Update Password
111+
- `PUT /profile/password`
112+
- Request:
113+
```json
114+
{
115+
"current_password": "oldpassword123",
116+
"new_password": "newpassword123"
117+
}
118+
```
119+
- Response: `{ "message": "Password updated successfully. All sessions have been logged out for security." }`
120+
- Note: All existing tokens will be revoked for security
121+
122+
### Delete Account
123+
- `DELETE /profile`
124+
- Request:
125+
```json
126+
{
127+
"password": "currentpassword",
128+
"confirm_deletion": true
129+
}
130+
```
131+
- Response: `{ "message": "Account deleted successfully. We're sorry to see you go." }`
132+
- Note: This action is permanent and cannot be undone
133+
134+
## Two-Factor Authentication Endpoints (Protected)
135+
136+
### Generate 2FA Setup
137+
- `POST /2fa/generate`
138+
- Response: QR code and secret for TOTP setup
139+
140+
### Verify 2FA Setup
141+
- `POST /2fa/verify-setup`
142+
- Request: `{ "code": "123456" }`
143+
- Response: Verification status
144+
145+
### Enable 2FA
146+
- `POST /2fa/enable`
147+
- Request: `{ "code": "123456" }`
148+
- Response: Recovery codes
149+
150+
### Disable 2FA
151+
- `POST /2fa/disable`
152+
- Request: `{ "code": "123456" }`
153+
- Response: `{ "message": "2FA disabled successfully" }`
154+
155+
### Generate New Recovery Codes
156+
- `POST /2fa/recovery-codes`
157+
- Request: `{ "code": "123456" }`
158+
- Response: New recovery codes
159+
160+
## Activity Log Endpoints (Protected)
161+
162+
### Get User Activity Logs
163+
- `GET /activity-logs`
164+
- Query parameters: `page`, `limit`, `event_type`, `start_date`, `end_date`
165+
- Response: Paginated list of user's activity logs
166+
167+
### Get Activity Log by ID
168+
- `GET /activity-logs/:id`
169+
- Response: Single activity log entry
170+
171+
### Get Available Event Types
172+
- `GET /activity-logs/event-types`
173+
- Response: List of available event types for filtering
174+
175+
## Admin Endpoints (Protected)
176+
177+
### Get All Activity Logs (Admin only)
178+
- `GET /admin/activity-logs`
179+
- Query parameters: `page`, `limit`, `user_id`, `event_type`, `start_date`, `end_date`
180+
- Response: Paginated list of all users' activity logs
181+
182+
---
183+
184+
## Event Types
185+
186+
The following event types are logged in the activity log system:
187+
188+
- `LOGIN` - User login
189+
- `LOGOUT` - User logout
190+
- `REGISTER` - User registration
191+
- `PASSWORD_CHANGE` - Password changed via profile
192+
- `PASSWORD_RESET` - Password reset via forgot password flow
193+
- `EMAIL_VERIFY` - Email verification completed
194+
- `EMAIL_CHANGE` - Email address changed
195+
- `2FA_ENABLE` - Two-factor authentication enabled
196+
- `2FA_DISABLE` - Two-factor authentication disabled
197+
- `2FA_LOGIN` - Login with 2FA verification
198+
- `TOKEN_REFRESH` - Access token refreshed
199+
- `SOCIAL_LOGIN` - Social authentication login
200+
- `PROFILE_ACCESS` - Profile accessed (GET /profile)
201+
- `PROFILE_UPDATE` - Profile updated
202+
- `ACCOUNT_DELETION` - Account deleted
203+
- `RECOVERY_CODE_USED` - 2FA recovery code used
204+
- `RECOVERY_CODE_GENERATE` - New recovery codes generated
205+
54206
---
55-
For more details, see the OpenAPI spec (if available) or code comments.
207+
208+
For detailed API specifications including request/response schemas and authentication requirements, see the [Swagger documentation](http://localhost:8080/swagger/index.html) when the API is running.

0 commit comments

Comments
 (0)