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.

58 changes: 58 additions & 0 deletions src/client/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,73 @@ function App() {
* */

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

if(!res.ok) {
throw new Error('fetching failed')
}

const data = await res.json()
console.log('User registered', data)
} catch (error) {
console.log('Error registreing user: ', error)
}
};

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

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

if(!res.ok) {
throw new Error('fetching failed')
}
const data = await res.json()
const token = data.data
localStorage.setItem('token', token)

console.log('token is : ', token)
} catch (e) {
console.log('Error Loging user: ', e)
}
};

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

const token = localStorage.getItem('token')

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

if(!res.ok) {
throw new Error('Fetching failed')
}
const data = await res.json()
console.log('Movie Created: ', data)

} catch (error) {
console.log('Error creating movie: ', error)
}
}

return (
Expand Down
25 changes: 17 additions & 8 deletions src/server/controllers/movie.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,27 @@ const getAllMovies = async (req, res) => {
};

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

const { title, description, runtimeMins } = req.body
try {
const token = null;
// todo verify the token
const [_,token] = req.headers.authorization.split(' ')

const decodedToken = jwt.verify(token, jwtSecret)

if(!decodedToken) {
throw new Error('Access Denied')
}
const createdMovie = await prisma.movie.create({
data : {
title : title,
description : description,
runtimeMins : runtimeMins
}
})
res.status(201).json({ data: createdMovie });
} catch (e) {
return res.status(401).json({ error: 'Invalid token provided.' })
return res.status(401).json({ error: 'Invalid data provided.' })
}

const createdMovie = null;

res.json({ data: createdMovie });
};

export {
Expand Down
48 changes: 39 additions & 9 deletions src/server/controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,66 @@
import bcrypt from 'bcrypt';
import bcrypt, { genSalt } from 'bcrypt';
import jwt from 'jsonwebtoken';
import PrismaClientKnownRequestError from "@prisma/client"
import { PrismaClient } from '@prisma/client'
import { json } from 'express';
const prisma = new PrismaClient();


const jwtSecret = 'mysecret';

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

const createdUser = null;

res.json({ data: createdUser });
try {
if(!username || !password) {
return res.status(400).json({
error: "Missing fields in request body"
})
}

const saltRound = 10
const hashedPassword = await bcrypt.hash(password, saltRound)
const registeredUser = await prisma.user.create({
data : {
username,
password : hashedPassword
}
})

res.status(201).json({ user: registeredUser });
} catch (e) {
if(e instanceof PrismaClientKnownRequestError) {
return res.status(400).json({error : 'Something went Wrong at User controller!'})
} else {
return res.status(403).json({error : 'Something went Wrong at User controller!'})
}
}

};

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

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

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

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

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

const token = null;
const token = jwt.sign({ sub : foundUser.username }, jwtSecret)

res.json({ data: token });
res.status(200).json({ data: token });
} catch (error) {
return res.status(500).json({error : 'An error occured duiting login!'})
}
};

export {
Expand Down
19 changes: 19 additions & 0 deletions src/server/domains/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// import { PrismaClient } from '@prisma/client'
// const prisma = new PrismaClient()
// import bcrypt, { genSalt } from 'bcrypt'

// const registerDb = async (username, password) => {

// return await prisma.user.create({
// data : {
// username,
// password : password
// }
// })
// }


// export { registerDb }