-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: set up database and auth (#178)
Co-authored-by: Cyro292 <[email protected]> Co-authored-by: simonhng <[email protected]> Co-authored-by: Behsad Riemer <[email protected]>
- Loading branch information
1 parent
b06e238
commit c7fca61
Showing
29 changed files
with
2,125 additions
and
845 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 @@ | ||
FROM node:20-alpine | ||
|
||
WORKDIR /space | ||
|
||
COPY . . | ||
|
||
RUN cd app && apk add --no-cache make gcc g++ python3 py3-pip && \ | ||
npm install && \ | ||
npm rebuild bcrypt --build-from-source && \ | ||
apk del make gcc g++ python3 py3-pip | ||
|
||
WORKDIR /space/app | ||
|
||
ENTRYPOINT ["sh", "entrypoint.sh"] |
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 |
---|---|---|
@@ -1 +1,4 @@ | ||
DATABASE_URL=postgres://postgres:password@localhost:15432/spacedb | ||
NEXT_PUBLIC_ENVIRONMENT=development | ||
NEXT_PUBLIC_API_URL=https://localhost:8000/ | ||
NEXTAUTH_URL=https://localhost:3000 | ||
DATABASE_URL=postgres://postgres:password@localhost:15432/spacedb |
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 |
---|---|---|
|
@@ -33,3 +33,5 @@ yarn-error.log* | |
# typescript | ||
*.tsbuildinfo | ||
next-env.d.ts | ||
|
||
certificates |
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
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,109 @@ | ||
import { NextAuthOptions } from "next-auth"; | ||
import NextAuth from "next-auth/next"; | ||
import SlackProvider from "next-auth/providers/slack"; | ||
import { PrismaAdapter } from "@next-auth/prisma-adapter" | ||
import prisma from "database/db"; | ||
import CredentialsProvider from "next-auth/providers/credentials"; | ||
import EmailProvider from "next-auth/providers/email"; | ||
import { compare } from "bcrypt"; | ||
|
||
export const authOptions: NextAuthOptions = { | ||
session:{ | ||
strategy:"jwt", | ||
maxAge: 30 * 24 * 60 * 60 //30 days | ||
}, | ||
adapter: PrismaAdapter(prisma), | ||
secret: process.env.NEXTAUTH_SECRET, | ||
pages: { | ||
signIn: '/auth', | ||
/* | ||
newUser: '/auth/signup', | ||
signOut: '/auth/signout', | ||
error: '/auth/error', // Error code passed in query string as ?error= | ||
verifyRequest: '/auth/verify-request', // (used for check email message) | ||
*/ | ||
//TODO: add signOut, error pages | ||
}, | ||
providers: [ | ||
SlackProvider( | ||
{ | ||
clientId: process.env.SLACK_CLIENT_ID, | ||
clientSecret: process.env.SLACK_CLIENT_SECRET, | ||
profile(profile, tokens) { | ||
return { | ||
id: profile["https://slack.com/user_id"] || profile.sub, | ||
email: profile.email, | ||
image: profile.picture, | ||
first_name: profile.given_name, | ||
permission: 'member', | ||
last_name: profile.family_name, | ||
emailVerified: profile.date_email_verified | ||
}; | ||
}, | ||
}, | ||
), | ||
//TODO: add email provider setup | ||
CredentialsProvider({ | ||
name: "Credentials", | ||
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 existingUser = await prisma.user.findUnique({ | ||
where: { email : credentials?.email } | ||
}); | ||
|
||
if(!existingUser) { | ||
return null; | ||
} | ||
|
||
const passwordMatch = await compare(credentials.password, existingUser.password); | ||
|
||
if(!passwordMatch) { | ||
return null; | ||
} | ||
|
||
return { | ||
id: `${existingUser.id}`, | ||
username: existingUser.first_name + "_" + existingUser.last_name, //do we really need this here? | ||
email: existingUser.email | ||
} | ||
} | ||
}), | ||
// EmailProvider({ | ||
// server: process.env.EMAIL_SERVER, | ||
// from: process.env.EMAIL_FROM | ||
// }), | ||
], | ||
callbacks:{ | ||
async jwt({token,user}){ | ||
return{...token,...user} | ||
}, | ||
async session({session,token}){ | ||
return { | ||
...session, | ||
user : { | ||
...session.user, | ||
id: token.id, | ||
first_name: token.first_name, | ||
permission: token.permission, | ||
image: token.image, | ||
} | ||
} | ||
// session.user.id = token.id | ||
// session.user.first_name = token.first_name | ||
// session.user.permission = token.permission | ||
// session.user.image = token.image | ||
// return session | ||
} | ||
} | ||
} | ||
|
||
const handler = NextAuth(authOptions) | ||
|
||
export {handler as GET, handler as POST} |
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,32 @@ | ||
import { NextResponse } from 'next/server'; | ||
import db from '../../../../database/db'; | ||
import { hash } from 'bcrypt'; | ||
|
||
|
||
|
||
export default async function POST(req, res) { | ||
try { | ||
const { email, password } = req.body; | ||
console.log(email, password); | ||
const existingUser = await db.user.findUnique({ | ||
where: { email : email } | ||
}); | ||
if (!existingUser) { | ||
return NextResponse.json({ error: 'User with this email already exists' }, { status: 409 }); | ||
} | ||
|
||
const hashedPassword = await hash(password, 10); | ||
|
||
// const newUser = await db.user.create({ | ||
// data: { | ||
// email: email, | ||
// password: hashedPassword | ||
// //TODO: add other fields | ||
// } | ||
// }); | ||
|
||
return NextResponse.json({ message: 'User created' }, { status: 201 }); | ||
} catch (error) { | ||
return NextResponse.json({ error: 'Something went wrong' }, { status: 500 }); | ||
} | ||
} |
Oops, something went wrong.