Skip to content

Commit

Permalink
added some minor things
Browse files Browse the repository at this point in the history
  • Loading branch information
Asif-Alam1 committed Jan 3, 2024
1 parent 29d79a7 commit 9a4c2b3
Show file tree
Hide file tree
Showing 5 changed files with 756 additions and 1,740 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
78 changes: 39 additions & 39 deletions app/api/auth/[...nextauth]/route.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";

import User from '@models/user';
import { connectToDB } from '@utils/database';
import User from "@models/user";
import { connectToDB } from "@utils/database";

const handler = NextAuth({
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
})
],
callbacks: {
async session({ session }) {
// store the user id from MongoDB to session
const sessionUser = await User.findOne({ email: session.user.email });
session.user.id = sessionUser._id.toString();
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
],
callbacks: {
async session({ session }) {
// store the user id from MongoDB to session
const sessionUser = await User.findOne({ email: session.user.email });
session.user.id = sessionUser._id.toString();

return session;
},
async signIn({ account, profile, user, credentials }) {
try {
await connectToDB();
return session;
},
async signIn({ account, profile, user, credentials }) {
try {
await connectToDB();

// check if user already exists
const userExists = await User.findOne({ email: profile.email });
// check if user already exists
const userExists = await User.findOne({ email: profile.email });

// if not, create a new document and save user in MongoDB
if (!userExists) {
await User.create({
email: profile.email,
username: profile.name.replace(" ", "").toLowerCase(),
image: profile.picture,
});
}
// if not, create a new document and save user in MongoDB
if (!userExists) {
await User.create({
email: profile.email,
username: profile.name.replace(" ", "").toLowerCase(),
image: profile.picture,
});
}

return true
} catch (error) {
console.log("Error checking if user exists: ", error.message);
return false
}
},
}
})
return true;
} catch (error) {
console.log("Error checking if user exists: ", error.message);
return false;
}
},
},
});

export { handler as GET, handler as POST }
export { handler as GET, handler as POST };
98 changes: 49 additions & 49 deletions app/profile/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,55 @@ import { useRouter } from "next/navigation";
import Profile from "@components/Profile";

const MyProfile = () => {
const router = useRouter();
const { data: session } = useSession();

const [myPosts, setMyPosts] = useState([]);

useEffect(() => {
const fetchPosts = async () => {
const response = await fetch(`/api/users/${session?.user.id}/posts`);
const data = await response.json();

setMyPosts(data);
};

if (session?.user.id) fetchPosts();
}, [session?.user.id]);

const handleEdit = (post) => {
router.push(`/update-prompt?id=${post._id}`);
};

const handleDelete = async (post) => {
const hasConfirmed = confirm(
"Are you sure you want to delete this prompt?"
);

if (hasConfirmed) {
try {
await fetch(`/api/prompt/${post._id.toString()}`, {
method: "DELETE",
});

const filteredPosts = myPosts.filter((item) => item._id !== post._id);

setMyPosts(filteredPosts);
} catch (error) {
console.log(error);
}
}
};

return (
<Profile
name='My'
desc='Welcome to your personalized profile page. Share your exceptional prompts and inspire others with the power of your imagination'
data={myPosts}
handleEdit={handleEdit}
handleDelete={handleDelete}
/>
);
const router = useRouter();
const { data: session } = useSession();

const [myPosts, setMyPosts] = useState([]);

useEffect(() => {
const fetchPosts = async () => {
const response = await fetch(`/api/users/${session?.user.id}/posts`);
const data = await response.json();

setMyPosts(data);
};

if (session?.user.id) fetchPosts();
}, [session?.user.id]);

const handleEdit = (post) => {
router.push(`/update-prompt?id=${post._id}`);
};

const handleDelete = async (post) => {
const hasConfirmed = confirm(
"Are you sure you want to delete this prompt?"
);

if (hasConfirmed) {
try {
await fetch(`/api/prompt/${post._id.toString()}`, {
method: "DELETE",
});

const filteredPosts = myPosts.filter((item) => item._id !== post._id);

setMyPosts(filteredPosts);
} catch (error) {
console.log(error);
}
}
};

return (
<Profile
name="My"
desc="Welcome to your personalized profile page. Share your exceptional prompts and inspire others with the power of your imagination"
data={myPosts}
handleEdit={handleEdit}
handleDelete={handleDelete}
/>
);
};

export default MyProfile;
32 changes: 16 additions & 16 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
appDir: true,
serverComponentsExternalPackages: ["mongoose"],
},
images: {
domains: ['lh3.googleusercontent.com'],
},
webpack(config) {
config.experiments = {
...config.experiments,
topLevelAwait: true,
}
return config
}
}
experimental: {
appDir: true,
serverComponentsExternalPackages: ["mongoose"],
},
images: {
domains: ["lh3.googleusercontent.com"],
},
webpack(config) {
config.experiments = {
...config.experiments,
topLevelAwait: true,
};
return config;
},
};

module.exports = nextConfig
module.exports = nextConfig;
Loading

0 comments on commit 9a4c2b3

Please sign in to comment.