first commit
This commit is contained in:
157
app/api/user/accounts/route.ts
Normal file
157
app/api/user/accounts/route.ts
Normal file
@@ -0,0 +1,157 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unauthorized' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const { provider, providerAccountId, access_token, refresh_token, expires_at } = await request.json()
|
||||
|
||||
if (!provider || !providerAccountId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing required fields' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Check if this account is already linked to another user
|
||||
const existingAccount = await prisma.account.findFirst({
|
||||
where: {
|
||||
provider,
|
||||
providerAccountId,
|
||||
},
|
||||
})
|
||||
|
||||
if (existingAccount) {
|
||||
return NextResponse.json(
|
||||
{ error: 'This account is already linked to another user' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Link the account to the current user
|
||||
const account = await prisma.account.create({
|
||||
data: {
|
||||
userId: session.user.id,
|
||||
type: 'oauth',
|
||||
provider,
|
||||
providerAccountId,
|
||||
access_token,
|
||||
refresh_token,
|
||||
expires_at,
|
||||
token_type: 'Bearer',
|
||||
scope: 'email profile openid',
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Account linked successfully',
|
||||
account: {
|
||||
id: account.id,
|
||||
provider: account.provider,
|
||||
},
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error linking account:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unauthorized' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const provider = searchParams.get('provider')
|
||||
|
||||
if (!provider) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Provider is required' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
// Remove the linked account
|
||||
const deletedAccount = await prisma.account.deleteMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
provider,
|
||||
},
|
||||
})
|
||||
|
||||
if (deletedAccount.count === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Account not found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: 'Account unlinked successfully',
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error unlinking account:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unauthorized' },
|
||||
{ status: 401 }
|
||||
)
|
||||
}
|
||||
|
||||
// Get all linked accounts for the current user
|
||||
const accounts = await prisma.account.findMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
provider: true,
|
||||
type: true,
|
||||
},
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
accounts,
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching linked accounts:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
53
app/api/user/addresses/[id]/default/route.ts
Normal file
53
app/api/user/addresses/[id]/default/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id: addressId } = await params
|
||||
|
||||
// Check if address belongs to user
|
||||
const existingAddress = await prisma.address.findFirst({
|
||||
where: {
|
||||
id: addressId,
|
||||
userId: session.user.id
|
||||
}
|
||||
})
|
||||
|
||||
if (!existingAddress) {
|
||||
return NextResponse.json({ error: 'Address not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
// Unset all other default addresses for this user
|
||||
await prisma.address.updateMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
isDefault: true
|
||||
},
|
||||
data: { isDefault: false }
|
||||
})
|
||||
|
||||
// Set this address as default
|
||||
const address = await prisma.address.update({
|
||||
where: { id: addressId },
|
||||
data: { isDefault: true }
|
||||
})
|
||||
|
||||
return NextResponse.json({ address })
|
||||
} catch (error) {
|
||||
console.error('Error setting default address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to set default address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
122
app/api/user/addresses/[id]/route.ts
Normal file
122
app/api/user/addresses/[id]/route.ts
Normal file
@@ -0,0 +1,122 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id: addressId } = await params
|
||||
const data = await request.json()
|
||||
const {
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country,
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
} = data
|
||||
|
||||
// Check if address belongs to user
|
||||
const existingAddress = await prisma.address.findFirst({
|
||||
where: {
|
||||
id: addressId,
|
||||
userId: session.user.id
|
||||
}
|
||||
})
|
||||
|
||||
if (!existingAddress) {
|
||||
return NextResponse.json({ error: 'Address not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
// If setting as default, unset other default addresses
|
||||
if (isDefault) {
|
||||
await prisma.address.updateMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
isDefault: true,
|
||||
id: { not: addressId }
|
||||
},
|
||||
data: { isDefault: false }
|
||||
})
|
||||
}
|
||||
|
||||
const address = await prisma.address.update({
|
||||
where: { id: addressId },
|
||||
data: {
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country: country || 'India',
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ address })
|
||||
} catch (error) {
|
||||
console.error('Error updating address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { id: addressId } = await params
|
||||
|
||||
// Check if address belongs to user
|
||||
const existingAddress = await prisma.address.findFirst({
|
||||
where: {
|
||||
id: addressId,
|
||||
userId: session.user.id
|
||||
}
|
||||
})
|
||||
|
||||
if (!existingAddress) {
|
||||
return NextResponse.json({ error: 'Address not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
await prisma.address.delete({
|
||||
where: { id: addressId }
|
||||
})
|
||||
|
||||
return NextResponse.json({ message: 'Address deleted successfully' })
|
||||
} catch (error) {
|
||||
console.error('Error deleting address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to delete address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
94
app/api/user/addresses/route.ts
Normal file
94
app/api/user/addresses/route.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const addresses = await prisma.address.findMany({
|
||||
where: { userId: session.user.id },
|
||||
orderBy: [
|
||||
{ isDefault: 'desc' },
|
||||
{ createdAt: 'desc' }
|
||||
]
|
||||
})
|
||||
|
||||
return NextResponse.json({ addresses })
|
||||
} catch (error) {
|
||||
console.error('Error fetching addresses:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch addresses' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const data = await request.json()
|
||||
const {
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country,
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
} = data
|
||||
|
||||
// If setting as default, unset other default addresses
|
||||
if (isDefault) {
|
||||
await prisma.address.updateMany({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
isDefault: true
|
||||
},
|
||||
data: { isDefault: false }
|
||||
})
|
||||
}
|
||||
|
||||
const address = await prisma.address.create({
|
||||
data: {
|
||||
userId: session.user.id,
|
||||
firstName,
|
||||
lastName,
|
||||
company,
|
||||
address1,
|
||||
address2,
|
||||
city,
|
||||
state,
|
||||
zipCode,
|
||||
country: country || 'India',
|
||||
phone,
|
||||
isDefault,
|
||||
type
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({ address })
|
||||
} catch (error) {
|
||||
console.error('Error creating address:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create address' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
58
app/api/user/change-password/route.ts
Normal file
58
app/api/user/change-password/route.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
import bcrypt from 'bcryptjs'
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const { currentPassword, newPassword } = await request.json()
|
||||
|
||||
if (!currentPassword || !newPassword) {
|
||||
return NextResponse.json({ error: 'All fields are required' }, { status: 400 })
|
||||
}
|
||||
|
||||
if (newPassword.length < 6) {
|
||||
return NextResponse.json({ error: 'Password must be at least 6 characters long' }, { status: 400 })
|
||||
}
|
||||
|
||||
// Get current user with password
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: session.user.id },
|
||||
select: { password: true }
|
||||
})
|
||||
|
||||
if (!user || !user.password) {
|
||||
return NextResponse.json({ error: 'User not found or password not set' }, { status: 404 })
|
||||
}
|
||||
|
||||
// Verify current password
|
||||
const isCurrentPasswordValid = await bcrypt.compare(currentPassword, user.password)
|
||||
|
||||
if (!isCurrentPasswordValid) {
|
||||
return NextResponse.json({ error: 'Current password is incorrect' }, { status: 400 })
|
||||
}
|
||||
|
||||
// Hash new password
|
||||
const hashedNewPassword = await bcrypt.hash(newPassword, 12)
|
||||
|
||||
// Update password
|
||||
await prisma.user.update({
|
||||
where: { id: session.user.id },
|
||||
data: { password: hashedNewPassword }
|
||||
})
|
||||
|
||||
return NextResponse.json({ message: 'Password updated successfully' })
|
||||
} catch (error) {
|
||||
console.error('Error changing password:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to change password' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
83
app/api/user/profile/route.ts
Normal file
83
app/api/user/profile/route.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { auth } from '@/auth'
|
||||
import { prisma } from '@/lib/prisma'
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: session.user.id },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
phone: true,
|
||||
address: true,
|
||||
image: true,
|
||||
role: true,
|
||||
referralCode: true,
|
||||
isActive: true,
|
||||
joinedAt: true
|
||||
}
|
||||
})
|
||||
|
||||
if (!user) {
|
||||
return NextResponse.json({ error: 'User not found' }, { status: 404 })
|
||||
}
|
||||
|
||||
return NextResponse.json(user)
|
||||
} catch (error) {
|
||||
console.error('Error fetching user profile:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to fetch user profile' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export async function PUT(request: NextRequest) {
|
||||
try {
|
||||
const session = await auth()
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
||||
}
|
||||
|
||||
const data = await request.json()
|
||||
const { name, phone, address } = data
|
||||
|
||||
const updatedUser = await prisma.user.update({
|
||||
where: { id: session.user.id },
|
||||
data: {
|
||||
name: name || undefined,
|
||||
phone: phone || undefined,
|
||||
address: address || undefined
|
||||
},
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
phone: true,
|
||||
address: true,
|
||||
role: true,
|
||||
referralCode: true,
|
||||
isActive: true,
|
||||
joinedAt: true
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json(updatedUser)
|
||||
} catch (error) {
|
||||
console.error('Error updating user profile:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to update user profile' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user