Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Updated to use Vercel Postgres + Suspense streaming + Metadata API #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
steven-tey merged 7 commits into main from postgres
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .env.example
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Create a MySQL database with 2 clicks (no account needed): https://railway.app/
DATABASE_URL="mysql://johndoe:randompassword@localhost:5432/mydb?schema=public"
# Create a Postgres database on Vercel: https://vercel.com/postgres
POSTGRES_URL=
POSTGRES_PRISMA_URL=
POSTGRES_URL_NON_POOLING=
POSTGRES_USER=
POSTGRES_HOST=
POSTGRES_PASSWORD=
POSTGRES_DATABASE=

# Generate one here: https://generate-secret.vercel.app/32 (only required for localhost)
NEXTAUTH_SECRET=
10 changes: 5 additions & 5 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
<p align="center">
<a href="https://nextjs-mysql-auth.vercel.app/">
<a href="https://nextjs-postgres-auth.vercel.app/">
<img src="/public/logo.png" height="96">
<h3 align="center">Next.js Prisma MySQL Auth Starter</h3>
<h3 align="center">Next.js Prisma PostgreSQL Auth Starter</h3>
</a>
</p>

<p align="center">
This is a <a href="https://nextjs.org/">Next.js</a> starter kit that uses <a href="https://next-auth.js.org/">Next-Auth</a> for simple email + password login<br/>
<a href="https://www.prisma.io/">Prisma</a> as the ORM, and a MySQL database to persist the data.</p>
<a href="https://www.prisma.io/">Prisma</a> as the ORM, and a PostgreSQL database to persist the data.</p>

<br/>

## Deploy Your Own

You can clone & deploy it to Vercel with one click:

[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?demo-title=Next.js%20MySQL%20Auth%20Starter&demo-description=Simple%20Next.js%2013%20starter%20kit%20that%20uses%20Next-Auth%20for%20auth%20and%20MySQL%20as%20a%20database.&demo-url=https%3A%2F%2Fnextjs-mysql-auth.vercel.app%2F&demo-image=%2F%2Fimages.ctfassets.net%2Fe5382hct74si%2F7rsVQ1ZBSiWe9JGO6FUeZZ%2F210cba91036ca912b2770e0bd5d6cc5d%2Fthumbnail.png&project-name=Next.js%20MySQL%20Auth%20Starter&repository-name=nextjs-mysql-auth-starter&repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnextjs-mysql-auth-starter&from=templates&skippable-integrations=1&env=DATABASE_URL%2CNEXTAUTH_SECRET&envDescription=Generate%20a%20random%20secret%3A&envLink=https%3A%2F%2Fgithub.com%2Fvercel%2Fnextjs-mysql-auth-starter%2Fblob%2Fmain%2F.env.example)
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?demo-title=Next.js%20Prisma%20PostgreSQL%20Auth%20Starter&demo-description=Simple%20Next.js%2013%20starter%20kit%20that%20uses%20Next-Auth%20for%20auth%20and%20Prisma%20PostgreSQL%20as%20a%20database.&demo-url=https%3A%2F%2Fnextjs-postgres-auth.vercel.app%2F&demo-image=%2F%2Fimages.ctfassets.net%2Fe5382hct74si%2F7rsVQ1ZBSiWe9JGO6FUeZZ%2F210cba91036ca912b2770e0bd5d6cc5d%2Fthumbnail.png&project-name=Next.js%%20Prisma%20PostgreSQL%20Auth%20Starter&repository-name=nextjs-postgres-auth-starter&repository-url=https%3A%2F%2Fgithub.com%2Fvercel%2Fnextjs-postgres-auth-starter&from=templates&skippable-integrations=1&env=NEXTAUTH_SECRET&envDescription=Generate%20a%20random%20secret%3A&envLink=https://generate-secret.vercel.app/&stores=%5B%7B"type"%3A"postgres"%7D%5D)

## Developing Locally

You can clone & create this repo with the following command

```bash
npx create-next-app nextjs-typescript-starter --example "https://github.com/vercel/nextjs-mysql-auth-starter"
npx create-next-app nextjs-typescript-starter --example "https://github.com/vercel/nextjs-postgres-auth-starter"
```

