We start by setting up the home page and creating the navigation bar for our website. The home component is wrapped in an HTML5 semantic <section>
tag with Tailwind CSS classes for styling. The navigation bar contains links, buttons, and icons.
- The main content of the home page is wrapped in a
<section>
tag with Tailwind CSS classes for layout and styling. - The heading text "Discover and Share" is styled using the
head_text
class from theglobals.css
stylesheet. - A
<span>
element is used to display the tagline "AI powered prompts." - The body text "Primetopia is an open source AI prompting tool..." is styled using the
desc
class fromglobals.css
.
- The navigation bar is created as a horizontal layout with links and buttons.
- The Primetopia logo and site name are displayed using the
Image
component from Next.js with a source ofassets/images/logo.svg
. - Links for creating prompts and viewing the user profile are included.
- For authentication, Google Sign-In button is provided (will be implemented using NextAuth).
- On smaller devices, the navigation bar collapses into a mobile-friendly version.
- The profile icon is displayed using the same logo image for now.
- Clicking the icon opens a dropdown menu with links for user profile, creating prompts, and signing out.
- The
Layout
component is used to wrap the entire website content. - The
Provider
component from NextAuth is used for user authentication. - This ensures authentication functionality is available across the entire app.
This documentation provides a summary of the tutorial transcript that covers the process of building a web application with Google OAuth authentication, MongoDB database integration, user models, and sign-in functionality.
- Create a project on the Google Cloud Platform.
- Enable the Google+ API for the project.
- Create OAuth 2.0 credentials for a web application.
- Configure OAuth consent screen settings, including app name, email, authorized domains, logo, and developer email.
- Generate OAuth client ID and client secret, storing them in the
.env
file. - Implement environmental variable retrieval in the code using
process.env
.
- Create a
database.js
file in theutils
directory. - Import Mongoose and set up options to handle connections.
- Define an
isConnected
variable to track connection status. - Export an asynchronous
connectToDB
function that establishes the database connection. - Use the MongoDB Atlas service to create a new cluster.
- Obtain the MongoDB URI and update the
.env
file. - Import the
connectToDB
function in the main route file. - Ensure the connection is established before proceeding.
- Establish a connection to the MongoDB Atlas database using Mongoose.
- Define a user schema with fields like email, username, and image.
- Create an asynchronous function to retrieve the user's session based on their email.
- Update the user's session with their ID to track the current user.
- Create a MongoDB Atlas cluster via the MongoDB website.
- Configure database access by setting up network access and a user password.
- Retrieve the MongoDB URI and save it as an environment variable in
.env
.
- Create a
user.js
file in themodels
directory. - Import Mongoose functions for schema creation.
- Define a user schema with fields like email, username, and image.
- Set validation rules for email uniqueness, required fields, and username format.
- Utilize a registered model if available, else create a new one.
- Import the user model in the main route file.
- Implement the
signIn
function using a try-catch block. - Use
user.findOne
to check if a user with the provided email already exists. - If the user does not exist, create a new user using
user.create
. - Extract user data from the Google OAuth profile, including email, username, and image.
- Handle errors during sign-in and user creation.
- To maintain an active user session, create a
sessionUser
variable. - Retrieve user data based on the email stored in the session.
- Update the user's ID in the session to associate the user with their session.
- Return the updated session object.
- Configure callback URLs for OAuth providers by adding URIs to the authorized redirect URIs in the Google Cloud console.
- Create an appropriate
next-auth.local.js
configuration file to specify environment variables such asNEXTAUTH_URL
,NEXTAUTH_URL_INTERNAL
, andNEXTAUTH_SECRET
. - Generate a random
NEXTAUTH_SECRET
using the OpenSSL terminal command.
- Utilize the
session.user.image
property to display the user's profile icon. - Replace the mock logo with the actual user profile icon.
- Update both mobile and desktop navigation bars to include the profile icon.
This tutorial has guided you through the process of setting up user sessions, implementing authentication, and displaying user profile information in your web application. Refer to the full tutorial for detailed explanations and code samples.
For more in-depth insights and to learn about error handling, deployment, and additional features, explore the NextAuth.js documentation and resources.
With user authentication and session management now established, as well as MongoDB integration in place, the application is poised for further development. The "Create Post" page will be the next focus, allowing users to share prompts.
Continuing to build on this foundation will help you develop a complete web application. Refer to the full tutorial for detailed explanations and code samples.
For advanced features, deployment, and enhancing the user experience, explore additional resources and tutorials available online.