Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
0e8cb61
Added basic login functionality
Kalu32k May 29, 2025
0b77888
Added username cookie when login is correct
Kalu32k May 29, 2025
91ed064
Added test page for login
Kalu32k May 29, 2025
55bcc02
Fixed bug navigating to secret page before verifying if login is ok
Kalu32k May 29, 2025
c112101
Added logout button to clear auth cookie and navigating to login page
Kalu32k May 29, 2025
1902b53
Added user schema for MongoDB model with username and password fields
Kalu32k Jun 2, 2025
f362a92
Refactor login API to verify credentials against database
Kalu32k Jun 2, 2025
731a023
Refactor logout API to await cookie before clearing username
Kalu32k Jun 2, 2025
b268d46
Added user registration API
Kalu32k Jun 2, 2025
456ded1
Update navigation on successful login
Kalu32k Jun 2, 2025
ddb9509
Added account page to display very basic user login status and provid…
Kalu32k Jun 2, 2025
2c8fd19
Remove Secret page that is now the account page
Kalu32k Jun 2, 2025
da9e820
Added registration page to create accounts
Kalu32k Jun 2, 2025
2511216
Update NavMenu to manage user login status with cookie checks
Kalu32k Jun 2, 2025
dfc6dc0
Added basic error text styling to login page
Kalu32k Jun 2, 2025
4105b44
Added user management functions including creation, retrieval, deleti…
Kalu32k Jun 2, 2025
8b303a9
Added basic login functionality
Kalu32k May 29, 2025
57aa3a5
Added username cookie when login is correct
Kalu32k May 29, 2025
c047570
Added test page for login
Kalu32k May 29, 2025
74be4ba
Fixed bug navigating to secret page before verifying if login is ok
Kalu32k May 29, 2025
f00010f
Added logout button to clear auth cookie and navigating to login page
Kalu32k May 29, 2025
f6e6144
Added user schema for MongoDB model with username and password fields
Kalu32k Jun 2, 2025
94300a0
Refactor login API to verify credentials against database
Kalu32k Jun 2, 2025
52d751b
Refactor logout API to await cookie before clearing username
Kalu32k Jun 2, 2025
12efa7e
Added user registration API
Kalu32k Jun 2, 2025
4cb0450
Update navigation on successful login
Kalu32k Jun 2, 2025
251e81b
Added account page to display very basic user login status and provid…
Kalu32k Jun 2, 2025
a0a30c9
Remove Secret page that is now the account page
Kalu32k Jun 2, 2025
402f38f
Added registration page to create accounts
Kalu32k Jun 2, 2025
70c0c5c
Update NavMenu to manage user login status with cookie checks
Kalu32k Jun 2, 2025
e660668
Added basic error text styling to login page
Kalu32k Jun 2, 2025
0e17d98
Added user management functions including creation, retrieval, deleti…
Kalu32k Jun 2, 2025
3d79515
Merge branch '8.2-login-functionality' of https://github.com/AlexCode…
Kalu32k Jun 2, 2025
ed670dd
Added admin user management functionality and update admin panel navi…
Kalu32k Jun 4, 2025
d28b489
Updated login API to include admin status in response cookies
Kalu32k Jun 4, 2025
6dea7bb
Added admin cookie clearing on logout
Kalu32k Jun 4, 2025
28fb89a
Updated user management and admin status in user retrieval and creati…
Kalu32k Jun 4, 2025
fc5528e
Added toggle admin functionality with POST endpoint
Kalu32k Jun 4, 2025
571dbc7
Added POST endpoint for user deletion functionality
Kalu32k Jun 4, 2025
3b0d133
Added GET endpoint for retrieving users with admin authorization
Kalu32k Jun 4, 2025
d0e638b
Added POST endpoint for changing user password
Kalu32k Jun 4, 2025
cad7a6f
Added change password page
Kalu32k Jun 4, 2025
cbd8b6a
Updated login page,register page and account page translating some wo…
Kalu32k Jun 4, 2025
f1bce9d
Added Admin field to user schema with default value set to false
Kalu32k Jun 4, 2025
980ec78
Updated LogoutButton and NavMenu to improve login status handling and…
Kalu32k Jun 4, 2025
4c38bc3
Adding jsonwebtoken
Kalu32k Jun 5, 2025
1ff5740
Adding jwt-decode for jsonwebtoken handling
Kalu32k Jun 5, 2025
cc6691b
Updated login/logout, account and admin page to work with JWT
Kalu32k Jun 5, 2025
265c069
Adding bcrypt for password hashing
Kalu32k Jun 5, 2025
08cd091
Added password hashing for user registration and login & updated forg…
Kalu32k Jun 5, 2025
8210e15
Removed some testing logs
Kalu32k Jun 5, 2025
3633f27
Added basic login functionality
Kalu32k May 29, 2025
6ed12af
Added username cookie when login is correct
Kalu32k May 29, 2025
efd7e2f
Added test page for login
Kalu32k May 29, 2025
b101ca8
Fixed bug navigating to secret page before verifying if login is ok
Kalu32k May 29, 2025
0e3670c
Added logout button to clear auth cookie and navigating to login page
Kalu32k May 29, 2025
1903fdc
Added user schema for MongoDB model with username and password fields
Kalu32k Jun 2, 2025
b6854f3
Refactor login API to verify credentials against database
Kalu32k Jun 2, 2025
26b0f50
Refactor logout API to await cookie before clearing username
Kalu32k Jun 2, 2025
3a92345
Added user registration API
Kalu32k Jun 2, 2025
3d4f72c
Update navigation on successful login
Kalu32k Jun 2, 2025
4e3f850
Added account page to display very basic user login status and provid…
Kalu32k Jun 2, 2025
c64f570
Remove Secret page that is now the account page
Kalu32k Jun 2, 2025
8c23b03
Added registration page to create accounts
Kalu32k Jun 2, 2025
a3553a9
Update NavMenu to manage user login status with cookie checks
Kalu32k Jun 2, 2025
56cc603
Added basic error text styling to login page
Kalu32k Jun 2, 2025
317c76a
Added user management functions including creation, retrieval, deleti…
Kalu32k Jun 2, 2025
9315785
Added test page for login
Kalu32k May 29, 2025
15b8c38
Fixed bug navigating to secret page before verifying if login is ok
Kalu32k May 29, 2025
90a2d73
Added logout button to clear auth cookie and navigating to login page
Kalu32k May 29, 2025
bf4546a
Remove Secret page that is now the account page
Kalu32k Jun 2, 2025
ad8857c
Added admin user management functionality and update admin panel navi…
Kalu32k Jun 4, 2025
e1405c1
Updated login API to include admin status in response cookies
Kalu32k Jun 4, 2025
871d865
Added admin cookie clearing on logout
Kalu32k Jun 4, 2025
9e70e96
Updated user management and admin status in user retrieval and creati…
Kalu32k Jun 4, 2025
3db8bb5
Added toggle admin functionality with POST endpoint
Kalu32k Jun 4, 2025
d11602b
Added POST endpoint for user deletion functionality
Kalu32k Jun 4, 2025
6c0741e
Added GET endpoint for retrieving users with admin authorization
Kalu32k Jun 4, 2025
e0843eb
Added POST endpoint for changing user password
Kalu32k Jun 4, 2025
98d7d31
Added change password page
Kalu32k Jun 4, 2025
027f734
Updated login page,register page and account page translating some wo…
Kalu32k Jun 4, 2025
cceac5a
Added Admin field to user schema with default value set to false
Kalu32k Jun 4, 2025
0f0f52a
Updated LogoutButton and NavMenu to improve login status handling and…
Kalu32k Jun 4, 2025
153ce01
Adding jsonwebtoken
Kalu32k Jun 5, 2025
e234308
Adding jwt-decode for jsonwebtoken handling
Kalu32k Jun 5, 2025
bbeb1ba
Updated login/logout, account and admin page to work with JWT
Kalu32k Jun 5, 2025
d4ee9c9
Adding bcrypt for password hashing
Kalu32k Jun 5, 2025
a2035b1
Added password hashing for user registration and login & updated forg…
Kalu32k Jun 5, 2025
5d1966c
Removed some testing logs
Kalu32k Jun 5, 2025
9848e4a
Merge branch '8.2-login-functionality' of https://github.com/AlexCode…
Kalu32k Jun 5, 2025
7859285
Merge branch 'main' into 8.2-login-functionality
AlexCode-dot Jun 6, 2025
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
143 changes: 141 additions & 2 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
"watch-css": "sass --watch src/styles/main.scss:public/dist/styles.css"
},
"dependencies": {
"bcrypt": "^6.0.0",
"dotenv": "^16.5.0",
"formdata-node": "^6.0.3",
"jsonwebtoken": "^9.0.2",
"jwt-decode": "^4.0.0",
"html2pdf.js": "^0.10.3",
"mongoose": "^8.15.0",
"next": "15.3.1",
Expand Down
39 changes: 37 additions & 2 deletions src/app/admin/page.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use client'

import { useState } from 'react'
import { useState, useEffect } from 'react'
import AdminRoomForm from '@/components/admin/AdminRoomForm'
import AdminScreeningForm from '@/components/admin/AdminScreeningForm'
import AdminMovieForm from '@/components/admin/AdminMovieForm'
Expand All @@ -12,10 +11,31 @@ import { useAdminData } from '@/hooks/useAdminData'
import AdminTabNav from '@/components/admin/AdminTabNav'
import AdminRoomList from '@/components/admin/AdminRoomList'
import AdminBookingPanel from '@/components/admin/AdminBookingPanel'
import AdminCreateUser from '@/components/admin/AdminCreateUser'
import { jwtDecode } from 'jwt-decode'

export default function AdminPanel() {
const [isAdmin, setIsAdmin] = useState(false)
const [checked, setChecked] = useState(false)
const [activeTab, setActiveTab] = useState('list')

useEffect(() => {
const cookies = document.cookie.split(';').map((c) => c.trim())
const jwtCookie = cookies.find((c) => c.startsWith('JWT='))
if (jwtCookie) {
const token = jwtCookie.split('=')[1]
try {
const decoded = jwtDecode(token)
setIsAdmin(decoded.admin)
} catch (e) {
setIsAdmin(false)
}
} else {
setIsAdmin(false)
}
setChecked(true)
}, [])

const {
movies,
screenings,
Expand All @@ -42,6 +62,14 @@ export default function AdminPanel() {
loading,
} = useAdminData()

if (!checked) {
return <div>Laddar...</div>
}

if (!isAdmin) {
return <h1>Du är inte admin!</h1>
}

return (
<main className="admin-page">
<div className="admin-page__container">
Expand Down Expand Up @@ -128,6 +156,13 @@ export default function AdminPanel() {
</section>
</>
)}
{activeTab === 'user1' && (
<>
<section className="admin-page__section">
<AdminCreateUser />
</section>
</>
)}
</div>

{successMessage && <SuccessModal message={successMessage} onClose={() => setSuccessMessage(null)} />}
Expand Down
18 changes: 18 additions & 0 deletions src/app/api/changepassword/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { updateUserPassword } from '@/lib/db/userDbService'
import { NextResponse } from 'next/server'

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

console.log(`Received request to update password for user: ${payload.Username}`)

try {
const updatedUser = await updateUserPassword(payload.Username, payload.Password)

console.log(`Password updated successfully for user: ${updatedUser.Username}`)
return NextResponse.json({ message: 'Password updated successfully' }, { status: 200 })
} catch (error) {
console.error(`Error updating password for user ${username}:`, error)
return NextResponse.json({ error: 'Failed to update password' }, { status: 500 })
}
}
17 changes: 17 additions & 0 deletions src/app/api/deleteuser/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { deleteUserByUsername } from '@/lib/db/userDbService'

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

if (!username) {
return new Response('Username is required', { status: 400 })
}

try {
await deleteUserByUsername(username)
return new Response(`User ${username} deleted successfully`, { status: 200 })
} catch (error) {
console.error('Error deleting user:', error)
return new Response('Failed to delete user', { status: 500 })
}
}
30 changes: 30 additions & 0 deletions src/app/api/getusers/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { NextResponse } from 'next/server'
import connectDB from '@/lib/db/connectDB'
import { getAllUsers } from '@/lib/db/userDbService'
import { jwtDecode } from 'jwt-decode'
export async function GET(request) {
const cookieHeader = request.headers.get('cookie') || ''
const cookies = cookieHeader.split(';').map((c) => c.trim())
const jwtCookie = cookies.find((c) => c.startsWith('JWT='))
if (!jwtCookie) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}

const token = jwtCookie.split('=')[1]
try {
const decoded = jwtDecode(token)
if (!decoded.admin) {
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
}
} catch (error) {
return NextResponse.json({ error: 'Invalid token' }, { status: 401 })
}
try {
await connectDB()
const users = await getAllUsers()
return NextResponse.json(users, { status: 200 })
} catch (error) {
console.error('Error fetching users:', error)
return NextResponse.json({ error: 'Failed to fetch users' }, { status: 500 })
}
}
Loading