## Getting Started
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import NextAuth from "next-auth";
import NextAuth, { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import prisma from "@/lib/prisma";
import { compare } from "bcrypt";

export default NextAuth({
export const authOptions: NextAuthOptions = {
providers: [
CredentialsProvider({
credentials: {},
Expand All @@ -30,4 +30,8 @@ export default NextAuth({
}),
],
session: { strategy: "jwt" },
});
};

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };
12 changes: 5 additions & 7 deletions pages/api/auth/register.ts → app/api/auth/register/route.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import prisma from "@/lib/prisma";
import { NextApiRequest, NextApiResponse } from "next";
import { hash } from "bcrypt";
import { NextResponse } from "next/server";

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { email, password } = req.body;
export async function POST(req: Request) {
const { email, password } = await req.json();
const exists = await prisma.user.findUnique({
where: {
email,
},
});
if (exists) {
res.status(400).send("User already exists");
return NextResponse.json({ error: "User already exists" }, { status: 400 });
} else {
const user = await prisma.user.create({
data: {
email,
password: await hash(password, 10),
},
});
res.status(200).json(user);
return NextResponse.json(user);
}
}
21 changes: 0 additions & 21 deletions app/head.tsx
View file Open in desktop

This file was deleted.

27 changes: 24 additions & 3 deletions app/layout.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
// These styles apply to every route in the application
import "@/styles/globals.css";
import { Metadata } from "next";
import { Inter } from "@next/font/google";
import Toaster from "@/components/toaster";
import { Toaster } from "react-hot-toast";
import AuthStatus from "@/components/auth-status";
import { Suspense } from "react";

const inter = Inter({
variable: "--font-inter",
subsets: ["latin"],
});

const title = "Next.js Prisma Postgres Auth Starter";
const description =
"This is a Next.js starter kit that uses Next-Auth for simple email + password login and a Postgres database to persist the data.";

export const metadata: Metadata = {
title,
description,
twitter: {
card: "summary_large_image",
title,
description,
},
metadataBase: new URL("https://nextjs-postgres-auth.vercel.app"),
themeColor: "#FFF",
};

export default async function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
const AuthStatusDiv = await AuthStatus();
return (
<html lang="en">
<body className={inter.variable}>
<Toaster />
{AuthStatusDiv}
<Suspense fallback="Loading...">
{/* @ts-expect-error Async Server Component */}
<AuthStatus />
</Suspense>
{children}
</body>
</html>
Expand Down
5 changes: 3 additions & 2 deletions app/login/page.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import Image from "next/image";
import Form from "@/components/form";
import Link from "next/link";

export default function Login() {
return (
<div className="flex h-screen w-screen items-center justify-center bg-gray-50">
<div className="z-10 w-full max-w-md overflow-hidden rounded-2xl border border-gray-100 shadow-xl">
<div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 bg-white px-4 py-6 pt-8 text-center sm:px-16">
<a href="https://dub.sh">
<Link href="/">
<Image
src="/logo.png"
alt="Logo"
className="h-10 w-10 rounded-full"
width={20}
height={20}
/>
</a>
</Link>
<h3 className="text-xl font-semibold">Sign In</h3>
<p className="text-sm text-gray-500">
Use your email and password to sign in
Expand Down
Binary file added app/opengraph-image.png
View file Open in desktop
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
[フレーム]
6 changes: 3 additions & 3 deletions app/page.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function Home() {
/>
<div className="text-center max-w-screen-sm mb-10">
<h1 className="text-stone-200 font-bold text-2xl">
Next.js Prisma MySQL Auth Starter
Next.js Prisma PostgreSQL Auth Starter
</h1>
<p className="text-stone-400 mt-5">
This is a{" "}
Expand All @@ -35,8 +35,8 @@ export default function Home() {
>
Next-Auth
</a>{" "}
for simple email + password login and a MySQL database to persist
the data.
for simple email + password login and a PostgreSQL database to
persist the data.
</p>
</div>
<div className="flex space-x-3">
Expand Down
1 change: 0 additions & 1 deletion app/protected/page.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export default function Home() {
<iframe
src="https://www.youtube.com/embed/dQw4w9WgXcQ?autoplay=1"
title="YouTube video player"
frameBorder="0"
allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
allowFullScreen
className="w-full max-w-screen-lg aspect-video"
Expand Down
5 changes: 3 additions & 2 deletions app/register/page.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import Image from "next/image";
import Form from "@/components/form";
import Link from "next/link";

export default function Login() {
return (
<div className="flex h-screen w-screen items-center justify-center bg-gray-50">
<div className="z-10 w-full max-w-md overflow-hidden rounded-2xl border border-gray-100 shadow-xl">
<div className="flex flex-col items-center justify-center space-y-3 border-b border-gray-200 bg-white px-4 py-6 pt-8 text-center sm:px-16">
<a href="https://dub.sh">
<Link href="/">
<Image
src="/logo.png"
alt="Logo"
className="h-10 w-10 rounded-full"
width={20}
height={20}
/>
</a>
</Link>
<h3 className="text-xl font-semibold">Sign Up</h3>
<p className="text-sm text-gray-500">
Create an account with your email and password
Expand Down
7 changes: 5 additions & 2 deletions components/form.tsx
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export default function Form({ type }: { type: "login" | "register" }) {
password: e.currentTarget.password.value,
// @ts-ignore
}).then(({ ok, error }) => {
console.log("ok", ok, "error", error);
setLoading(false);
if (ok) {
router.push("/protected");
} else {
}
if (error) {
toast.error(error);
}
});
Expand All @@ -48,7 +50,8 @@ export default function Form({ type }: { type: "login" | "register" }) {
router.push("/login");
}, 2000);
} else {
toast.error(await res.text());
const { error } = await res.json();
toast.error(error);
}
});
}
Expand Down
3 changes: 0 additions & 3 deletions components/toaster.tsx
View file Open in desktop

This file was deleted.

3 changes: 2 additions & 1 deletion middleware.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { NextRequest, NextResponse } from "next/server";
export default async function middleware(req: NextRequest) {
// Get the pathname of the request (e.g. /, /protected)
const path = req.nextUrl.pathname;

// If it's the root path, just render it
if (path === "/") {
return NextResponse.next();
Expand All @@ -14,6 +14,7 @@ export default async function middleware(req: NextRequest) {
req,
secret: process.env.NEXTAUTH_SECRET,
});

if (!session && path === "/protected") {
return NextResponse.redirect(new URL("/login", req.url));
} else if (session && (path === "/login" || path === "/register")) {
Expand Down
12 changes: 2 additions & 10 deletions next.config.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
reactStrictMode: true,
experimental: {
appDir: true,
serverComponentsExternalPackages: ["prisma", "@prisma/client"],
},
};
/** @type {import('next').NextConfig} */
const nextConfig = {};

module.exports = nextConfig;
19 changes: 8 additions & 11 deletions package.json
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
{
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"dev": "prisma generate && next dev",
"build": "prisma generate && prisma db push next build",
"start": "next start",
"lint": "next lint",
"prebuild": "prisma generate",
"predev": "prisma generate",
"prisma:migrate": "prisma migrate deploy"
"lint": "next lint"
},
"dependencies": {
"@next/font": "^13.0.5",
"@prisma/client": "^4.6.1",
"@prisma/client": "^4.14.0",
"@types/node": "^18.11.9",
"@types/react": "^18.0.25",
"bcrypt": "^5.1.0",
"next": "^13.0.5",
"next-auth": "^4.17.0",
"next": "^13.4.2",
"next-auth": "^4.22.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-hot-toast": "^2.4.0"
"react-hot-toast": "^2.4.1"
},
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"autoprefixer": "^10.4.4",
"eslint": "8.11.0",
"eslint-config-next": "^13.0.5",
"postcss": "^8.4.12",
"prisma": "^4.6.1",
"prisma": "^4.14.0",
"tailwindcss": "^3.0.23",
"typescript": "^4.6.2"
}
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /