An open source Next.js bare starter with step-by-step instructions if required. Built with Next.js 14, Drizzle (Postgres), NextAuth/Auth.js.
https://github.com/nrjdalal/onset.git
An open source Next.js starter with step-by-step instructions if required.
Features ยท Step by Step ยท Roadmap ยท Author ยท Credits
Onset is a Next.js starter that comes with step-by-step instructions to understand how everything works, easy for both beginners and experts alike and giving you the confidence to customize it to your needs. Built with Next.js 14, Drizzle (Postgres), NextAuth/Auth.js.
Clone & create this repo locally with the following command:
Note: You can usenpxorpnpxas well
bunx create-next-app onset-starter --example "https://github.com/nrjdalal/onset"
bun install
.env.example to .env.local and update the variables.cp .env.example .env.local
bun db:push
bun dev
Hint: Usingbuninstead ofnpm/pnpmandbunxinstead ofnpx/pnpx. You can use the latter if you want.
Refs:
bunx create-next-app . --ts --eslint --tailwind --src-dir --app --import-alias "@/*"
prettier and supporting pluginsRefs:
bun add -D @ianvs/prettier-plugin-sort-imports prettier prettier-plugin-tailwindcss
prettier.config.js/** @type {import('prettier').Config} */
module.exports = {
semi: false,
singleQuote: true,
plugins: [
'@ianvs/prettier-plugin-sort-imports',
'prettier-plugin-tailwindcss',
],
}
src/lib/fonts.tsRefs:
import {
JetBrains_Mono as FontMono,
DM_Sans as FontSans,
} from 'next/font/google'
export const fontMono = FontMono({
subsets: ['latin'],
variable: '--font-mono',
})
export const fontSans = FontSans({
subsets: ['latin'],
variable: '--font-sans',
})
clsx, tailwind-merge and nanoidRefs:
bun add clsx tailwind-merge nanoid
src/lib/utils.tsimport { clsx, type ClassValue } from 'clsx'
import { customAlphabet } from 'nanoid'
import { twMerge } from 'tailwind-merge'
export const cn = (...inputs: ClassValue[]) => {
return twMerge(clsx(inputs))
}
export function generateId(
{
chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
length = 12,
}: {
chars: string
length: number
} = {
chars: '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
length: 12,
},
) {
const nanoid = customAlphabet(chars, length)
return nanoid()
}
src/app/layout.tsximport './globals.css'
import { fontMono, fontSans } from '@/lib/fonts'
import { cn } from '@/lib/utils'
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: 'Onset',
description: 'The only Next.js starter you need',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body
className={cn(
'min-h-dvh font-sans antialiased',
fontMono.variable,
fontSans.variable,
)}
>
{children}
</body>
</html>
)
}
drizzle and supporting packagesRefs:
bun add drizzle-orm postgres
bun add -D drizzle-kit
src/lib/database.tsRefs:
import {
integer,
pgTable,
primaryKey,
text,
timestamp,
} from 'drizzle-orm/pg-core'
import { drizzle, PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
const queryClient = postgres(process.env.POSTGRES_URL as string)
export const db: PostgresJsDatabase = drizzle(queryClient)
export const users = pgTable('user', {
id: text('id')
.primaryKey()
.$defaultFn(() => crypto.randomUUID()),
publicId: text('publicId').unique().notNull(),
name: text('name'),
email: text('email').notNull(),
emailVerified: timestamp('emailVerified', { mode: 'date' }),
image: text('image'),
})
export const accounts = pgTable(
'account',
{
userId: text('userId')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
type: text('type').notNull(),
provider: text('provider').notNull(),
providerAccountId: text('providerAccountId').notNull(),
refresh_token: text('refresh_token'),
access_token: text('access_token'),
expires_at: integer('expires_at'),
token_type: text('token_type'),
scope: text('scope'),
id_token: text('id_token'),
session_state: text('session_state'),
},
(account) => ({
compoundKey: primaryKey({
columns: [account.provider, account.providerAccountId],
}),
}),
)
export const sessions = pgTable('session', {
id: text('id').notNull(),
sessionToken: text('sessionToken').primaryKey(),
userId: text('userId')
.notNull()
.references(() => users.id, { onDelete: 'cascade' }),
expires: timestamp('expires', { mode: 'date' }).notNull(),
})
export const verificationTokens = pgTable(
'verificationToken',
{
identifier: text('identifier').notNull(),
token: text('token').notNull(),
expires: timestamp('expires', { mode: 'date' }).notNull(),
},
(vt) => ({
compoundKey: primaryKey({ columns: [vt.identifier, vt.token] }),
}),
)
drizzle.config.tsimport type { Config } from 'drizzle-kit'
export default {
schema: './src/lib/database.ts',
dialect: 'postgresql',
dbCredentials: {
url: process.env.POSTGRES_URL as string,
},
} satisfies Config
.env.local.example to .env.localHint: You can use pglaunch to generate a postgres url
POSTGRES_URL="**********"
package.json{
// ...
"scripts": {
// ...
"db:push": "bun --env-file=.env.local drizzle-kit push",
"db:studio": "bun --env-file=.env.local drizzle-kit studio"
}
// ...
}
db:push to create tablesbun db:push
next-authbun add next-auth@beta @auth/drizzle-adapter
.env.local# ...
AUTH_SECRET="**********"
AUTH_GITHUB_ID="**********"
AUTH_GITHUB_SECRET="**********"
src/lib/auth.tsimport { db, users } from '@/lib/database'
import { generateId } from '@/lib/utils'
import { DrizzleAdapter } from '@auth/drizzle-adapter'
import { eq } from 'drizzle-orm'
import NextAuth from 'next-auth'
import GitHub from 'next-auth/providers/github'
export const { handlers, signIn, signOut, auth } = NextAuth({
adapter: {
...DrizzleAdapter(db),
async createUser(user) {
return await db
.insert(users)
.values({
...user,
publicId: generateId(),
})
.returning()
.then((res) => res[0])
},
},
providers: [GitHub],
session: {
strategy: 'jwt',
},
callbacks: {
async session({ session, token }) {
if (token) {
session.user.id = token.id as string
session.user.publicId = token.publicId as string
session.user.name = token.name as string
session.user.email = token.email as string
session.user.image = token.image as string
}
return session
},
async jwt({ token, user }) {
const [result] = await db
.select()
.from(users)
.where(eq(users.email, token.email as string))
.limit(1)
if (!result) {
if (user) {
token.id = user.id
}
return token
}
return {
id: result.id,
publicId: result.publicId,
name: result.name,
email: result.email,
image: result.image,
}
},
},
})
declare module 'next-auth' {
interface Session {
user: {
id: string
publicId: string
name: string
email: string
image: string
}
}
}
src/app/api/auth/[...nextauth]/route.tsimport { handlers } from '@/lib/auth'
export const { GET, POST } = handlers
src/middleware.ts - not supported yetimport { getToken } from 'next-auth/jwt'
import { withAuth } from 'next-auth/middleware'
import { NextResponse } from 'next/server'
export default withAuth(
async function middleware(req) {
const token = await getToken({ req })
const isAuth = !!token
const isAuthPage = req.nextUrl.pathname.startsWith('/access')
if (isAuthPage) {
if (isAuth) {
return NextResponse.redirect(new URL('/dashboard', req.url))
}
return null
}
if (!isAuth) {
let from = req.nextUrl.pathname
if (req.nextUrl.search) {
from += req.nextUrl.search
}
return NextResponse.redirect(
new URL(`/access?from=${encodeURIComponent(from)}`, req.url),
)
}
},
{
callbacks: {
async authorized() {
return true
},
},
},
)
export const config = {
matcher: ['/access', '/dashboard/:path*'],
}
src/app/(auth)/access/page.tsximport { auth, signIn } from '@/lib/auth'
import { redirect } from 'next/navigation'
const Page = async () => {
const session = await auth()
if (session) return redirect('/dashboard')
return (
<div className="flex min-h-[100dvh] flex-col items-center justify-center gap-8">
<form
action={async () => {
'use server'
await signIn('github')
}}
>
<button className="rounded-md border px-8 py-2.5" type="submit">
Signin with GitHub
</button>
</form>
</div>
)
}
export default Page
src/app/(admin)/dashboard/page.tsximport { auth, signOut } from '@/lib/auth'
import { redirect } from 'next/navigation'
const Page = async () => {
const session = await auth()
if (!session) return redirect('/access')
return (
<div className="flex min-h-[100dvh] flex-col items-center justify-center gap-8">
<div className="text-center">
{Object.entries(session.user).map(([key, value]) => (
<p key={key}>
<span className="font-bold">{key}</span>: {value}
</p>
))}
</div>
<form
action={async () => {
'use server'
await signOut()
}}
>
<button className="rounded-md border px-8 py-2" type="submit">
Sign Out
</button>
</form>
</div>
)
}
export default Page
Created by @nrjdalal in 2023, released under the MIT license.