diff --git a/app/api/auth/refresh/route.js b/app/api/auth/refresh/route.js index 0246478..d3e5923 100644 --- a/app/api/auth/refresh/route.js +++ b/app/api/auth/refresh/route.js @@ -5,25 +5,17 @@ export async function POST(request) { const { refresh_token } = await request.json(); const API_URL = process.env.NEXT_PUBLIC_API_URL; - const CLIENT_ID = process.env.CLIENT_ID || process.env.NEXT_PUBLIC_CLIENT_ID; - const CLIENT_SECRET = process.env.CLIENT_SECRET || process.env.NEXT_PUBLIC_CLIENT_SECRET; if (!refresh_token) { return NextResponse.json({ error: "refresh_token is required" }, { status: 400 }); } - const formData = new URLSearchParams(); - formData.append("grant_type", "refresh_token"); - formData.append("client_id", CLIENT_ID); - formData.append("client_secret", CLIENT_SECRET); - formData.append("refresh_token", refresh_token); - try { const res = await axiosInstance.post( - `${API_URL}/oauth/token`, - formData.toString(), + `${API_URL}/oauth/token/refresh`, + { refresh_token }, { - headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: "" }, + headers: { "Content-Type": "application/json", Authorization: "" }, } ); return NextResponse.json(res.data, { status: res.status }); diff --git a/app/api/auth/token/route.js b/app/api/auth/token/route.js index 4920da6..0e013a2 100644 --- a/app/api/auth/token/route.js +++ b/app/api/auth/token/route.js @@ -2,31 +2,29 @@ import { NextResponse } from "next/server"; import { axiosInstance } from "@/lib/req"; export async function POST(request) { - const { uuid, code, scope = "nobat724" } = await request.json(); + const { uuid, code } = await request.json(); const API_URL = process.env.NEXT_PUBLIC_API_URL; - const CLIENT_ID = process.env.CLIENT_ID || process.env.NEXT_PUBLIC_CLIENT_ID; - const CLIENT_SECRET = process.env.CLIENT_SECRET || process.env.NEXT_PUBLIC_CLIENT_SECRET; if (!uuid || !code) { return NextResponse.json({ error: "uuid and code are required" }, { status: 400 }); } - const formData = new URLSearchParams(); - formData.append("grant_type", "mobile"); - formData.append("client_id", CLIENT_ID); - formData.append("client_secret", CLIENT_SECRET); - formData.append("uuid", uuid); - formData.append("code", code); - formData.append("scope", scope); + 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`, - formData.toString(), - { - headers: { "Content-Type": "application/x-www-form-urlencoded", Authorization: "" }, - } + { grant_type: "mobile", uuid }, + { headers: jsonHeaders } ); return NextResponse.json(res.data, { status: res.status }); } catch (error) {