-
Notifications
You must be signed in to change notification settings - Fork 13
[TASK-7429] fix authentication #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
151e57b
926d10a
6798dc5
1814717
fd1b44f
e3f88ba
dff936b
8716d7b
a1709ea
6f084f4
860a582
16eb9e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,13 +1,34 @@ | ||||||||||||||
| import type { NextRequest } from 'next/server' | ||||||||||||||
| import { NextResponse } from 'next/server' | ||||||||||||||
| import { NextResponse, NextRequest } from 'next/server' | ||||||||||||||
| import * as interfaces from '@/interfaces' | ||||||||||||||
| import { GET as getUserFromCookie } from '@/app/api/peanut/user/get-user-from-cookie/route' | ||||||||||||||
|
|
||||||||||||||
| export async function POST(request: NextRequest) { | ||||||||||||||
| try { | ||||||||||||||
| const { userId, type } = await request.json() | ||||||||||||||
| if (!process.env.BRIDGE_API_KEY) { | ||||||||||||||
| throw new Error('BRIDGE_API_KEY is not defined') | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| const getUserFromCookieRequest = new NextRequest( | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this plus |
||||||||||||||
| `${process.env.NEXT_PUBLIC_BASE_URL}/api/peanut/user/get-user-from-cookie`, | ||||||||||||||
| { | ||||||||||||||
| method: 'GET', | ||||||||||||||
| headers: { | ||||||||||||||
| cookie: request.headers.get('cookie') ?? '', | ||||||||||||||
| ...request.headers, | ||||||||||||||
|
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid potential header conflicts when spreading headers Spreading Apply this diff to adjust the headers: headers: {
cookie: request.headers.get('cookie') ?? '',
- ...request.headers,
},📝 Committable suggestion
Suggested change
|
||||||||||||||
| }, | ||||||||||||||
| } | ||||||||||||||
| ) | ||||||||||||||
| const getUserFromCookieResponse = await getUserFromCookie(getUserFromCookieRequest) | ||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's going on here, why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need the user data, of the calling user, so we need to ensure that the user is authenticated and get its data. We can't trust on input |
||||||||||||||
| if (!getUserFromCookieResponse.ok) { | ||||||||||||||
| return new NextResponse('Unauthorized', { status: 401 }) | ||||||||||||||
| } | ||||||||||||||
| const { user } = await getUserFromCookieResponse.json() | ||||||||||||||
|
|
||||||||||||||
| if (userId !== user?.bridge_customer_id) { | ||||||||||||||
| return new NextResponse('Forbidden', { status: 403 }) | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+28
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure type consistency when comparing To prevent potential mismatches due to type differences, ensure that both Apply this diff to enforce type consistency: - if (userId !== user?.bridge_customer_id) {
+ if (String(userId) !== String(user?.bridge_customer_id)) {📝 Committable suggestion
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| const response = await fetch(`https://api.bridge.xyz/v0/kyc_links/${userId}`, { | ||||||||||||||
| method: 'GET', | ||||||||||||||
| headers: { | ||||||||||||||
|
|
@@ -37,7 +58,7 @@ export async function POST(request: NextRequest) { | |||||||||||||
| }, | ||||||||||||||
| }) | ||||||||||||||
| } else if (type === 'customer_id') { | ||||||||||||||
| return new NextResponse(JSON.stringify({ customer_id: data.customer_id }), { | ||||||||||||||
| return new NextResponse(JSON.stringify({ id: data.customer_id, kyc_status: data.kyc_status }), { | ||||||||||||||
| status: 200, | ||||||||||||||
| headers: { | ||||||||||||||
| 'Content-Type': 'application/json', | ||||||||||||||
|
|
||||||||||||||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { NextRequest, NextResponse } from 'next/server' | |
| import * as consts from '@/constants' | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| const { userId, username, bridge_customer_id } = await request.json() | ||
| const { userId, username, bridge_customer_id, kycStatus } = await request.json() | ||
| const apiKey = process.env.PEANUT_API_KEY | ||
| const cookieStore = cookies() | ||
| const token = cookieStore.get('jwt-token') | ||
|
|
@@ -24,6 +24,7 @@ export async function POST(request: NextRequest) { | |
| userId, | ||
| username, | ||
| bridge_customer_id, | ||
| kycStatus, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Handle undefined If Apply this diff to include body: JSON.stringify({
userId,
username,
bridge_customer_id,
- kycStatus,
+ ...(kycStatus && { kycStatus }),
}),
|
||
| }), | ||
| }) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -47,9 +47,7 @@ export const GlobalKYCComponent = ({ intialStep, offrampForm, setOfframpForm, on | |
|
|
||
| const [loadingState, setLoadingState] = useState<string>('Idle') | ||
| const isLoading = useMemo(() => loadingState !== 'Idle', [loadingState]) | ||
| const { fetchUser, updateBridgeCustomerId } = useAuth() | ||
|
|
||
| const [kycStatus, setKycStatus] = useState<'completed' | 'under_review' | 'rejected' | 'approved'>('completed') | ||
| const { fetchUser, updateBridgeCustomerData } = useAuth() | ||
|
|
||
| const { | ||
| setStep: setActiveStep, | ||
|
|
@@ -98,6 +96,7 @@ export const GlobalKYCComponent = ({ intialStep, offrampForm, setOfframpForm, on | |
| } | ||
| } else { | ||
| let data = await utils.getUserLinks(inputFormData) | ||
| await updateBridgeCustomerData(data) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Error handling is inconsistent across updateBridgeCustomerData calls The verification reveals inconsistent error handling patterns:
Locations needing attention:
🔗 Analysis chainVerify consistent error handling across all updateBridgeCustomerData calls. The function is called in multiple places with potentially different data shapes. Ensure error handling is consistent across all calls. Also applies to: 132-132, 187-187 Let me check the error handling implementation by looking for try-catch blocks around these calls. Let me try a different approach to check the error handling context. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check error handling consistency across updateBridgeCustomerData calls.
# Search for all updateBridgeCustomerData calls and their surrounding error handling
rg -A 5 -B 5 'updateBridgeCustomerData\('
Length of output: 3838 Script: #!/bin/bash
# Search for try-catch blocks containing updateBridgeCustomerData calls
ast-grep --pattern 'try {
$$$
updateBridgeCustomerData($$$)
$$$
} catch ($$$) {
$$$
}'
Length of output: 96 Script: #!/bin/bash
# Get more context around the function calls to see error handling patterns
rg -A 15 -B 15 'updateBridgeCustomerData\(' src/components/Global/KYCComponent/index.tsx
Length of output: 4894 |
||
| setCustomerObject(data) | ||
|
|
||
| let { tos_status: tosStatus, kyc_status: kycStatus } = data | ||
|
|
@@ -130,6 +129,7 @@ export const GlobalKYCComponent = ({ intialStep, offrampForm, setOfframpForm, on | |
| // @ts-ignore | ||
| if (!customerObject || customerObject.code === 'invalid_parameters') { | ||
| _customerObject = await utils.getUserLinks(_offrampForm) | ||
| await updateBridgeCustomerData(_customerObject) | ||
| setCustomerObject(_customerObject) | ||
| } else { | ||
| _customerObject = customerObject | ||
|
|
@@ -184,6 +184,7 @@ export const GlobalKYCComponent = ({ intialStep, offrampForm, setOfframpForm, on | |
| const _offrampForm = watchOfframp() | ||
| if (!customerObject) { | ||
| _customerObject = await utils.getUserLinks(_offrampForm) | ||
| await updateBridgeCustomerData(_customerObject) | ||
| setCustomerObject(_customerObject) | ||
| } else { | ||
| _customerObject = customerObject | ||
|
|
@@ -258,10 +259,10 @@ export const GlobalKYCComponent = ({ intialStep, offrampForm, setOfframpForm, on | |
|
|
||
| // Get customer ID | ||
| const customer = await utils.getStatus(_customerObject.id, 'customer_id') | ||
| setCustomerObject({ ..._customerObject, customer_id: customer.customer_id }) | ||
| setCustomerObject({ ..._customerObject, customer_id: customer.id }) | ||
|
|
||
| // Update peanut user with bridge customer id | ||
| const updatedUser = await updateBridgeCustomerId(customer.customer_id) | ||
| await updateBridgeCustomerData(customer) | ||
|
|
||
| // recipientType === 'us' && setAddressRequired(true) | ||
| setLoadingState('Idle') | ||
|
|
@@ -307,7 +308,6 @@ export const GlobalKYCComponent = ({ intialStep, offrampForm, setOfframpForm, on | |
| }) | ||
|
|
||
| // Ensure the state updates happen | ||
| setKycStatus('completed') | ||
| if (onCompleted) { | ||
| onCompleted('KYC completed') | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eww