Coder Social home page Coder Social logo

georgwittberger / t3-commerce-template Goto Github PK

View Code? Open in Web Editor NEW
8.0 3.0 1.0 206 KB

Headless commerce template based on T3 stack

License: MIT License

JavaScript 14.23% TypeScript 85.66% CSS 0.10%
headless-commerce medusajs next-auth nextjs project-template t3-stack trpc example-project

t3-commerce-template's Introduction

T3 Commerce Template

This is a project template for headless commerce using T3 stack for the storefront and Medusa.js for the backend.

Getting Started

Required Software

  • Node.js version 18.x (or higher)
  • pnpm version 8.x (or higher)

General Preparation

  1. Clone this repository.
  2. Open terminal in local repository.
  3. Install NPM dependencies.
pnpm install

Setting Up Medusa.js Backend

  1. Open terminal in apps/backend folder.

  2. Run seed script

    pnpm seed
  3. Start server

    pnpm start
  4. Create test customer

    curl --request POST \
      --url http://localhost:9000/store/customers \
      --header 'Accept: application/json' \
      --header 'Content-Type: application/json' \
      --data '{"first_name":"John","last_name":"Doe","email":"[email protected]","password":"supersecret","phone":"+49 123 456789"}'

Hint: You can access Medusa.js Admin via http://localhost:9000/app and sign in with email "[email protected]" and password "supersecret".

Setting Up Next.js Storefront

  1. Open terminal in apps/storefront folder.

  2. Create .env file from template.

    cp .env.example .env
  3. Generate secret key for Next-Auth.

    openssl rand -base64 32
  4. Open .env file and insert variable with secret key.

    # .env
    NEXTAUTH_SECRET="your_secret_key"
  5. Start server

    pnpm dev

Browse the Storefront

  1. Visit http://localhost:3000/
  2. Sign in with test customer:

Fundamental Concepts

Storefront Based on Create T3 App

The storefront app has been set up using Create T3 App. See its documentation for more details.

Authentication Using Medusa.js

The storefront uses Next-Auth as authentication library. A custom provider is implemented to support sign in via Medusa.js API. See src/integrations/medusa/auth-provider.ts

Medusa.js session ID is stored inside the JWT managed by Next-Auth and is not exposed as part of client session data (e.g. useSession). Therefore, it can only be access on the server-side, i.e. in getServerSideProps or in API routes (including tRPC procedures). As a consequence, all queries and mutations requiring user authentication must be proxied to Medusa.js via tRPC procedures. See auth router for example: src/features/auth/server/api/auth-router.ts

Internationalization Using i18next

The storefront supports English and German. Internationalization is implemented using next-i18next. Language files with translations are located in public/locales folder.

Keep in mind that data coming from Medusa.js is not localized because there is no multi-language support yet.

Styling Using daisyUI

The storefront has some basic styling using TailwindCSS and daisyUI.

Modularization and Abstraction

To keep things simple, there are only two guidelines for modularization and abstraction in the storefront project.

  1. Feature-specific code belongs into the corresponding folder inside src/features (e.g. React components dedicated to the catalog in src/features/catalog/components) while shared code goes into the top-level directories (e.g. general React components in src/components)
  2. Communication with vendor-specific backend APIs is performed via service functions which create a backend-agnostic facade. These functions build the bridge between the business domain model used in React components and the underlying vendor-specific APIs (like Medusa.js model). See cart services for example: src/features/cart/services

License

MIT

t3-commerce-template's People

Contributors

georgwittberger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ringge

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.