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
17 changes: 9 additions & 8 deletions src/api/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def login():

if user and bcrypt.check_password_hash(user.password, password):
access_token = create_access_token(identity=email)
return jsonify(access_token=access_token)
return jsonify(access_token=access_token, user=user.serialize())
else:
return jsonify({"msg": "Bad email or password"}), 401

Expand Down Expand Up @@ -184,11 +184,12 @@ def search():
return jsonify({"error": "Error fetching data from API."}), 500

# Create category for user
@api.route('/users/<int:user_id>/categories', methods=['POST'])
@api.route('/users/<int:user_id>/category', methods=['POST'])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This endpoint should be authenticated, i.e. should have @jwt_required

def create_category(user_id):
category_name = request.json.get("category_name", None)
# user_id = request.json.get("user_id", None)

# current_user_id = get_jwt_identity()
# if current_user_id != user_id:
# return jsonify({"error": "You are not authorized to perform this action"}), 403
if category_name is None:
return jsonify({"msg": "Please fill out the required fields"}), 400

Expand All @@ -200,13 +201,13 @@ def create_category(user_id):

#Get all of user's categories
@api.route('/users/<int:user_id>/categories', methods=['GET'])
@jwt_required() # Requires a valid JWT token
# @jwt_required() # Requires a valid JWT token
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be an authenticated endpoint, i.e. @jwt_required should be set on this function

def get_all_categories(user_id):

try:
current_user_id = get_jwt_identity()
if current_user_id != user_id:
return jsonify({"error": "You are not authorized to perform this action"}), 403
# current_user_id = get_jwt_identity()
# if current_user_id != user_id:
# return jsonify({"error": "You are not authorized to perform this action"}), 403

categories = User_Category.query.filter_by(user_id=user_id).all()
serialized_categories = [category.serialize() for category in categories]
Expand Down
2 changes: 1 addition & 1 deletion src/front/js/pages/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const Home = () => {

// Sends the user to their page if logged in
useEffect(() => {
if (store.token && store.token != "" && store.token != undefined) {
if (store.token && store.token != "" && store.token && store.user != undefined) {
forward("/user-home");
}
}, [store.token, forward]);
Expand Down
24 changes: 18 additions & 6 deletions src/front/js/pages/user-home.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,35 @@ export const UserHome = () => {
const forward = useNavigate();
const [isModalOpen, setIsModalOpen] = useState(false);
const [categoryName, setCategoryName] = useState("");

const [ categories, setCategories ] = useState(null)

// Sends the user to the main home page if not logged in
useEffect(() => {
// if (store.token == null) {
// forward("/");
// }
}, [store.token, forward]);
if (!store.token || store.user == null) {
forward("/");
}
const getUserCategories = async() => {
const userCategories = await actions.getUserCategories()
if(userCategories) {
console.log("Got the categories!", userCategories)
setCategories(userCategories)
}
}
getUserCategories()
console.log("User category state", categories)
}, [store.token]);


const toggleModal = () => {
setIsModalOpen(!isModalOpen);
};
const handleCategoryNameChange = (e) => {
setCategoryName(e.target.value); // Update category name when input changes
};
const handleCreateCategory = () => {
const handleCreateCategory = async() => {
// Handle saving the category name (e.g., send it to an API or store it in state)
console.log("Category name:", categoryName);
await actions.createCategory(categoryName)
// You can add your logic here to save the category name
// For now, I'm just logging it to the console
};
Expand Down
42 changes: 39 additions & 3 deletions src/front/js/store/flux.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
const base = "https://orange-broccoli-j4rpj6prpr2qpvq-3001.app.github.dev/api/";
const base = process.env.BACKEND_URL + "/api/";

const getState = ({ getStore, getActions, setStore }) => {
return {
store: {
token: null,
user: null,
items: []
},
actions: {
Expand Down Expand Up @@ -45,7 +46,7 @@ const getState = ({ getStore, getActions, setStore }) => {
const result = await response.json();
console.log("This came from the back-end", result);
sessionStorage.setItem("token", result.access_token);
setStore({ token: result.access_token });
setStore({ token: result.access_token, user: result.user });
return true;
} catch (error) {
console.error('Error fetching data:', error);
Expand Down Expand Up @@ -129,7 +130,42 @@ const getState = ({ getStore, getActions, setStore }) => {
setStore({ items: storedItems });
};
console.log(getStore().items)
}
},
createCategory: async (category)=>{
const store = getStore();
console.log("STORE", store)
try {
const resp = await fetch(base + `users/${store.user.id}/category`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({category_name: category})
})
const data = await resp.json()
console.log("Data after creating category", data)
} catch(error) {
console.log("Error creating category", error)
throw new Error
}
},
getUserCategories: async () => {
const store = getStore();
try {
const resp = await fetch(base + `users/${store.user.id}/categories`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
}
})
const data = await resp.json()
console.log("Data after getting categories", data)
return data
} catch(error) {
console.log("Error getting categories", error)
throw new Error
}
},
}
};
};
Expand Down