-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
121 changed files
with
21,184 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Get your OpenAI API Key here: https://platform.openai.com/account/api-keys | ||
OPENAI_API_KEY=**** | ||
|
||
# Generate a random secret: https://generate-secret.vercel.app/32 or `openssl rand -base64 32` | ||
AUTH_SECRET=**** | ||
|
||
# The following keys below are automatically created and | ||
# added to your environment when you deploy on vercel | ||
|
||
# Instructions to create a Vercel Blob Store here: https://vercel.com/docs/storage/vercel-blob | ||
BLOB_READ_WRITE_TOKEN=**** | ||
|
||
# Instructions to create a database here: https://vercel.com/docs/storage/vercel-postgres/quickstart | ||
POSTGRES_URL=**** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"extends": [ | ||
"next/core-web-vitals", | ||
"plugin:import/recommended", | ||
"plugin:import/typescript", | ||
"prettier", | ||
"plugin:tailwindcss/recommended" | ||
], | ||
"plugins": ["tailwindcss"], | ||
"rules": { | ||
"tailwindcss/no-custom-classname": "off", | ||
"tailwindcss/classnames-order": "off" | ||
}, | ||
"settings": { | ||
"import/resolver": { | ||
"typescript": { | ||
"alwaysTryTypes": true | ||
} | ||
} | ||
}, | ||
"ignorePatterns": ["**/components/ui/**"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files. | ||
|
||
# dependencies | ||
node_modules | ||
.pnp | ||
.pnp.js | ||
|
||
# testing | ||
coverage | ||
|
||
# next.js | ||
.next/ | ||
out/ | ||
build | ||
|
||
# misc | ||
.DS_Store | ||
*.pem | ||
|
||
# debug | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.pnpm-debug.log* | ||
|
||
# local env files | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
# turbo | ||
.turbo | ||
|
||
.env | ||
.vercel | ||
.vscode | ||
.env*.local |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Copyright 2024 Vercel, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<a href="https://chat.vercel.ai/"> | ||
<img alt="Next.js 14 and App Router-ready AI chatbot." src="app/(chat)/opengraph-image.png"> | ||
<h1 align="center">Next.js AI Chatbot</h1> | ||
</a> | ||
|
||
<p align="center"> | ||
An Open-Source AI Chatbot Template Built With Next.js and the AI SDK by Vercel. | ||
</p> | ||
|
||
<p align="center"> | ||
<a href="#features"><strong>Features</strong></a> · | ||
<a href="#model-providers"><strong>Model Providers</strong></a> · | ||
<a href="#deploy-your-own"><strong>Deploy Your Own</strong></a> · | ||
<a href="#running-locally"><strong>Running locally</strong></a> | ||
</p> | ||
<br/> | ||
|
||
## Features | ||
|
||
- [Next.js](https://nextjs.org) App Router | ||
- Advanced routing for seamless navigation and performance | ||
- React Server Components (RSCs) and Server Actions for server-side rendering and increased performance | ||
- [AI SDK](https://sdk.vercel.ai/docs) | ||
- Unified API for generating text, structured objects, and tool calls with LLMs | ||
- Hooks for building dynamic chat and generative user interfaces | ||
- Supports OpenAI (default), Anthropic, Cohere, and other model providers | ||
- [shadcn/ui](https://ui.shadcn.com) | ||
- Styling with [Tailwind CSS](https://tailwindcss.com) | ||
- Component primitives from [Radix UI](https://radix-ui.com) for accessibility and flexibility | ||
- Data Persistence | ||
- [Vercel Postgres powered by Neon](https://vercel.com/storage/postgres) for saving chat history and user data | ||
- [Vercel Blob](https://vercel.com/storage/blob) for efficient file storage | ||
- [NextAuth.js](https://github.com/nextauthjs/next-auth) | ||
- Simple and secure authentication | ||
|
||
## Model Providers | ||
|
||
This template ships with OpenAI `gpt-4o` as the default. However, with the [AI SDK](https://sdk.vercel.ai/docs), you can switch LLM providers to [OpenAI](https://openai.com), [Anthropic](https://anthropic.com), [Cohere](https://cohere.com/), and [many more](https://sdk.vercel.ai/providers/ai-sdk-providers) with just a few lines of code. | ||
|
||
## Deploy Your Own | ||
|
||
You can deploy your own version of the Next.js AI Chatbot to Vercel with one click: | ||
|
||
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fai-chatbot&env=AUTH_SECRET,OPENAI_API_KEY&envDescription=Learn%20more%20about%20how%20to%20get%20the%20API%20Keys%20for%20the%20application&envLink=https%3A%2F%2Fgithub.com%2Fvercel%2Fai-chatbot%2Fblob%2Fmain%2F.env.example&demo-title=AI%20Chatbot&demo-description=An%20Open-Source%20AI%20Chatbot%20Template%20Built%20With%20Next.js%20and%20the%20AI%20SDK%20by%20Vercel.&demo-url=https%3A%2F%2Fchat.vercel.ai&stores=[{%22type%22:%22postgres%22},{%22type%22:%22blob%22}]) | ||
|
||
## Running locally | ||
|
||
You will need to use the environment variables [defined in `.env.example`](.env.example) to run Next.js AI Chatbot. It's recommended you use [Vercel Environment Variables](https://vercel.com/docs/projects/environment-variables) for this, but a `.env` file is all that is necessary. | ||
|
||
> Note: You should not commit your `.env` file or it will expose secrets that will allow others to control access to your various OpenAI and authentication provider accounts. | ||
1. Install Vercel CLI: `npm i -g vercel` | ||
2. Link local instance with Vercel and GitHub accounts (creates `.vercel` directory): `vercel link` | ||
3. Download your environment variables: `vercel env pull` | ||
|
||
```bash | ||
pnpm install | ||
pnpm dev | ||
``` | ||
|
||
Your app template should now be running on [localhost:3000](http://localhost:3000/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
'use server'; | ||
|
||
import { z } from 'zod'; | ||
|
||
import { createUser, getUser } from '@/lib/db/queries'; | ||
|
||
import { signIn } from './auth'; | ||
|
||
const authFormSchema = z.object({ | ||
email: z.string().email(), | ||
password: z.string().min(6), | ||
}); | ||
|
||
export interface LoginActionState { | ||
status: 'idle' | 'in_progress' | 'success' | 'failed' | 'invalid_data'; | ||
} | ||
|
||
export const login = async ( | ||
_: LoginActionState, | ||
formData: FormData, | ||
): Promise<LoginActionState> => { | ||
try { | ||
const validatedData = authFormSchema.parse({ | ||
email: formData.get('email'), | ||
password: formData.get('password'), | ||
}); | ||
|
||
await signIn('credentials', { | ||
email: validatedData.email, | ||
password: validatedData.password, | ||
redirect: false, | ||
}); | ||
|
||
return { status: 'success' }; | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
return { status: 'invalid_data' }; | ||
} | ||
|
||
return { status: 'failed' }; | ||
} | ||
}; | ||
|
||
export interface RegisterActionState { | ||
status: | ||
| 'idle' | ||
| 'in_progress' | ||
| 'success' | ||
| 'failed' | ||
| 'user_exists' | ||
| 'invalid_data'; | ||
} | ||
|
||
export const register = async ( | ||
_: RegisterActionState, | ||
formData: FormData, | ||
): Promise<RegisterActionState> => { | ||
try { | ||
const validatedData = authFormSchema.parse({ | ||
email: formData.get('email'), | ||
password: formData.get('password'), | ||
}); | ||
|
||
const [user] = await getUser(validatedData.email); | ||
|
||
if (user) { | ||
return { status: 'user_exists' } as RegisterActionState; | ||
} | ||
await createUser(validatedData.email, validatedData.password); | ||
await signIn('credentials', { | ||
email: validatedData.email, | ||
password: validatedData.password, | ||
redirect: false, | ||
}); | ||
|
||
return { status: 'success' }; | ||
} catch (error) { | ||
if (error instanceof z.ZodError) { | ||
return { status: 'invalid_data' }; | ||
} | ||
|
||
return { status: 'failed' }; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { GET, POST } from '@/app/(auth)/auth'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import type { NextAuthConfig } from 'next-auth'; | ||
|
||
export const authConfig = { | ||
pages: { | ||
signIn: '/login', | ||
newUser: '/', | ||
}, | ||
providers: [ | ||
// added later in auth.ts since it requires bcrypt which is only compatible with Node.js | ||
// while this file is also used in non-Node.js environments | ||
], | ||
callbacks: { | ||
authorized({ auth, request: { nextUrl } }) { | ||
const isLoggedIn = !!auth?.user; | ||
const isOnChat = nextUrl.pathname.startsWith('/'); | ||
const isOnRegister = nextUrl.pathname.startsWith('/register'); | ||
const isOnLogin = nextUrl.pathname.startsWith('/login'); | ||
|
||
if (isLoggedIn && (isOnLogin || isOnRegister)) { | ||
return Response.redirect(new URL('/', nextUrl as unknown as URL)); | ||
} | ||
|
||
if (isOnRegister || isOnLogin) { | ||
return true; // Always allow access to register and login pages | ||
} | ||
|
||
if (isOnChat) { | ||
if (isLoggedIn) return true; | ||
return false; // Redirect unauthenticated users to login page | ||
} | ||
|
||
if (isLoggedIn) { | ||
return Response.redirect(new URL('/', nextUrl as unknown as URL)); | ||
} | ||
|
||
return true; | ||
}, | ||
}, | ||
} satisfies NextAuthConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { compare } from 'bcrypt-ts'; | ||
import NextAuth, { type User, type Session } from 'next-auth'; | ||
import Credentials from 'next-auth/providers/credentials'; | ||
|
||
import { getUser } from '@/lib/db/queries'; | ||
|
||
import { authConfig } from './auth.config'; | ||
|
||
interface ExtendedSession extends Session { | ||
user: User; | ||
} | ||
|
||
export const { | ||
handlers: { GET, POST }, | ||
auth, | ||
signIn, | ||
signOut, | ||
} = NextAuth({ | ||
...authConfig, | ||
providers: [ | ||
Credentials({ | ||
credentials: {}, | ||
async authorize({ email, password }: any) { | ||
const users = await getUser(email); | ||
if (users.length === 0) return null; | ||
// biome-ignore lint: Forbidden non-null assertion. | ||
const passwordsMatch = await compare(password, users[0].password!); | ||
if (!passwordsMatch) return null; | ||
return users[0] as any; | ||
}, | ||
}), | ||
], | ||
callbacks: { | ||
async jwt({ token, user }) { | ||
if (user) { | ||
token.id = user.id; | ||
} | ||
|
||
return token; | ||
}, | ||
async session({ | ||
session, | ||
token, | ||
}: { | ||
session: ExtendedSession; | ||
token: any; | ||
}) { | ||
if (session.user) { | ||
session.user.id = token.id as string; | ||
} | ||
|
||
return session; | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.