Refresh session in contacts API proxy routes
This commit is contained in:
@ -1,15 +1,9 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { cookies } from 'next/headers';
|
import { SERVER_API_URL as API_URL } from '../../../../lib/server-api';
|
||||||
import { authCookieName } from '../../../../lib/auth';
|
import { getProxyAccessToken } from '../../../../lib/proxy-auth';
|
||||||
|
|
||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api';
|
|
||||||
|
|
||||||
async function getToken() {
|
|
||||||
return (await cookies()).get(authCookieName)?.value;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET(_: Request, { params }: { params: Promise<{ id: string }> }) {
|
export async function GET(_: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||||
const token = await getToken();
|
const token = await getProxyAccessToken();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
@ -25,7 +19,7 @@ export async function GET(_: Request, { params }: { params: Promise<{ id: string
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
export async function PATCH(request: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||||
const token = await getToken();
|
const token = await getProxyAccessToken();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
@ -47,7 +41,7 @@ export async function PATCH(request: Request, { params }: { params: Promise<{ id
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(_: Request, { params }: { params: Promise<{ id: string }> }) {
|
export async function DELETE(_: Request, { params }: { params: Promise<{ id: string }> }) {
|
||||||
const token = await getToken();
|
const token = await getProxyAccessToken();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,11 +1,9 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { cookies } from 'next/headers';
|
import { SERVER_API_URL as API_URL } from '../../../../lib/server-api';
|
||||||
import { authCookieName } from '../../../../lib/auth';
|
import { getProxyAccessToken } from '../../../../lib/proxy-auth';
|
||||||
|
|
||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api';
|
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const token = (await cookies()).get(authCookieName)?.value;
|
const token = await getProxyAccessToken();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { cookies } from 'next/headers';
|
import { SERVER_API_URL as API_URL } from '../../../lib/server-api';
|
||||||
import { authCookieName } from '../../../lib/auth';
|
import { getProxyAccessToken } from '../../../lib/proxy-auth';
|
||||||
|
|
||||||
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3001/api';
|
|
||||||
|
|
||||||
function buildUrl(searchParams: URLSearchParams) {
|
function buildUrl(searchParams: URLSearchParams) {
|
||||||
const query = searchParams.toString();
|
const query = searchParams.toString();
|
||||||
@ -10,7 +8,7 @@ function buildUrl(searchParams: URLSearchParams) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
const token = (await cookies()).get(authCookieName)?.value;
|
const token = await getProxyAccessToken();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
@ -28,7 +26,7 @@ export async function GET(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: Request) {
|
export async function POST(request: Request) {
|
||||||
const token = (await cookies()).get(authCookieName)?.value;
|
const token = await getProxyAccessToken();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ message: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|||||||
58
frontend/src/lib/proxy-auth.ts
Normal file
58
frontend/src/lib/proxy-auth.ts
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import { cookies } from 'next/headers';
|
||||||
|
import { authCookieName, refreshCookieName } from './auth';
|
||||||
|
import { SERVER_API_URL } from './server-api';
|
||||||
|
|
||||||
|
const secureCookies = process.env.NODE_ENV === 'production';
|
||||||
|
|
||||||
|
type RefreshPayload = {
|
||||||
|
access_token: string;
|
||||||
|
refresh_token: string;
|
||||||
|
access_token_max_age_seconds?: number;
|
||||||
|
refresh_token_max_age_seconds?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function getProxyAccessToken() {
|
||||||
|
const cookieStore = await cookies();
|
||||||
|
const accessToken = cookieStore.get(authCookieName)?.value;
|
||||||
|
if (accessToken) {
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
const refreshToken = cookieStore.get(refreshCookieName)?.value;
|
||||||
|
if (!refreshToken) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${SERVER_API_URL}/auth/refresh`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ refreshToken }),
|
||||||
|
cache: 'no-store',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const payload = (await response.json()) as RefreshPayload;
|
||||||
|
if (!payload.access_token || !payload.refresh_token) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
cookieStore.set(authCookieName, payload.access_token, {
|
||||||
|
httpOnly: true,
|
||||||
|
sameSite: 'lax',
|
||||||
|
secure: secureCookies,
|
||||||
|
path: '/',
|
||||||
|
maxAge: payload.access_token_max_age_seconds || 60 * 60 * 24,
|
||||||
|
});
|
||||||
|
cookieStore.set(refreshCookieName, payload.refresh_token, {
|
||||||
|
httpOnly: true,
|
||||||
|
sameSite: 'lax',
|
||||||
|
secure: secureCookies,
|
||||||
|
path: '/',
|
||||||
|
maxAge: payload.refresh_token_max_age_seconds || 60 * 60 * 24 * 30,
|
||||||
|
});
|
||||||
|
|
||||||
|
return payload.access_token;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user