Skip to content
Open
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
4 changes: 0 additions & 4 deletions .env.example

This file was deleted.

7 changes: 4 additions & 3 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ generator client {
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
provider = "postgresql"
url = env("DATABASE_URL")

directUrl = env("postgresql://blog_owner:[email protected]/blog?sslmode=require")
}

model User {
Expand Down
79 changes: 74 additions & 5 deletions src/client/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,85 @@ function App() {
* */

const handleRegister = async ({ username, password }) => {

try {
const response = await fetch(`${apiUrl}/user/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});

if (!response.ok) {
throw new Error('Registration failed');
}

const data = await response.json();
console.log('User registered:', data);
// You might want to automatically log in the user or show a success message
} catch (error) {
console.error('Registration error:', error);
// Handle the error (e.g., show an error message to the user)
}
};

const handleLogin = async ({ username, password }) => {

try {
const response = await fetch(`${apiUrl}/user/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ username, password }),
});

if (!response.ok) {
throw new Error('Login failed');
}

const { data: token } = await response.json();

// Save the token in local storage
localStorage.setItem('token', token);

console.log('User logged in successfully');
// You might want to update the app state to reflect that the user is logged in
} catch (error) {
console.error('Login error:', error);
// Handle the error (e.g., show an error message to the user)
}
};

const handleCreateMovie = async ({ title, description, runtimeMins }) => {

}
try {
const token = localStorage.getItem('token');
if (!token) {
throw new Error('No token found. Please login first.');
}

const response = await fetch(`${apiUrl}/movie`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify({ title, description, runtimeMins }),
});

if (!response.ok) {
throw new Error('Failed to create movie');
}

const { data: newMovie } = await response.json();

// Update the movies state to include the new movie
setMovies(prevMovies => [...prevMovies, newMovie]);

console.log('Movie created successfully');
} catch (error) {
console.error('Create movie error:', error);
// Handle the error (e.g., show an error message to the user)
}
};

return (
<div className="App">
Expand Down
50 changes: 37 additions & 13 deletions src/server/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,53 @@ const jwtSecret = 'mysecret';
const register = async (req, res) => {
const { username, password } = req.body;

const createdUser = null;

res.json({ data: createdUser });
try {
// Hash the password
const hashedPassword = await bcrypt.hash(password, 10);

// Create a new user
const createdUser = await prisma.user.create({
data: {
username,
password: hashedPassword,
},
});

// Remove the password from the response
const { password: _, ...userWithoutPassword } = createdUser;

res.json({ data: userWithoutPassword });
} catch (error) {
console.error('Registration error:', error);
res.status(500).json({ error: 'Registration failed' });
}
};

const login = async (req, res) => {
const { username, password } = req.body;

const foundUser = null;
try {
const foundUser = await prisma.user.findUnique({
where: { username },
});

if (!foundUser) {
return res.status(401).json({ error: 'Invalid username or password.' });
}
if (!foundUser) {
return res.status(401).json({ error: 'Invalid username or password.' });
}

const passwordsMatch = false;
const passwordsMatch = await bcrypt.compare(password, foundUser.password);

if (!passwordsMatch) {
return res.status(401).json({ error: 'Invalid username or password.' });
}
if (!passwordsMatch) {
return res.status(401).json({ error: 'Invalid username or password.' });
}

const token = null;
const token = jwt.sign({ username: foundUser.username }, jwtSecret, { expiresIn: '1h' });

res.json({ data: token });
res.json({ data: token });
} catch (error) {
console.error('Login error:', error);
res.status(500).json({ error: 'Login failed' });
}
};

export {
Expand Down
43 changes: 43 additions & 0 deletions src/server/routers/movie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,49 @@
import express from 'express';
import { getAllMovies, createMovie } from '../controllers/movie.js';
import jwt from 'jsonwebtoken';
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient();

const jwtSecret = 'mysecret';

const createMovie = async (req, res) => {
const { title, description, runtimeMins } = req.body;

try {
// 1. Get the token from the appropriate request header
const token = req.headers.authorization?.split(' ')[1];

if (!token) {
return res.status(401).json({ error: 'No token provided' });
}

// 2. Verify the token using the jsonwebtoken library
const decoded = jwt.verify(token, jwtSecret);

// 3. Create the movie and store the result in the createdMovie variable
const createdMovie = await prisma.movie.create({
data: {
title,
description,
runtimeMins: parseInt(runtimeMins),
createdBy: decoded.username, // Assuming the username is stored in the token
},
});

res.json({ data: createdMovie });
} catch (error) {
console.error('Create movie error:', error);
if (error.name === 'JsonWebTokenError') {
return res.status(401).json({ error: 'Invalid token provided.' });
}
res.status(500).json({ error: 'Failed to create movie' });
}
};

export {
getAllMovies,
createMovie
};
const router = express.Router();

router.get('/', getAllMovies);
Expand Down