1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579<a href="https://nextjs.org">
<h1 align="center">Onset Next.js Starter 2024</h1>
</a>
<p align="center">
An open source Next.js starter with step-by-step instructions if required.
</p>
<p align="center">
<a href="https://twitter.com/nrjdalal_com">
<img src="https://img.shields.io/twitter/follow/nrjdalal_com?style=flat&label=nrjdalal_com&logo=twitter&color=0bf&logoColor=fff" alt="Follow Neeraj on Twitter" />
</a>
</p>
<p align="center">
<a href="#features"><strong>Features</strong></a> ·
<a href="#step-by-step"><strong>Step by Step</strong></a> ·
<a href="#roadmap"><strong>Roadmap</strong></a> ·
<a href="#author"><strong>Author</strong></a> ·
<a href="#credits"><strong>Credits</strong></a>
</p>
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.
<!-- About: An open source Next.js bare starter with step-by-step instructions if required. Built with Next.js 14, Drizzle (Postgres), NextAuth/Auth.js. -->
<!-- Keywords: drizzle neondb nextauthjs nextjs postgres shadcn tailwindcss typescript vercel -->
## Features
### Frameworks
- [Next.js](https://nextjs.org/) – React framework for building performant apps with the best developer experience
- [Auth.js](https://authjs.dev/) – Handle user authentication with ease with providers like Google, Twitter, GitHub, etc.
- [Drizzle](https://orm.drizzle.team/) – Typescript-first ORM for Node.js
### Platforms
- [Vercel](https://vercel.com/) – Easily preview & deploy changes with git
- [Neon](https://neon.tech/) – The fully managed serverless Postgres with a generous free tier
## Automatic Setup
### Installation
Clone & create this repo locally with the following command:
> Note: You can use `npx` or `pnpx` as well
```bash
bunx create-next-app onset-starter --example "https://github.com/nrjdalal/onset"
```
1. Install dependencies using pnpm:
```sh
bun install
```
2. Copy `.env.example` to `.env.local` and update the variables.
```sh
cp .env.example .env.local
```
3. Run the database migrations:
```sh
bun db:push
```
3. Start the development server:
```sh
bun dev
```
## Step by Step
> Hint: Using `bun` instead of `npm`/`pnpm` and `bunx` instead of `npx`/`pnpx`. You can use the latter if you want.
### Phase 1 (Initialization)
#### 1. Initialize the project
Refs:
- [Installation](https://nextjs.org/docs/getting-started/installation)
```sh
bunx create-next-app . --ts --eslint --tailwind --src-dir --app --import-alias "@/*"
```
#### 2. Install `prettier` and supporting plugins
Refs:
- [prettier-plugin-sort-imports](https://github.com/IanVS/prettier-plugin-sort-imports)
- [prettier](https://prettier.io/)
- [prettier-plugin-tailwindcss](https://github.com/tailwindlabs/prettier-plugin-tailwindcss)
```sh
bun add -D @ianvs/prettier-plugin-sort-imports prettier prettier-plugin-tailwindcss
```
#### 3. Create `prettier.config.js`
```js
/** @type {import('prettier').Config} */
module.exports = {
semi: false,
singleQuote: true,
plugins: [
'@ianvs/prettier-plugin-sort-imports',
'prettier-plugin-tailwindcss',
],
}
```
#### 4. Create `src/lib/fonts.ts`
Refs:
- [Font Optimization](https://nextjs.org/docs/app/building-your-application/optimizing/fonts)
```ts
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',
})
```
#### 5. Install `clsx`, `tailwind-merge` and `nanoid`
Refs:
- [clsx](https://github.com/lukeed/clsx)
- [tailwind-merge](https://github.com/dcastil/tailwind-merge)
```sh
bun add clsx tailwind-merge nanoid
```
#### 6. Create `src/lib/utils.ts`
```ts
import { 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()
}
```
#### 7. Update `src/app/layout.tsx`
```ts
import './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>
)
}
```
### Phase 2 (Database)
#### 1. Install `drizzle` and supporting packages
Refs:
- [Drizzle Postgres](https://orm.drizzle.team/docs/quick-postgresql/postgresjs)
```sh
bun add drizzle-orm postgres
bun add -D drizzle-kit
```
#### 2. Create `src/lib/database.ts`
Refs:
- [Drizzle NextAuth Schema](https://authjs.dev/getting-started/adapters/drizzle)
```ts
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] }),
}),
)
```
#### 3. Create `drizzle.config.ts`
```ts
import type { Config } from 'drizzle-kit'
export default {
schema: './src/lib/database.ts',
dialect: 'postgresql',
dbCredentials: {
url: process.env.POSTGRES_URL as string,
},
} satisfies Config
```
#### 4. Copy `.env.local.example` to `.env.local`
> Hint: You can use [`pglaunch`](https://github.com/nrjdalal/pglaunch) to generate a postgres url
```env
POSTGRES_URL="**********"
```
#### 5. Update `package.json`
```json
{
// ...
"scripts": {
// ...
"db:push": "bun --env-file=.env.local drizzle-kit push",
"db:studio": "bun --env-file=.env.local drizzle-kit studio"
}
// ...
}
```
##### 6. Run `db:push` to create tables
```sh
bun db:push
```
### Phase 3 (Authentication)
#### 1. Install `next-auth`
```sh
bun add next-auth@beta @auth/drizzle-adapter
```
#### 2. Update `.env.local`
```env
# ...
AUTH_SECRET="**********"
AUTH_GITHUB_ID="**********"
AUTH_GITHUB_SECRET="**********"
```
3. Create `src/lib/auth.ts`
```ts
import { 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
}
}
}
```
#### 3. Create `src/app/api/auth/[...nextauth]/route.ts`
```ts
import { handlers } from '@/lib/auth'
export const { GET, POST } = handlers
```
#### 4. Create `src/middleware.ts` - not supported yet
```ts
import { 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*'],
}
```
#### 5. Create `src/app/(auth)/access/page.tsx`
```tsx
import { 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
```
#### 6. Create `src/app/(admin)/dashboard/page.tsx`
```tsx
import { 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
```
## Roadmap
- [ ] Light and dark mode
- [ ] To added fine-grained instructions
- [ ] More features and points to be added
## Author
Created by [@nrjdalal](https://twitter.com/nrjdalal_com) in 2023, released under the [MIT license](https://github.com/nrjdalal/onset/blob/main/LICENSE.md).
## Credits
This project is inspired by [@shadcn](https://twitter.com/shadcn)'s [Taxonomy](https://github.com/shadcn-ui/taxonomy).