Skip to content

Fix/admin token security - #140

Closed
HoangHieu21201 wants to merge 3 commits into
devfrom
fix/admin-token-security
Closed

HoangHieu21201 wants to merge 3 commits into
devfrom
fix/admin-token-security

Conversation

@HoangHieu21201

@HoangHieu21201 HoangHieu21201 commented Jul 26, 2026

Copy link
Copy Markdown
Collaborator

admin token

Summary by CodeRabbit

  • Tính năng mới

    • Bổ sung refresh token cho đăng nhập, đăng ký, đăng nhập quản trị và đăng nhập Google.
    • Thêm khả năng tự động làm mới phiên khi access token hết hạn, giúp tiếp tục thao tác mà không cần đăng nhập lại.
    • Bổ sung API đăng xuất và làm mới token cho tài khoản người dùng và quản trị.
  • Cải tiến

    • Token mới được lưu và xóa đồng bộ khi đăng nhập, đăng xuất hoặc phiên hết hạn.
    • Cải thiện khởi tạo reCAPTCHA trong biểu mẫu liên hệ và quên mật khẩu, hạn chế lỗi khi dịch vụ chưa sẵn sàng.

@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@HoangHieu21201, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 34 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5ccf6388-47f7-47dd-beb4-b5d80197fcab

📥 Commits

Reviewing files that changed from the base of the PR and between 66583a7 and 06ad5a0.

📒 Files selected for processing (14)
  • backend/app/Http/Controllers/Api/Admin/AdminAccountController.php
  • backend/app/Http/Controllers/Api/Auth/AuthController.php
  • backend/app/Http/Controllers/Api/Auth/GoogleAuthController.php
  • backend/config/cors.php
  • backend/routes/api.php
  • frontend/package.json
  • frontend/src/composables/useUtilities.js
  • frontend/src/pages/user/auth/CallBackGoogle.vue
  • frontend/src/pages/user/auth/ForgotPassword.vue
  • frontend/src/pages/user/auth/Login.vue
  • frontend/src/pages/user/auth/Register.vue
  • frontend/src/pages/user/contact/Index.vue
  • frontend/src/utils/adminApiClient.js
  • frontend/src/utils/clientApiClient.js
📝 Walkthrough

Walkthrough

PR bổ sung refresh token cho xác thực user/admin, endpoint làm mới phiên, tự động retry request sau lỗi 401, lưu trữ token tương ứng trên frontend, đồng thời cập nhật khởi tạo reCAPTCHA và script phát triển.

Changes

Vòng đời token xác thực

