Skip to content

Commit

Permalink
Fixes so it builds on Vercel.
Browse files Browse the repository at this point in the history
  • Loading branch information
cammoore committed Nov 1, 2024
1 parent 2ec2150 commit 8d92eee
Show file tree
Hide file tree
Showing 11 changed files with 359 additions and 940 deletions.
1,092 changes: 254 additions & 838 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ generator client {

datasource db {
provider = "postgresql"
// for local development
url = env("DATABASE_URL")
// for Vercel
// url = env("POSTGRES_PRISMA_URL")
// directUrl = env("POSTGRES_URL_NON_POOLING")
}

enum Role {
Expand Down
2 changes: 1 addition & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async function main() {
}
console.log(` Adding stuff: ${data.name} (${data.owner})`);
await prisma.stuff.upsert({
where: { id: index },
where: { id: index + 1 },
update: {},
create: {
name: data.name,
Expand Down
12 changes: 11 additions & 1 deletion sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ DATABASE_URL="postgresql://user:password@localhost:5432/mydb?schema=public"
# for redirections on login, etc.

NEXTAUTH_SECRET=secret
NEXTAUTH_URL=http://localhost:3000
NEXTAUTH_URL=http://localhost:3000

# Created by Vercel CLI
POSTGRES_DATABASE="verceldb"
POSTGRES_HOST="***"
POSTGRES_PASSWORD="***"
POSTGRES_PRISMA_URL="postgres://default:***@***/verceldb?pgbouncer=true&connect_timeout=15&sslmode=require"
POSTGRES_URL="postgres://default:***@***/verceldb?sslmode=require"
POSTGRES_URL_NON_POOLING="postgres://default:***@***/verceldb?sslmode=require"
POSTGRES_URL_NO_SSL="postgres://default:***@***/verceldb"
POSTGRES_USER="default"
2 changes: 1 addition & 1 deletion src/app/add/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getServerSession } from 'next-auth';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import authOptions from '@/lib/authOptions';
import { loggedInProtectedPage } from '@/lib/page-protection';
import AddStuffForm from '@/components/AddStuffForm';

Expand Down
2 changes: 1 addition & 1 deletion src/app/admin/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Col, Container, Row, Table } from 'react-bootstrap';
import StuffItemAdmin from '@/components/StuffItemAdmin';
import { prisma } from '@/lib/prisma';
import { adminProtectedPage } from '@/lib/page-protection';
import { authOptions } from '../api/auth/[...nextauth]/route';
import authOptions from '@/lib/authOptions';

const AdminPage = async () => {
const session = await getServerSession(authOptions);
Expand Down
82 changes: 2 additions & 80 deletions src/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,83 +1,5 @@
/* eslint-disable arrow-body-style */
import { compare } from 'bcrypt';
import NextAuth, { type NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { prisma } from '@/lib/prisma';

export const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
providers: [
CredentialsProvider({
name: 'Email and Password',
credentials: {
email: {
label: 'Email',
type: 'email',
placeholder: '[email protected]',
},
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
if (!credentials?.email || !credentials.password) {
return null;
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});
if (!user) {
return null;
}

const isPasswordValid = await compare(credentials.password, user.password);
if (!isPasswordValid) {
return null;
}

return {
id: `${user.id}`,
email: user.email,
randomKey: user.role,
};
},
}),
],
pages: {
signIn: '/auth/signin',
signOut: '/auth/signout',
// error: '/auth/error',
// verifyRequest: '/auth/verify-request',
// newUser: '/auth/new-user'
},
callbacks: {
session: ({ session, token }) => {
// console.log('Session Callback', { session, token })
return {
...session,
user: {
...session.user,
id: token.id,
randomKey: token.randomKey,
},
};
},
jwt: ({ token, user }) => {
// console.log('JWT Callback', { token, user })
if (user) {
const u = user as unknown as any;
return {
...token,
id: u.id,
randomKey: u.randomKey,
};
}
return token;
},
},
};
import NextAuth from 'next-auth';
import authOptions from '@/lib/authOptions';

const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
16 changes: 0 additions & 16 deletions src/app/api/route.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/edit/[id]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getServerSession } from 'next-auth';
import { notFound } from 'next/navigation';
import { Stuff } from '@prisma/client';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import authOptions from '@/lib/authOptions';
import { loggedInProtectedPage } from '@/lib/page-protection';
import { prisma } from '@/lib/prisma';
import EditStuffForm from '@/components/EditStuffForm';
Expand Down
2 changes: 1 addition & 1 deletion src/app/list/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Col, Container, Row, Table } from 'react-bootstrap';
import { prisma } from '@/lib/prisma';
import StuffItem from '@/components/StuffItem';
import { loggedInProtectedPage } from '@/lib/page-protection';
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
import authOptions from '@/lib/authOptions';

/** Render a list of stuff for the logged in user. */
const ListPage = async () => {
Expand Down
83 changes: 83 additions & 0 deletions src/lib/authOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/* eslint-disable arrow-body-style */
import { compare } from 'bcrypt';
import { type NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import { prisma } from '@/lib/prisma';

const authOptions: NextAuthOptions = {
session: {
strategy: 'jwt',
},
providers: [
CredentialsProvider({
name: 'Email and Password',
credentials: {
email: {
label: 'Email',
type: 'email',
placeholder: '[email protected]',
},
password: { label: 'Password', type: 'password' },
},
async authorize(credentials) {
if (!credentials?.email || !credentials.password) {
return null;
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});
if (!user) {
return null;
}

const isPasswordValid = await compare(credentials.password, user.password);
if (!isPasswordValid) {
return null;
}

return {
id: `${user.id}`,
email: user.email,
randomKey: user.role,
};
},
}),
],
pages: {
signIn: '/auth/signin',
signOut: '/auth/signout',
// error: '/auth/error',
// verifyRequest: '/auth/verify-request',
// newUser: '/auth/new-user'
},
callbacks: {
session: ({ session, token }) => {
// console.log('Session Callback', { session, token })
return {
...session,
user: {
...session.user,
id: token.id,
randomKey: token.randomKey,
},
};
},
jwt: ({ token, user }) => {
// console.log('JWT Callback', { token, user })
if (user) {
const u = user as unknown as any;
return {
...token,
id: u.id,
randomKey: u.randomKey,
};
}
return token;
},
},
secret: process.env.NEXTAUTH_SECRET,
};

export default authOptions;

0 comments on commit 8d92eee

Please sign in to comment.