Files
nobat724_front/app/api/auth/token/route.js
T
hamedandClaude Opus 4.8 05cf5fc585 fix(auth): align token/refresh routes with clinicpro 3-step OTP flow
backend جدید clinicpro جریان سه‌مرحله‌ای دارد:
send-code → verify-code (uuid+code) → oauth/token (uuid).
- token route حالا اول /api/v1/user/verify-code را صدا می‌زند سپس
  /oauth/token با بدنه JSON {grant_type, uuid} (به‌جای form-urlencoded
  با client_id/secret/scope که فقط backend پروداکشن قدیم می‌پذیرفت)
- refresh route به مسیر درست /oauth/token/refresh با بدنه JSON
- تست E2E روی ddev محلی: access_token واقعی صادر شد

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-15 00:39:43 +03:30

37 lines
1.2 KiB
JavaScript

import { NextResponse } from "next/server";
import { axiosInstance } from "@/lib/req";
export async function POST(request) {
const { uuid, code } = await request.json();
const API_URL = process.env.NEXT_PUBLIC_API_URL;
if (!uuid || !code) {
return NextResponse.json({ error: "uuid and code are required" }, { status: 400 });
}
const jsonHeaders = { "Content-Type": "application/json", Authorization: "" };
try {
// مرحله ۱: تأیید OTP — بدون این، oauth/token کد را verified نمی‌بیند
await axiosInstance.post(
`${API_URL}/api/v1/user/verify-code`,
{ uuid, code },
{ headers: jsonHeaders }
);
// مرحله ۲: تبدیل uuid تأییدشده به توکن
const res = await axiosInstance.post(
`${API_URL}/oauth/token`,
{ grant_type: "mobile", uuid },
{ headers: jsonHeaders }
);
return NextResponse.json(res.data, { status: res.status });
} catch (error) {
if (error.response) {
return NextResponse.json(error.response.data, { status: error.response.status });
}
return NextResponse.json({ error: "Token request failed" }, { status: 500 });
}
}