Fixed auth issues
This commit is contained in:
parent
7ddf6cd35c
commit
042186323b
1
src/app/api-utlities/errors/authorization.error.ts
Normal file
1
src/app/api-utlities/errors/authorization.error.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export class AuthorizationError extends Error {}
|
||||||
@ -2,6 +2,7 @@ import { RequestCookie } from 'next/dist/compiled/@edge-runtime/cookies';
|
|||||||
import { NextRequest } from 'next/server';
|
import { NextRequest } from 'next/server';
|
||||||
|
|
||||||
import oriyoClient from '@/app/api-utlities/clients/oriyo.client';
|
import oriyoClient from '@/app/api-utlities/clients/oriyo.client';
|
||||||
|
import { AuthorizationError } from '@/app/api-utlities/errors/authorization.error';
|
||||||
|
|
||||||
import { authorizationMiddleware } from '../../middlewares/auth.middleware';
|
import { authorizationMiddleware } from '../../middlewares/auth.middleware';
|
||||||
import { validationErrorHandler } from '../../middlewares/error-handler.middleware';
|
import { validationErrorHandler } from '../../middlewares/error-handler.middleware';
|
||||||
@ -16,6 +17,10 @@ const routeHandler = async (req: NextRequest, requestCookie: RequestCookie) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (oriyoResponse.status === 401) {
|
||||||
|
throw new AuthorizationError();
|
||||||
|
}
|
||||||
|
|
||||||
return new Response(JSON.stringify(oriyoResponse.data), {
|
return new Response(JSON.stringify(oriyoResponse.data), {
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { NextRequest } from 'next/server';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import oriyoClient from '@/app/api-utlities/clients/oriyo.client';
|
import oriyoClient from '@/app/api-utlities/clients/oriyo.client';
|
||||||
|
import { AuthorizationError } from '@/app/api-utlities/errors/authorization.error';
|
||||||
import { getParams } from '@/app/api-utlities/utilities/get-params';
|
import { getParams } from '@/app/api-utlities/utilities/get-params';
|
||||||
|
|
||||||
import { authorizationMiddleware } from '../../middlewares/auth.middleware';
|
import { authorizationMiddleware } from '../../middlewares/auth.middleware';
|
||||||
@ -34,6 +35,25 @@ const routeHandler = async (req: NextRequest, requestCookie: RequestCookie) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (oriyoResponse.status === 404)
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
transactions: [],
|
||||||
|
card_id,
|
||||||
|
current_page: validatedRequest.page,
|
||||||
|
limit: validatedRequest.limit,
|
||||||
|
total_records: 0,
|
||||||
|
total_pages: 0,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (oriyoResponse.status === 401) {
|
||||||
|
throw new AuthorizationError();
|
||||||
|
}
|
||||||
|
|
||||||
if (oriyoResponse.data.error) throw oriyoResponse.data;
|
if (oriyoResponse.data.error) throw oriyoResponse.data;
|
||||||
|
|
||||||
return new Response(JSON.stringify(oriyoResponse.data), {
|
return new Response(JSON.stringify(oriyoResponse.data), {
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { NextRequest } from 'next/server';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import oriyoClient from '@/app/api-utlities/clients/oriyo.client';
|
import oriyoClient from '@/app/api-utlities/clients/oriyo.client';
|
||||||
|
import { AuthorizationError } from '@/app/api-utlities/errors/authorization.error';
|
||||||
import { getParams } from '@/app/api-utlities/utilities/get-params';
|
import { getParams } from '@/app/api-utlities/utilities/get-params';
|
||||||
|
|
||||||
import { authorizationMiddleware } from '../../middlewares/auth.middleware';
|
import { authorizationMiddleware } from '../../middlewares/auth.middleware';
|
||||||
@ -34,6 +35,24 @@ const routeHandler = async (req: NextRequest, requestCookie: RequestCookie) => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (oriyoResponse.status === 404)
|
||||||
|
return new Response(
|
||||||
|
JSON.stringify({
|
||||||
|
transactions: [],
|
||||||
|
current_page: validatedRequest.page,
|
||||||
|
limit: validatedRequest.limit,
|
||||||
|
total_records: 0,
|
||||||
|
total_pages: 0,
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (oriyoResponse.status === 401) {
|
||||||
|
throw new AuthorizationError();
|
||||||
|
}
|
||||||
|
|
||||||
if (oriyoResponse.data.error) throw oriyoResponse.data;
|
if (oriyoResponse.data.error) throw oriyoResponse.data;
|
||||||
|
|
||||||
return new Response(JSON.stringify(oriyoResponse.data), {
|
return new Response(JSON.stringify(oriyoResponse.data), {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { has } from 'lodash';
|
|
||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
import { AuthorizationError } from '@/app/api-utlities/errors/authorization.error';
|
||||||
|
|
||||||
export const authorizationMiddleware =
|
export const authorizationMiddleware =
|
||||||
(handler: Function, authorizationTokenKey: string) =>
|
(handler: Function, authorizationTokenKey: string) =>
|
||||||
async (req: NextRequest, ...args: any[]) => {
|
async (req: NextRequest, ...args: any[]) => {
|
||||||
@ -16,7 +17,7 @@ export const authorizationMiddleware =
|
|||||||
try {
|
try {
|
||||||
return await handler(req, requestedToken, ...args);
|
return await handler(req, requestedToken, ...args);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (has(error, 'code') && error.code === 401) {
|
if (error instanceof AuthorizationError) {
|
||||||
const response = NextResponse.json(
|
const response = NextResponse.json(
|
||||||
{ message: 'Authorization session was timed out' },
|
{ message: 'Authorization session was timed out' },
|
||||||
{ status: 401 },
|
{ status: 401 },
|
||||||
|
|||||||
@ -1,6 +1,5 @@
|
|||||||
export interface TransactionResponse {
|
export interface TransactionResponse {
|
||||||
transactions: Transaction[];
|
transactions: Transaction[];
|
||||||
card_id: string;
|
|
||||||
current_page: number;
|
current_page: number;
|
||||||
limit: number;
|
limit: number;
|
||||||
total_records: number;
|
total_records: number;
|
||||||
|
|||||||
@ -25,7 +25,7 @@ import { TransactionsTable } from '@/widgets/transactions-table';
|
|||||||
export function CorporateDashboard() {
|
export function CorporateDashboard() {
|
||||||
const { t } = useTextController();
|
const { t } = useTextController();
|
||||||
|
|
||||||
const { data, isLoading } = useFetchMyCorporateInfoQuery({});
|
const { data } = useFetchMyCorporateInfoQuery({});
|
||||||
|
|
||||||
const [request, setTransactionFetchRequest] = useState<TransactionRequest>({
|
const [request, setTransactionFetchRequest] = useState<TransactionRequest>({
|
||||||
limit: 10,
|
limit: 10,
|
||||||
@ -159,12 +159,18 @@ export function CorporateDashboard() {
|
|||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{transactionsResponse && (
|
<TransactionsTable
|
||||||
<TransactionsTable
|
data={
|
||||||
data={transactionsResponse}
|
transactionsResponse || {
|
||||||
onChange={setTransactionFetchRequest}
|
limit: 10,
|
||||||
/>
|
current_page: 1,
|
||||||
)}
|
total_pages: 0,
|
||||||
|
total_records: 0,
|
||||||
|
transactions: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onChange={setTransactionFetchRequest}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -148,12 +148,18 @@ export function CustomerDashboard() {
|
|||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{transactionsResponse && (
|
<TransactionsTable
|
||||||
<TransactionsTable
|
data={
|
||||||
data={transactionsResponse}
|
transactionsResponse || {
|
||||||
onChange={setTransactionFetchRequest}
|
limit: 10,
|
||||||
/>
|
current_page: 1,
|
||||||
)}
|
total_pages: 0,
|
||||||
|
total_records: 0,
|
||||||
|
transactions: [],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onChange={setTransactionFetchRequest}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</main>
|
</main>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import { format } from 'date-fns';
|
import { format } from 'date-fns';
|
||||||
import { ru } from 'date-fns/locale';
|
import { ru } from 'date-fns/locale';
|
||||||
import { CalendarIcon } from 'lucide-react';
|
import { CalendarIcon, X } from 'lucide-react';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -46,8 +46,10 @@ export const TransactionsTable = ({
|
|||||||
data,
|
data,
|
||||||
onChange,
|
onChange,
|
||||||
}: TransactionsTableProps) => {
|
}: TransactionsTableProps) => {
|
||||||
const [startDate, setStartDate] = useState<Date | undefined>(undefined);
|
const [startDate, setStartDate] = useState<Date | undefined>(
|
||||||
const [endDate, setEndDate] = useState<Date | undefined>(undefined);
|
new Date(new Date().setMonth(new Date().getMonth() - 1)),
|
||||||
|
);
|
||||||
|
const [endDate, setEndDate] = useState<Date | undefined>(new Date());
|
||||||
const [currentPage, setCurrentPage] = useState(1);
|
const [currentPage, setCurrentPage] = useState(1);
|
||||||
const [itemsPerPage, setItemsPerPage] = useState(10);
|
const [itemsPerPage, setItemsPerPage] = useState(10);
|
||||||
|
|
||||||
@ -128,6 +130,9 @@ export const TransactionsTable = ({
|
|||||||
initialFocus
|
initialFocus
|
||||||
/>
|
/>
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
|
<Button variant='ghost' onClick={() => setStartDate(undefined)}>
|
||||||
|
<X className='mr-2 h-4 w-4' />
|
||||||
|
</Button>
|
||||||
</Popover>
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -155,16 +160,12 @@ export const TransactionsTable = ({
|
|||||||
initialFocus
|
initialFocus
|
||||||
/>
|
/>
|
||||||
</PopoverContent>
|
</PopoverContent>
|
||||||
|
<Button variant='ghost' onClick={() => setEndDate(undefined)}>
|
||||||
|
<X className='mr-2 h-4 w-4' />
|
||||||
|
</Button>
|
||||||
</Popover>
|
</Popover>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Button
|
|
||||||
className='mt-auto bg-red-600 hover:bg-red-700'
|
|
||||||
onClick={filterTransactions}
|
|
||||||
>
|
|
||||||
{t('corporate.transactions.applyButton')}
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user