Layer / File(s) Summary
API phát hành và làm mới token
backend/app/Http/Controllers/Api/Auth/..., backend/app/Http/Controllers/Api/Admin/..., backend/routes/api.php
Backend tạo access/refresh token cho user, admin và Google callback; thêm endpoint refresh/logout, kiểm tra quyền refresh và thu hồi token hiện tại.
Lưu trữ token tại frontend
frontend/src/composables/useUtilities.js, frontend/src/pages/.../auth/*
Frontend lưu refresh token sau đăng nhập, đăng ký và Google callback; bổ sung helper đọc và xóa refresh token cho user/admin.
Tự động refresh và retry request
frontend/src/utils/clientApiClient.js, frontend/src/utils/adminApiClient.js
API client xử lý lỗi 401 bằng một lần refresh, xếp hàng request đồng thời, cập nhật header/token và redirect khi refresh thất bại.

Khởi tạo frontend

Layer / File(s) Summary
Khởi tạo reCAPTCHA
frontend/src/pages/user/auth/ForgotPassword.vue, frontend/src/pages/user/contact/Index.vue
Các form chờ reCAPTCHA sẵn sàng trước khi render và làm sạch container trước mỗi lần render.
Bootstrap môi trường phát triển
frontend/package.json
Script dev chạy package:discover trước các tiến trình phát triển.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant APIClient
  participant AuthAPI
  participant Storage
  Client->>APIClient: Gửi request
  APIClient-->>Client: Lỗi 401
  APIClient->>Storage: Đọc refresh token
  APIClient->>AuthAPI: POST refresh-token
  AuthAPI-->>APIClient: Token mới
  APIClient->>Storage: Lưu token mới
  APIClient-->>Client: Retry request
Loading

Possibly related PRs

Suggested labels: enhancement

Suggested reviewers: binhminh021106

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed Tiêu đề phù hợp với thay đổi chính về xử lý và bảo mật token admin, dù chưa nêu hết chi tiết refresh token ở frontend/backend.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/admin-token-security

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 7

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/app/Http/Controllers/Api/Admin/AdminAccountController.php`:
- Around line 89-94: Update AdminAccountController::logout at
backend/app/Http/Controllers/Api/Admin/AdminAccountController.php lines 89-94 to
delete both admin_token and admin_refresh_token for the authenticated admin
instead of only currentAccessToken(). Apply the same change in
AuthController::logout at
backend/app/Http/Controllers/Api/Auth/AuthController.php lines 79-84, deleting
both auth_token and refresh_token for the authenticated user.

In `@backend/app/Http/Controllers/Api/Auth/GoogleAuthController.php`:
- Around line 38-43: Thay đổi luồng trong GoogleAuthController, đặc biệt đoạn
tạo token và redirect, để không đưa accessToken hoặc refreshToken vào query
string. Tạo exchange code ngắn hạn, dùng một lần với khả năng `exchange`, chuyển
hướng frontend chỉ với mã này, đồng thời bổ sung endpoint POST
`/auth/google/exchange` để xác thực quyền exchange, xoá mã sau khi dùng và phát
hành cặp token thật.

In `@backend/routes/api.php`:
- Around line 110-113: Update the auth route definitions around the
/refresh-token and logout routes: apply the ability:refresh middleware
specifically to AuthController::refresh, and wrap business routes protected by
auth:sanctum with ability:access (or abilities:access,...). Ensure refresh
tokens cannot access business endpoints, while access tokens retain normal
access.

In `@frontend/package.json`:
- Line 10: Update the frontend package.json dev script to run the Laravel
package:discover bootstrap step before invoking concurrently. Preserve all
existing Vite, queue, scheduler, crawler, API, and Reverb processes and their
arguments after the bootstrap completes.

In `@frontend/src/composables/useUtilities.js`:
- Around line 4-13: Move refresh-token handling from browser storage to
backend-issued httpOnly, Secure, SameSite=Strict cookies. In
frontend/src/composables/useUtilities.js, remove refresh-token keys from the
storage-based authentication key collections; in
frontend/src/pages/admin/auth/Login.vue, frontend/src/pages/user/auth/Login.vue,
and frontend/src/pages/user/auth/Register.vue, remove refresh-token localStorage
writes and rely on the backend cookie; in
frontend/src/pages/user/auth/CallBackGoogle.vue, stop persisting the
refresh_token received in the Google callback query string.

In `@frontend/src/pages/user/auth/ForgotPassword.vue`:
- Around line 154-172: The reCAPTCHA initialization polling continues
indefinitely after script load failure or component unmount. In
frontend/src/pages/user/auth/ForgotPassword.vue lines 154-172, retain the retry
timer, add bounded/error handling for script loading including script.onerror,
and clear the timer in onUnmounted; apply the same cancellation and
script.onerror handling in frontend/src/pages/user/contact/Index.vue lines
158-176, using each component’s existing initialization flow.

In `@frontend/src/utils/adminApiClient.js`:
- Around line 63-96: The refresh flow leaves isRefreshing permanently true when
no refresh token exists, so queued 401 requests are never released. In
frontend/src/utils/adminApiClient.js lines 63-96, add the no-token branch
alongside the refreshToken check to call processQueue with the authentication
error and null token, then reset isRefreshing; apply the same change in
frontend/src/utils/clientApiClient.js lines 119-148 around
getUserRefreshToken().
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 51485924-aaf6-46d6-b1a7-39ddc9056236

📥 Commits

Reviewing files that changed from the base of the PR and between 31031b0 and 66583a7.

📒 Files selected for processing (14)
  • backend/app/Http/Controllers/Api/Admin/AdminAccountController.php
  • backend/app/Http/Controllers/Api/Auth/AuthController.php
  • backend/app/Http/Controllers/Api/Auth/GoogleAuthController.php
  • backend/routes/api.php
  • frontend/package.json
  • frontend/src/composables/useUtilities.js
  • frontend/src/pages/admin/auth/Login.vue
  • frontend/src/pages/user/auth/CallBackGoogle.vue
  • frontend/src/pages/user/auth/ForgotPassword.vue
  • frontend/src/pages/user/auth/Login.vue
  • frontend/src/pages/user/auth/Register.vue
  • frontend/src/pages/user/contact/Index.vue
  • frontend/src/utils/adminApiClient.js
  • frontend/src/utils/clientApiClient.js

Comment thread backend/app/Http/Controllers/Api/Admin/AdminAccountController.php
Comment thread backend/app/Http/Controllers/Api/Auth/GoogleAuthController.php Outdated
Comment thread backend/routes/api.php Outdated
Comment thread frontend/package.json
Comment thread frontend/src/composables/useUtilities.js Outdated
Comment thread frontend/src/pages/user/auth/ForgotPassword.vue
Comment thread frontend/src/utils/adminApiClient.js Outdated
@HoangHieu21201
HoangHieu21201 deleted the fix/admin-token-security branch July 27, 2026 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant