This project is used as my starting point for most of my web application personal projects.
- Create a new repository for your new project. Do not create it with a README or any other files. Just name it and create it.
- Select the
Import Code
option. - Paste in the GitHub url for this repo:
https://github.com/EthanHogan/UserPlatform.git
- Click
Begin Import
.
- Next.js (React Framework)
- Tailwind CSS
- Drizzle ORM
- tRPC (TS-based framework for building typesafe APIs)
- PlanetScale (Serverless MySQL DB)
- Clerk (Auth/User Management)
- Vercel (CI/CD, Hosting for Serverless Apps)
- Axiom (Logging)
- Upstash (Rate Limiter)
- Run:
npm install
Next.js
,Tailwind
,Drizzle
,tRPC
: These are already set up with the project.- Ensure the project is in your GitHub.
- PlanetScale:
- Sign in to PlanetScale.
- Click
Create a New Database
. - Select the region for your DB, ideally close to where your
Vercel
functions will be deployed. - Name your DB (e.g., "userplatformdb").
- Click the
Connect
button. - Change the
Connect with:
value to "Prisma" (at the time of writing this, "Drizzle" is not an option but this should still work with the "Prisma" option selected). - Copy the
DATABASE_URL
environment variable and paste it into your project's.env
file. - In your terminal, run
npm run dbpush
to set the database schema based on the current schema in thedrizzle/schema.ts
file. - Run
npx drizzle-kit studio
in the terminal to view your database tables and data in the browser.
- Clerk:
- Navigate to Clerk and sign in.
- Add an application (you may need to create a workspace first, e.g., "Personal").
- Set the application name (e.g., "userplatform").
- Select Identifiers, Auth strategy, and Social Connections as desired.
- Click
Create Application
. - Navigate to
API Keys
on Clerk to get theNEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
andCLERK_SECRET_KEY
. Copy and paste both into your project's.env
file. - If you want to require users to have a username, in the Clerk side bar under "User & Authentication", select "Email, Phone, Username" and toggle on
Username
.
- Note: some social connections will pass the username to the webhook without this toggled on, but for the ones that don't, when this is turned on, an additional popup will come up for the user, requiring them to enter a unique username. Or they made be required to enter a unique username if the username given by the social connection conflicts with an existing users username.
- Upstash:
- Navigate to Upstash and sign in.
- Click
Console
button to get the console or go to console here. - Click
Create Database
. - Name the database (e.g., "userplatform-ratelimiter")
- Select type
Regional
. - Select the Region closest to your DB and server (e.g., "US-EAST-1").
- Click
Create
. - Once done creating, on the
Details
tab (should be the default tab), scroll down under theREST API
section, you should see options to access your Upstash database (cURL, JavaScript (Fetch), @upstash/redis, .env). - Select
.env
- Copy both the
UPSTASH_REDIS_REST_URL
andUPSTASH_REDIS_REST_TOKEN
. - Paste both of the environment variables into your
.env
.
- Vercel:
- Create a new project in
Vercel
at https://vercel.com/new. - Import your project from GitHub by finding it in the list and clicking "import."
- Add the
DATABASE_URL
,NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
,CLERK_SECRET_KEY
,UPSTASH_REDIS_REST_URL
andUPSTASH_REDIS_REST_TOKEN
environment variables from your.env
file to theEnvironment Variables
section. - Select
Production
,Preview
, andDevelopment
environments (you can do this later after deployment at https://vercel.com/MyGitHubUsername/MyProjectName/settings/environment-variables if needed). - Click
Deploy
. - Set the function region to the region closest to your DB deployment at https://vercel.com/MyGitHubUsername/MyProjectName/settings/functions.
- Create a new project in
- Axiom:
- Create an
Axiom
account with your GitHub at https://app.axiom.co. - Navigate to Vercel Integrations at https://vercel.com/dashboard/integrations.
- Click
Browse Marketplace
. - Search "Axiom" and add it.
- Select the
Vercel
account you want to add the integration to, then clickContinue
. - Choose specific projects or all
Vercel
projects, then clickContinue
. - Click
Add Integration
. - Click
Connect to Vercel
inAxiom
modal.
- Create an
npm run dev
npm run dbpush
Deployment should be as easy as pushing changes to main
or master
branch if Vercel and PlanetScale are setup correctly.
git push
-
Ngrok: You will need Ngrok to locally test your webhook endpoints and their ability to receive/handle webhook requests. (Clerk Webhook Integrations w/ Ngrok for testing locally)[https://ngrok.com/docs/integrations/clerk/webhooks/]
- Open ngrok terminal by running the .exe once you have ngrok for windows installed.
- Run ngrok on the same port you have your project running on.
ngrok http 3000
- Copy the "Forwarding" url from the terminal to setup an endpoint with Clerk (e.g. https://1234-56-789-012-345.ngrok-free.app
-
Clerk:
- Navigate to https://dashboard.clerk.com and sign in.
- Select your application.
- Select "Webhooks".
- Select "Add Endpoint".
- Paste the "Forwarding" url from the ngrok terminal in the "Endpoint URL" field.
- Append your route of your webhook to the end of the "Endpoint URL" (e.g. /api/clerk-user-webhook)
- Select which events you would like handle in your webhook endpoint (e.g. "user.created", "user.deleted", and "user.updated").
- Select "Create".
- Copy the "Signing Secret" in the bottom right of the "Endpoints" tab.
- Paste this secret as the value for your "CLERK_USER_EVENT_WEBHOOK_SECRET" environment variable in you
.env
file.
- Note: This environment variable is endpoint specific. So for your production endpoint, you will have a different "Signing Secret" that will need to be added to your environment variables in Vercel Settings (example url of how to get there: https://vercel.com/MyGitHubUsername/MyProjectName/settings/environment-variables).
- Select "Testing" tab of your Clerk endpoint, then select a supported event type to test your webhook. Click "Send Example" to test your webhook event handler.
- Create a new endpoint in Clerk ("Add Endpoint" on "Webhooks" page) with the same settings, except change the endpoint url to your production endpoint.
Vercel User Webhook Project:
- Navigate to https://vercel.com/ethanhogan/user-webhook/settings/environment-variables. Sign in to Vercel first if needed.
- Add about 8 environment variables for your app, using the naming convention for the variables. Variable names begin with "App_" and end with the name for the variable and contain the app name between the 2. For example, if I was adding a DATABASE_URL variable for the UserPlatform application, I would add it as "App_UserPlatform_DATABASE_URL".
- Make sure to include an "_AppId" variable and give it the name of your application. You will use this name when setting up the endpoint with Clerk.
Clerk:
-
Navigate to https://dashboard.clerk.com and sign in.
-
Select your application.
-
Select "Webhooks".
-
Select "Add Endpoint".
-
Use "https://user-webhook.vercel.app/api/clerk-user-webhook?AppId=UserPlatform" url, changing out "UserPlatform" for the name of your application. This must match the "_AppId" variable added to the Vercel User Webhook project.
-
Select which events you would like handle in your webhook endpoint (e.g. "user.created", "user.deleted", and "user.updated").
-
Select "Create".
-
Copy the "Signing Secret" in the bottom right of the "Endpoints" tab.
-
Paste this secret as the value for your "App_UserPlatform_CLERK_USER_EVENT_WEBHOOK_SECRET" environment variable in the Vercel User Platform application, replacing "UserPlatform" with the AppId of your application.
- Note: This environment variable is endpoint specific. So for your production endpoint, you will have a different "Signing Secret" that will need to be added to your environment variables in Vercel Settings (example url of how to get there: https://vercel.com/MyGitHubUsername/MyProjectName/settings/environment-variables).
- Select "Testing" tab of your Clerk endpoint, then select a supported event type to test your webhook. Click "Send Example" to test your webhook event handler.
- Create a new endpoint in Clerk ("Add Endpoint" on "Webhooks" page) with the same settings, except change the endpoint url to your production endpoint.
- Docker:
- Spin up a container with an image that has mysql on it. If you run the command below, it will automatically install the latest version of the mysql image and startup a container.
- Swap the
user-platform-mysql
for whatever you want to call the container. - Swap the
3333
for whatever port number you want to expose the container on. - Swap
myPassword
with your password to the db.
docker run --name user-platform-mysql -p 3333:3306 -e MYSQL_ROOT_PASSWORD=myPassword -d mysql
- MySQL:
- Use MySQL Workbench to create your database schema. Note the name. For this example, lets say we name the schema
UserPlatform
.
- Use MySQL Workbench to create your database schema. Note the name. For this example, lets say we name the schema
- Drizzle:
- Update the
DATABASE_URL
line in the.env
to the following format:
root
andmyPassword
are your DB credentials. You would have used them to connect to the DB in MySQL Workbench.- Make sure the port number (
3333
in this example) matches the port number you exposed the Docker container on. - The last thing after the slash is the name of the DB schema to connect to.
UserPlatform
, in this example.
DATABASE_URL='mysql://root:myPassword@localhost:3333/UserPlatform'
- Update the other
DATABASE_...
variables with the DB info from the URL. Check.env.example
for examples
- Update the