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
6 changes: 2 additions & 4 deletions apps/api/src/routes/auth/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@ export async function completeOAuth(req, res) {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
maxAge: process.env.FINGERPRINT_EXPIRATION_DAYS * 24 * 60 * 60 * 1000,
sameSite: "lax",
domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost"
sameSite: "none"
});
res.cookie("tokenrf", refreshToken, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
maxAge: process.env.REFRESH_TOKEN_EXPIRATION_DAYS * 24 * 60 * 60 * 1000,
sameSite: "lax",
domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost"
sameSite: "none"
});

res.json({ message: "OAuth login completed successfully" });
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/routes/auth/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default async function login(req, res) {
const refreshToken = generateRefreshToken(user._id, fingerprint, `${process.env.REFRESH_TOKEN_EXPIRATION_DAYS || 7}d`)

res.header("Authorization", `Bearer ${token}`)
res.cookie("securefp", fingerprint, { httpOnly: true, secure: process.env.NODE_ENV === "production", maxAge: process.env.FINGERPRINT_EXPIRATION_DAYS * 24 * 60 * 60 * 1000, sameSite: "lax", domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost" });
res.cookie("tokenrf", refreshToken, { httpOnly: true, secure: process.env.NODE_ENV === "production", maxAge: process.env.REFRESH_TOKEN_EXPIRATION_DAYS * 24 * 60 * 60 * 1000, sameSite: "lax", domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost" });
res.cookie("securefp", fingerprint, { httpOnly: true, secure: process.env.NODE_ENV === "production", maxAge: process.env.FINGERPRINT_EXPIRATION_DAYS * 24 * 60 * 60 * 1000, sameSite: "none" });
res.cookie("tokenrf", refreshToken, { httpOnly: true, secure: process.env.NODE_ENV === "production", maxAge: process.env.REFRESH_TOKEN_EXPIRATION_DAYS * 24 * 60 * 60 * 1000, sameSite: "none" });

res.status(200).json({message: "Logged in successfully", id: user._id.toString(), email: user.email, name: user.name});
}
10 changes: 4 additions & 6 deletions apps/api/src/routes/auth/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@ async function logout(req, res) {
expires: new Date(0),
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost"
sameSite: 'none',
path: '/'
})
res.cookie("securefp", "", {
maxAge: 0,
expires: new Date(0),
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
path: '/',
domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost"
sameSite: 'none',
path: '/'
})
return res.status(200).send("Logged out successfully");
}
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/routes/auth/refresh.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import { generateAuthToken, generateRefreshToken } from "../../services/tokens.j
httpOnly: true,
secure: process.env.NODE_ENV === "production",
maxAge: process.env.REFRESH_TOKEN_EXPIRATION_DAYS * 24 * 60 * 60 * 1000,
sameSite: "lax",
domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost" });
sameSite: "none" });
// res.cookie("securefp", fingerprint, {
// httpOnly: true,
// secure: process.env.NODE_ENV === "production",
Expand Down
15 changes: 10 additions & 5 deletions apps/api/src/routes/auth/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,22 @@ export default async function register(req, res) {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
maxAge: process.env.FINGERPRINT_EXPIRATION_DAYS * 24 * 60 * 60 * 1000,
sameSite: "lax",
domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost"
sameSite: "none"
});
res.cookie("tokenrf", refreshToken, {
httpOnly: true,
secure: process.env.NODE_ENV === "production",
maxAge: process.env.REFRESH_TOKEN_EXPIRATION_DAYS * 24 * 60 * 60 * 1000,
sameSite: "lax",
domain: process.env.NODE_ENV === "production" ? process.env.FRONTEND_URL : "localhost"
sameSite: "none"
});

res.status(201).json({ name: user.name, email: user.email, id: user._id.toString()});
res.status(201).json({
message: "User registered successfully",
user: {
id: user._id.toString(),
name: user.name,
email: user.email
}
});
};

23 changes: 19 additions & 4 deletions apps/api/src/routes/posts/delete.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import Content from "../../models/Content.js";
import { Idea } from "../../models/Idea.js";
import mongoose from "mongoose";

