Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 16 additions & 61 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions src/app/api/bookings/[id]/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export async function GET(req, { params }) {
}

export async function PATCH(req, context) {
if (requireAdminAccess()) {
const isAdmin = await requireAdminAccess()

if (!isAdmin) {
return NextResponse.json({ error: 'Endast tillgängligt för administratörer' }, { status: 403 })
}
try {
Expand Down Expand Up @@ -40,7 +42,9 @@ export async function PATCH(req, context) {
}

export async function DELETE(req, context) {
if (requireAdminAccess()) {
const isAdmin = await requireAdminAccess()

if (!isAdmin) {
return NextResponse.json({ error: 'Endast tillgängligt för administratörer' }, { status: 403 })
}

Expand Down
4 changes: 3 additions & 1 deletion src/app/api/bookings/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export async function POST(request) {
}

export async function GET() {
if (requireAdminAccess()) {
const isAdmin = await requireAdminAccess()

if (!isAdmin) {
return NextResponse.json({ error: 'Endast tillgängligt för administratörer' }, { status: 403 })
}

Expand Down
6 changes: 6 additions & 0 deletions src/app/api/deleteuser/route.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { deleteUserByUsername } from '@/lib/db/userDbService'
import { requireAdminAccess } from '@/lib/auth/requireAdminAccess'

export async function POST(request) {
const isAdmin = await requireAdminAccess()

if (!isAdmin) {
return NextResponse.json({ error: 'Endast tillgängligt för administratörer' }, { status: 403 })
}
const { username } = await request.json()

if (!username) {
Expand Down
59 changes: 34 additions & 25 deletions src/app/api/login/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,45 @@ import connectDB from '@/lib/db/connectDB'
import { findUserByUsername } from '@/lib/db/userDbService'
import { NextResponse } from 'next/server'
import jsonwebtoken from 'jsonwebtoken'
import hashPassword from '@/lib/utils/hashPassword'
import bcrypt from 'bcrypt'

export async function POST(request) {
const payload = await request.json()

// Log the payload for debugging
console.log(`${payload.Username} and ${payload.Password} received in the login API`)
console.log(`Login attempt with username: ${payload.Username} and password: ${payload.Password}`)
const { Username, Password } = payload

await connectDB()
let login = await findUserByUsername(payload.Username)

const secretpassword = await hashPassword(payload.Password)

if (login.Username == payload.Username) {
console.log(`User ${payload.Username} found in the database`)
const isMatch = await bcrypt.compare(payload.Password, login.Password)
console.log(`Password match for user ${payload.Username}: ${isMatch}`)
if (isMatch) {
const response = NextResponse.json({ message: 'Login successful' }, { status: 200 })
const jwtToken = jsonwebtoken.sign({ username: payload.Username, admin: login.Admin }, process.env.JWT_SECRET, {
expiresIn: '1h',
})
response.cookies.set('JWT', jwtToken, { httpOnly: false })
return response
} else login.Password !== secretpassword
{
console.log(`Password incorrect for user: ${payload.Username}`)
return NextResponse.json({ error: 'Invalid credentials' }, { status: 401 })
}
const user = await findUserByUsername(Username)

if (!user || user.Username !== Username) {
return NextResponse.json({ error: 'Invalid credentials' }, { status: 401 })
}

const isMatch = await bcrypt.compare(Password, user.Password)
if (!isMatch) {
return NextResponse.json({ error: 'Invalid credentials' }, { status: 401 })
}

const jwtToken = jsonwebtoken.sign({ username: user.Username, admin: user.Admin }, process.env.JWT_SECRET, {
expiresIn: '1h',
})

const response = NextResponse.json({ message: 'Login successful' }, { status: 200 })

response.cookies.set('token', jwtToken, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
maxAge: 60 * 60,
})

response.cookies.set('JWT', jwtToken, {
httpOnly: false,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
maxAge: 60 * 60,
})

return response
}
Loading