sync
This commit is contained in:
68
lib/auth.ts
Normal file
68
lib/auth.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import { cookies } from 'next/headers'
|
||||
import pool from './db'
|
||||
|
||||
export interface User {
|
||||
id: number
|
||||
username: string
|
||||
email: string
|
||||
}
|
||||
|
||||
// Get the current user from session cookie
|
||||
export async function getCurrentUser(): Promise<User | null> {
|
||||
try {
|
||||
const cookieStore = await cookies()
|
||||
const buyerId = cookieStore.get('buyer_id')?.value
|
||||
|
||||
if (!buyerId) {
|
||||
return null
|
||||
}
|
||||
|
||||
const [rows] = await pool.execute(
|
||||
'SELECT id, username, email FROM buyers WHERE id = ?',
|
||||
[buyerId]
|
||||
)
|
||||
|
||||
const buyers = rows as any[]
|
||||
if (buyers.length === 0) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
id: buyers[0].id,
|
||||
username: buyers[0].username,
|
||||
email: buyers[0].email,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error getting current user:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
// Get buyer ID from request cookies (for API routes)
|
||||
export async function getBuyerIdFromRequest(
|
||||
request: Request
|
||||
): Promise<number | null> {
|
||||
try {
|
||||
const cookieHeader = request.headers.get('cookie')
|
||||
if (!cookieHeader) {
|
||||
return null
|
||||
}
|
||||
|
||||
const cookies = cookieHeader.split(';').reduce((acc, cookie) => {
|
||||
const [key, value] = cookie.trim().split('=')
|
||||
acc[key] = value
|
||||
return acc
|
||||
}, {} as Record<string, string>)
|
||||
|
||||
const buyerId = cookies['buyer_id']
|
||||
if (!buyerId) {
|
||||
return null
|
||||
}
|
||||
|
||||
return parseInt(buyerId, 10)
|
||||
} catch (error) {
|
||||
console.error('Error getting buyer ID from request:', error)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
33
lib/nowpayments.ts
Normal file
33
lib/nowpayments.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
// NOWPayments API configuration
|
||||
export function getNowPaymentsConfig() {
|
||||
const isTestnet = process.env.NOWPAYMENTS_TESTNET === 'true'
|
||||
|
||||
// For testnet, use sandbox API key if available, otherwise fall back to regular API key
|
||||
const apiKey = isTestnet
|
||||
? (process.env.NOWPAYMENTS_SANDBOX_API_KEY || process.env.NOWPAYMENTS_API_KEY || '')
|
||||
: (process.env.NOWPAYMENTS_API_KEY || '')
|
||||
|
||||
// Sandbox/testnet uses api-sandbox.nowpayments.io
|
||||
// If the environment variable is not explicitly set, default to production
|
||||
const baseUrl = isTestnet
|
||||
? 'https://api-sandbox.nowpayments.io'
|
||||
: 'https://api.nowpayments.io'
|
||||
|
||||
// Currency configuration
|
||||
// Default: USD for testnet (sandbox doesn't support CHF), CHF for production
|
||||
// Can be overridden with NOWPAYMENTS_CURRENCY env variable
|
||||
const defaultCurrency = isTestnet ? 'usd' : 'chf'
|
||||
const currency = (process.env.NOWPAYMENTS_CURRENCY || defaultCurrency).toLowerCase()
|
||||
|
||||
if (isTestnet) {
|
||||
console.log('Using NOWPayments Sandbox/Testnet environment')
|
||||
}
|
||||
|
||||
return {
|
||||
baseUrl,
|
||||
apiKey,
|
||||
isTestnet,
|
||||
currency,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user