async function deletePost(req, res) {
try {
const { postId } = req.params;
const user = req.user;
const result = await Content.findOneAndUpdate(

// Try to delete from Content first
const contentResult = await Content.findOneAndUpdate(
{ "posts._id": postId, user: user },
{ $pull: { posts: { _id: postId } } },
{ new: true }
);

if (contentResult) {
return res.status(200).json({ message: "Post deleted successfully" });
}

// If not found in Content, try to delete from Ideas
const ideaResult = await Idea.findOneAndUpdate(
{ "posts._id": postId, user: user },
{ $pull: { posts: { _id: postId } } },
{ new: true }
);

if (!result) {
return res.status(404).json({ error: "Post not found" });
if (ideaResult) {
return res.status(200).json({ message: "Post deleted successfully" });
}

res.status(200).json({ message: "Post deleted successfully" });
// If not found in either collection
return res.status(404).json({ error: "Post not found" });
} catch (error) {
console.error("Error deleting post:", error);
res.status(500).json({ error: "Failed to delete post" });
Expand Down
74 changes: 51 additions & 23 deletions apps/api/src/routes/posts/get-posts.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
import mongoose from "mongoose";
import Content from "../../models/Content.js";
import { Idea } from "../../models/Idea.js";

async function getPosts(req, res) {
const page = parseInt(req.query.page) || 1;
const limit = parseInt(req.query.limit) || 10;
const skip = (page - 1) * limit;

try {
const countPipeline = [
// Get posts from Content sources
const contentPostsPipeline = [
{
$match: { user: mongoose.Types.ObjectId.createFromHexString(req.user) },
},
{ $unwind: "$posts" },
{ $count: "total" },
];

const countResult = await Content.aggregate(countPipeline);
const total = countResult.length > 0 ? countResult[0].total : 0;

if (total === 0) {
return res.json({
data: [],
pagination: {
total: 0,
page,
limit,
totalPages: 0,
{
$project: {
_id: "$posts._id",
title: "$posts.title",
description: "$posts.description",
platform: "$posts.platform",
tags: "$posts.tags",
length: "$posts.length",
customLength: "$posts.customLength",
tone: "$posts.tone",
createdAt: "$posts.createdAt",
sourceTitle: "$label",
sourceId: "$_id",
sourceType: "content",
},
});
}
},
];

const pipeline = [
// Get posts from Ideas
const ideaPostsPipeline = [
{
$match: { user: mongoose.Types.ObjectId.createFromHexString(req.user) },
},
{ $unwind: "$posts" },
{ $sort: { "posts.createdAt": -1 } },
{ $skip: skip },
{ $limit: limit },
{
$project: {
_id: "$posts._id",
Expand All @@ -49,13 +49,41 @@ async function getPosts(req, res) {
customLength: "$posts.customLength",
tone: "$posts.tone",
createdAt: "$posts.createdAt",
sourceTitle: "$label",
sourceTitle: "$title",
sourceId: "$_id",
sourceType: "idea",
},
},
];

const paginatedPosts = await Content.aggregate(pipeline);
// Execute both pipelines
const [contentPosts, ideaPosts] = await Promise.all([
Content.aggregate(contentPostsPipeline),
Idea.aggregate(ideaPostsPipeline),
]);

// Combine and sort all posts by creation date
const allPosts = [...contentPosts, ...ideaPosts].sort(
(a, b) => new Date(b.createdAt) - new Date(a.createdAt)
);

const total = allPosts.length;

if (total === 0) {
return res.json({
data: [],
pagination: {
total: 0,
page,
limit,
totalPages: 0,
},
});
}

// Apply pagination
const paginatedPosts = allPosts.slice(skip, skip + limit);

res.json({
data: paginatedPosts,
pagination: {
Expand Down
93 changes: 93 additions & 0 deletions apps/api/src/routes/posts/get-single-post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import mongoose from "mongoose";
import Content from "../../models/Content.js";
import { Idea } from "../../models/Idea.js";

const getSinglePost = async (req, res) => {
try {
const { postId } = req.params;

if (!postId) {
return res.status(400).json({
success: false,
message: "Post ID is required"
});
}

// Search for the post in Content sources
const contentPost = await Content.findOne(
{ "posts._id": mongoose.Types.ObjectId.createFromHexString(postId) },
{
"posts.$": 1,
"label": 1,
"_id": 1
}
);

if (contentPost && contentPost.posts.length > 0) {
const post = contentPost.posts[0];
return res.status(200).json({
success: true,
post: {
_id: post._id,
title: post.title,
description: post.description,
platform: post.platform,
tags: post.tags,
length: post.length,
customLength: post.customLength,
tone: post.tone,
createdAt: post.createdAt,
sourceTitle: contentPost.label,
sourceId: contentPost._id,
sourceType: "content",
}
});
}

// Search for the post in Ideas
const ideaPost = await Idea.findOne(
{ "posts._id": mongoose.Types.ObjectId.createFromHexString(postId) },
{
"posts.$": 1,
"title": 1,
"_id": 1
}
);

if (ideaPost && ideaPost.posts.length > 0) {
const post = ideaPost.posts[0];
return res.status(200).json({
success: true,
post: {
_id: post._id,
title: post.title,
description: post.description,
platform: post.platform,
tags: post.tags,
length: post.length,
customLength: post.customLength,
tone: post.tone,
createdAt: post.createdAt,
sourceTitle: ideaPost.title,
sourceId: ideaPost._id,
sourceType: "idea",
}
});
}

// Post not found in either collection
return res.status(404).json({
success: false,
message: "Post not found"
});

} catch (error) {
console.error("Error fetching post:", error);
res.status(500).json({
success: false,
message: "Failed to fetch post"
});
}
};

export default getSinglePost;
2 changes: 2 additions & 0 deletions apps/api/src/routes/posts/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from "express";
import getPosts from "./get-posts.js";
import getSinglePost from "./get-single-post.js";
import generate from "./generate.js";
import generateFromIdea from "./generate-from-idea.js";
import deletePost from "./delete.js";
Expand All @@ -8,6 +9,7 @@ import modify from "./modify.js";
const router = express.Router();

router.get("/", getPosts);
router.get("/:postId", getSinglePost);
router.post("/generate", generate);
router.post("/generate-from-idea", generateFromIdea);
router.delete("/:postId", deletePost);
Expand Down
Loading