Skip to content

TianMeds/Setup-Fullstack-Project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

Full Stack Next.js Application Setup

This guide will walk you through setting up a full-stack Next.js application with Node.js, Visual Studio Code, Postman for testing, Shadcn/UI for components, and Clerk for authentication.

Prerequisites

Before you begin, ensure you have the following installed on your machine:

Steps

1. Create a New Next.js Project

  1. Create a folder in your desired location.

  2. Open a terminal in the folder and run the following command:

    npx create-next-app@latest
  3. You will be prompted with the following options. For this guide, select "yes" for Tailwind CSS and App Router.

    What is your project named? my-app
    Would you like to use TypeScript? No
    Would you like to use ESLint? Yes
    Would you like to use Tailwind CSS? Yes
    Would you like to use `src/` directory? No
    Would you like to use App Router? (recommended) Yes
    Would you like to customize the default import alias (@/*)? No
    

2. Open the Project in Visual Studio Code

  1. Open Visual Studio Code.
  2. Open the folder you created and navigate to app/page.js.
  3. Replace the default HTML code with your desired content.

3. Run the Development Server

  1. In the terminal, run:

    npm run dev
  2. Your application will be available at http://localhost:3000.

4. Install Shadcn/UI for Components

  1. Run the following command to install Shadcn/UI:

    npx shadcn-ui@latest init
  2. Select the following options:

    Which style would you like to use? › Default
    Which color would you like to use as base color? › Neutral
    Do you want to use CSS variables for colors? › Yes
    
  3. Refer to the Shadcn/UI documentation for further details on using components.

5. Set Up Authentication with Clerk

  1. Sign up at Clerk.

  2. Create a new application in the Clerk dashboard and configure social media login options.

  3. Install Clerk in your project:

    npm install @clerk/nextjs
  4. Copy the .env variables provided by Clerk and create a .env.local file in your project.

  5. Create a middleware.js file in your root folder and add the following code:

    import {
      clerkMiddleware,
      createRouteMatcher
    } from '@clerk/nextjs/server';
    
    const isProtectedRoute = createRouteMatcher([
      '/dashboard(.*)',
      '/forum(.*)',
    ]);
    
    export default clerkMiddleware((auth, req) => {
      if (isProtectedRoute(req)) auth().protect();
    });
    
    export const config = {
      matcher: ['/((?!.*\\..*|_next).*)', '/', '/(api|trpc)(.*)'],
    };
  6. Update layout.js to include ClerkProvider:

    import { ClerkProvider } from "@clerk/nextjs";
    
    <ClerkProvider>
      <html lang="en">
        <body className={inter.className}>{children}</body>
      </html>
    </ClerkProvider>
  7. Protect routes by following the Clerk middleware documentation.

6. Create Protected Routes

  1. Create a dashboard folder in the app directory with layout.jsx and page.jsx:

    layout.jsx

    import React from 'react';
    
    const DashboardLayout = ({ children }) => {
      return <div>{children}</div>;
    };
    
    export default DashboardLayout;

    page.jsx

    import React from 'react';
    
    const Dashboard = () => {
      return <div>Dashboard</div>;
    };
    
    export default Dashboard;
  2. Test the route by navigating to http://localhost:3000/dashboard. It should redirect to the sign-up page if not logged in.

7. Customize Sign-In and Sign-Up Pages

  1. Create a folder named auth inside the app directory.

  2. Create sign-up/[[...sign-up]]/page.jsx with the following code:

    import { SignUp } from "@clerk/nextjs";
    
    export default function Page() {
      return <SignUp />;
    }
  3. Create sign-in/[[...sign-in]]/page.jsx with the following code:

    import { SignIn } from "@clerk/nextjs";
    
    export default function Page() {
      return <SignIn />;
    }
  4. Update .env.local:

    NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
    NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up
  5. Restart the development server:

    npm run dev

Now your application should have authentication with Clerk and be running on http://localhost:3000.

Useful Links

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published