diff --git a/components/userInfo.js b/components/userInfo.js new file mode 100644 index 0000000..e400c67 --- /dev/null +++ b/components/userInfo.js @@ -0,0 +1,25 @@ +import { useSession, signIn, signOut } from "next-auth/react"; +import styles from "../styles/Components.module.css"; + +export async function getServerSideProps(context) { + return { + props: { + borderInfo, + }, + }; +} + +export default function UserInfo(borderInfo) { + const { data: session } = useSession(); + return ( +

+ {session ? `Signed in as ${session.user.name}` : "Not signed in"} +
+ {session ? ( + + ) : ( + + )} +

+ ); +} diff --git a/lib/borders.js b/lib/borders.js new file mode 100644 index 0000000..c6945b1 --- /dev/null +++ b/lib/borders.js @@ -0,0 +1,43 @@ +import { getServerSession } from "next-auth"; +import { getSession } from "next-auth/react"; +import prisma from "./prisma"; + +export const getUserBorders = async (req) => { + const session = await getSession({ req }); + if (!session) { + return undefined; + } + + const accountData = await prisma.account.findFirst({ + where: { + userId: session.user.id, + }, + }); + + const userData = await prisma.applicationUserData.findUnique({ + where: { + userId: session.user.id, + }, + }); + + if (!!userData) { + return userData; + } + + const result = await prisma.applicationUserData.create({ + data: { + userId: session.user.id, + discordId: accountData.providerAccountId, + }, + }); + return result; +}; + +export const getByDiscordId = async (id) => { + const userData = await prisma.applicationUserData.findUnique({ + where: { + discordId: id, + }, + }); + return userData ? `${userData.borderId}` : undefined; +}; diff --git a/lib/prisma.js b/lib/prisma.js new file mode 100644 index 0000000..cc91a65 --- /dev/null +++ b/lib/prisma.js @@ -0,0 +1,14 @@ +import { PrismaClient } from "@prisma/client"; + +let prisma; + +if (process.env.NODE_ENV === "production") { + prisma = new PrismaClient(); +} else { + if (!global.prisma) { + global.prisma = new PrismaClient(); + } + prisma = global.prisma; +} + +export default prisma; diff --git a/package.json b/package.json index 80009cd..8ab3685 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,8 @@ "lint": "next lint" }, "dependencies": { + "@next-auth/prisma-adapter": "^1.0.3", + "@prisma/client": "^3.12.0", "dotenv": "^16.0.0", "next": "12.1.4", "next-auth": "^4.3.1", diff --git a/pages/api/auth/[...nextauth].js b/pages/api/auth/[...nextauth].js index 43a1043..f12c5bf 100644 --- a/pages/api/auth/[...nextauth].js +++ b/pages/api/auth/[...nextauth].js @@ -1,11 +1,21 @@ import NextAuth from "next-auth"; import DiscordProvider from "next-auth/providers/discord"; +import { PrismaAdapter } from "@next-auth/prisma-adapter"; +import prisma from "../../../lib/prisma"; export default NextAuth({ + adapter: PrismaAdapter(prisma), providers: [ DiscordProvider({ clientId: process.env.DISCORD_CLIENT_ID, clientSecret: process.env.DISCORD_CLIENT_SECRET, }), ], + callbacks: { + async session({ session, token, user }) { + session.user.id = user.id; + console.log(JSON.stringify(user)); + return session; + }, + }, }); diff --git a/pages/api/user/border/@me.js b/pages/api/user/border/@me.js new file mode 100644 index 0000000..61cf0e8 --- /dev/null +++ b/pages/api/user/border/@me.js @@ -0,0 +1,11 @@ +import { getUserBorders } from "../../../../lib/borders"; + +export default function handler(req, res) { + getUserBorders(req).then((result) => { + if (result) { + return res.status(200).json(result); + } else { + return res.status(404).json({ error: "Not Found" }); + } + }); +} diff --git a/pages/api/user/border/[id].js b/pages/api/user/border/[id].js new file mode 100644 index 0000000..8db7eee --- /dev/null +++ b/pages/api/user/border/[id].js @@ -0,0 +1,11 @@ +import { getByDiscordId } from "../../../../lib/borders"; + +export default function handler(req, res) { + const id = req.query.id; + + getByDiscordId(id).then((result) => { + const borderUrl = result ?? "0"; + + return res.status(200).send(borderUrl); + }); +} diff --git a/pages/index.js b/pages/index.js index 8edbb81..4596f3b 100644 --- a/pages/index.js +++ b/pages/index.js @@ -1,10 +1,9 @@ -import { useSession, signIn, signOut } from "next-auth/react"; import Head from "next/head"; import Image from "next/image"; import styles from "../styles/Home.module.css"; +import UserInfo from "../components/userInfo"; export default function Home() { - const { data: session } = useSession(); return (
@@ -15,16 +14,7 @@ export default function Home() {

Steam Borders

- -

- {session ? `Signed in as ${session.user.name}` : "Not signed in"} -
- {session ? ( - - ) : ( - - )} -

+
); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2dc7913..db1d77a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,6 +1,8 @@ lockfileVersion: 5.3 specifiers: + '@next-auth/prisma-adapter': ^1.0.3 + '@prisma/client': ^3.12.0 dotenv: ^16.0.0 eslint: 8.13.0 eslint-config-next: 12.1.4 @@ -11,6 +13,8 @@ specifiers: react-dom: 18.0.0 dependencies: + '@next-auth/prisma-adapter': 1.0.3_2b535bbfb604d219371c9582b52b11b1 + '@prisma/client': 3.12.0_prisma@3.12.0 dotenv: 16.0.0 next: 12.1.4_react-dom@18.0.0+react@18.0.0 next-auth: 4.3.1_react-dom@18.0.0+react@18.0.0 @@ -70,6 +74,16 @@ packages: resolution: {integrity: sha512-ZnQMnLV4e7hDlUvw8H+U8ASL02SS2Gn6+9Ac3wGGLIe7+je2AeAOxPY+izIPJDfFDb7eDjev0Us8MO1iFRN8hA==} dev: true + /@next-auth/prisma-adapter/1.0.3_2b535bbfb604d219371c9582b52b11b1: + resolution: {integrity: sha512-3Lq1cD3ytKM3EGKJZ4UZvlqshLtlPvYxLeCrUV9ifYwYlq51kmDaHjsIawlp8EbH5pE1UhlsvtlXMery7RghtA==} + peerDependencies: + '@prisma/client': '>=2.26.0 || >=3' + next-auth: ^4.0.1 + dependencies: + '@prisma/client': 3.12.0_prisma@3.12.0 + next-auth: 4.3.1_react-dom@18.0.0+react@18.0.0 + dev: false + /@next/env/12.1.4: resolution: {integrity: sha512-7gQwotJDKnfMxxXd8xJ2vsX5AzyDxO3zou0+QOXX8/unypA6icw5+wf6A62yKZ6qQ4UZHHxS68pb6UV+wNneXg==} dev: false @@ -213,6 +227,24 @@ packages: resolution: {integrity: sha512-mMyQ9vjpuFqePkfe5bZVIf/H3Dmk6wA8Kjxff9RcO4kqzJo+Ek9pGKwZHpeMr7Eku0QhLXMCd7fNCSnEnRMubg==} dev: false + /@prisma/client/3.12.0_prisma@3.12.0: + resolution: {integrity: sha512-4NEQjUcWja/NVBvfuDFscWSk1/rXg3+wj+TSkqXCb1tKlx/bsUE00rxsvOvGg7VZ6lw1JFpGkwjwmsOIc4zvQw==} + engines: {node: '>=12.6'} + requiresBuild: true + peerDependencies: + prisma: '*' + peerDependenciesMeta: + prisma: + optional: true + dependencies: + '@prisma/engines-version': 3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980 + prisma: 3.12.0 + dev: false + + /@prisma/engines-version/3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980: + resolution: {integrity: sha512-o+jo8d7ZEiVpcpNWUDh3fj2uPQpBxl79XE9ih9nkogJbhw6P33274SHnqheedZ7PyvPIK/mvU8MLNYgetgXPYw==} + dev: false + /@prisma/engines/3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980: resolution: {integrity: sha512-zULjkN8yhzS7B3yeEz4aIym4E2w1ChrV12i14pht3ePFufvsAvBSoZ+tuXMvfSoNTgBS5E4bolRzLbMmbwkkMQ==} requiresBuild: true diff --git a/prisma/borders.db b/prisma/borders.db new file mode 100644 index 0000000..91aa7c6 Binary files /dev/null and b/prisma/borders.db differ diff --git a/prisma/migrations/20220410164403_/migration.sql b/prisma/migrations/20220410164403_/migration.sql new file mode 100644 index 0000000..5a9a13b --- /dev/null +++ b/prisma/migrations/20220410164403_/migration.sql @@ -0,0 +1,68 @@ +-- CreateTable +CREATE TABLE "Account" ( + "id" TEXT NOT NULL PRIMARY KEY, + "userId" TEXT NOT NULL, + "type" TEXT NOT NULL, + "provider" TEXT NOT NULL, + "providerAccountId" TEXT NOT NULL, + "refresh_token" TEXT, + "access_token" TEXT, + "expires_at" INTEGER, + "token_type" TEXT, + "scope" TEXT, + "id_token" TEXT, + "session_state" TEXT, + CONSTRAINT "Account_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateTable +CREATE TABLE "Session" ( + "id" TEXT NOT NULL PRIMARY KEY, + "sessionToken" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "expires" DATETIME NOT NULL, + CONSTRAINT "Session_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User" ("id") ON DELETE CASCADE ON UPDATE CASCADE +); + +-- CreateTable +CREATE TABLE "User" ( + "id" TEXT NOT NULL PRIMARY KEY, + "name" TEXT, + "email" TEXT, + "emailVerified" DATETIME, + "image" TEXT +); + +-- CreateTable +CREATE TABLE "VerificationToken" ( + "identifier" TEXT NOT NULL, + "token" TEXT NOT NULL, + "expires" DATETIME NOT NULL +); + +-- CreateTable +CREATE TABLE "ApplicationUserData" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "discordId" TEXT NOT NULL, + "borderUrl" TEXT, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL +); + +-- CreateIndex +CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId"); + +-- CreateIndex +CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken"); + +-- CreateIndex +CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); + +-- CreateIndex +CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token"); + +-- CreateIndex +CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token"); + +-- CreateIndex +CREATE UNIQUE INDEX "ApplicationUserData_discordId_key" ON "ApplicationUserData"("discordId"); diff --git a/prisma/migrations/20220410173021_/migration.sql b/prisma/migrations/20220410173021_/migration.sql new file mode 100644 index 0000000..df0b158 --- /dev/null +++ b/prisma/migrations/20220410173021_/migration.sql @@ -0,0 +1,22 @@ +/* + Warnings: + + - You are about to drop the column `discordId` on the `ApplicationUserData` table. All the data in the column will be lost. + - Added the required column `userId` to the `ApplicationUserData` table without a default value. This is not possible if the table is not empty. + +*/ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_ApplicationUserData" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "userId" TEXT NOT NULL, + "borderUrl" TEXT, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL +); +INSERT INTO "new_ApplicationUserData" ("borderUrl", "createdAt", "id", "updatedAt") SELECT "borderUrl", "createdAt", "id", "updatedAt" FROM "ApplicationUserData"; +DROP TABLE "ApplicationUserData"; +ALTER TABLE "new_ApplicationUserData" RENAME TO "ApplicationUserData"; +CREATE UNIQUE INDEX "ApplicationUserData_userId_key" ON "ApplicationUserData"("userId"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/20220410181303_/migration.sql b/prisma/migrations/20220410181303_/migration.sql new file mode 100644 index 0000000..4ae5074 --- /dev/null +++ b/prisma/migrations/20220410181303_/migration.sql @@ -0,0 +1,23 @@ +/* + Warnings: + + - Added the required column `discordId` to the `ApplicationUserData` table without a default value. This is not possible if the table is not empty. + +*/ +-- RedefineTables +PRAGMA foreign_keys=OFF; +CREATE TABLE "new_ApplicationUserData" ( + "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + "userId" TEXT NOT NULL, + "discordId" TEXT NOT NULL, + "borderUrl" TEXT, + "createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updatedAt" DATETIME NOT NULL +); +INSERT INTO "new_ApplicationUserData" ("borderUrl", "createdAt", "id", "updatedAt", "userId") SELECT "borderUrl", "createdAt", "id", "updatedAt", "userId" FROM "ApplicationUserData"; +DROP TABLE "ApplicationUserData"; +ALTER TABLE "new_ApplicationUserData" RENAME TO "ApplicationUserData"; +CREATE UNIQUE INDEX "ApplicationUserData_userId_key" ON "ApplicationUserData"("userId"); +CREATE UNIQUE INDEX "ApplicationUserData_discordId_key" ON "ApplicationUserData"("discordId"); +PRAGMA foreign_key_check; +PRAGMA foreign_keys=ON; diff --git a/prisma/migrations/migration_lock.toml b/prisma/migrations/migration_lock.toml new file mode 100644 index 0000000..e5e5c47 --- /dev/null +++ b/prisma/migrations/migration_lock.toml @@ -0,0 +1,3 @@ +# Please do not edit this file manually +# It should be added in your version-control system (i.e. Git) +provider = "sqlite" \ No newline at end of file diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 4fa4ca6..f37715e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -10,13 +10,62 @@ datasource db { url = "file:./borders.db" } -model User { - id Int @id @default(autoincrement()) - discordId String @unique - username String - discriminator String - avatar String? - borderUrl String? - createdAt DateTime @default(now()) - updatedAt DateTime @updatedAt +model Account { + id String @id @default(cuid()) + userId String + type String + provider String + providerAccountId String + refresh_token String? + access_token String? + expires_at Int? + token_type String? + scope String? + id_token String? + session_state String? + + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + + @@unique([provider, providerAccountId]) +} + +model Session { + id String @id @default(cuid()) + sessionToken String @unique + userId String + expires DateTime + user User @relation(fields: [userId], references: [id], onDelete: Cascade) +} + +model User { + id String @id @default(cuid()) + name String? + email String? @unique + emailVerified DateTime? + image String? + accounts Account[] + sessions Session[] +} + +model VerificationToken { + identifier String + token String @unique + expires DateTime + + @@unique([identifier, token]) +} + +model ApplicationUserData { + id Int @id @default(autoincrement()) + userId String @unique + discordId String @unique + admin Boolean? + borderId Int? + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt +} + +model BorderImage { + id Int @id @default(autoincrement()) + imageName String @unique } diff --git a/public/favicon.ico b/public/favicon.ico index 718d6fe..8830cf6 100644 Binary files a/public/favicon.ico and b/public/favicon.ico differ diff --git a/public/images/00b9a62e09a1e452d6840170849e8ac06f6d3ef5.png b/public/images/00b9a62e09a1e452d6840170849e8ac06f6d3ef5.png new file mode 100644 index 0000000..c1d73db Binary files /dev/null and b/public/images/00b9a62e09a1e452d6840170849e8ac06f6d3ef5.png differ diff --git a/public/images/083c19cc935001ee0508aff3b948da62b6a093f6.png b/public/images/083c19cc935001ee0508aff3b948da62b6a093f6.png new file mode 100644 index 0000000..cf78abb Binary files /dev/null and b/public/images/083c19cc935001ee0508aff3b948da62b6a093f6.png differ diff --git a/public/images/18717998311ff5e68b1246dbdb43cbc0fa00a35d.png b/public/images/18717998311ff5e68b1246dbdb43cbc0fa00a35d.png new file mode 100644 index 0000000..72f762d Binary files /dev/null and b/public/images/18717998311ff5e68b1246dbdb43cbc0fa00a35d.png differ diff --git a/public/images/27ef3d62a0e42bf4ac60d426f2f172e1790ec0fd.png b/public/images/27ef3d62a0e42bf4ac60d426f2f172e1790ec0fd.png new file mode 100644 index 0000000..40a7ae1 Binary files /dev/null and b/public/images/27ef3d62a0e42bf4ac60d426f2f172e1790ec0fd.png differ diff --git a/public/images/2d876cff5940a8724340a5798c1c8e48880c675f.png b/public/images/2d876cff5940a8724340a5798c1c8e48880c675f.png new file mode 100644 index 0000000..01c4607 Binary files /dev/null and b/public/images/2d876cff5940a8724340a5798c1c8e48880c675f.png differ diff --git a/public/images/359032c610e13a8f370594b200b9ddf8b29aa8af.png b/public/images/359032c610e13a8f370594b200b9ddf8b29aa8af.png new file mode 100644 index 0000000..c592d08 Binary files /dev/null and b/public/images/359032c610e13a8f370594b200b9ddf8b29aa8af.png differ diff --git a/public/images/3fd73db5d33e9b6597e6975eb654e89b89b5db5c.png b/public/images/3fd73db5d33e9b6597e6975eb654e89b89b5db5c.png new file mode 100644 index 0000000..36f3cbe Binary files /dev/null and b/public/images/3fd73db5d33e9b6597e6975eb654e89b89b5db5c.png differ diff --git a/public/images/420efee31f0b0a3fe39cb7409dbe9c3a3e0a0e3a.png b/public/images/420efee31f0b0a3fe39cb7409dbe9c3a3e0a0e3a.png new file mode 100644 index 0000000..c0886d5 Binary files /dev/null and b/public/images/420efee31f0b0a3fe39cb7409dbe9c3a3e0a0e3a.png differ diff --git a/public/images/46461aaea39b18a4a3da2e6d3cf253006f2d6193.png b/public/images/46461aaea39b18a4a3da2e6d3cf253006f2d6193.png new file mode 100644 index 0000000..d043059 Binary files /dev/null and b/public/images/46461aaea39b18a4a3da2e6d3cf253006f2d6193.png differ diff --git a/public/images/4718aad9a4b04c3cfb049bb7cbf2bd35c2e2eb11.png b/public/images/4718aad9a4b04c3cfb049bb7cbf2bd35c2e2eb11.png new file mode 100644 index 0000000..2545d40 Binary files /dev/null and b/public/images/4718aad9a4b04c3cfb049bb7cbf2bd35c2e2eb11.png differ diff --git a/public/images/537ab433b4dbf1c225e5809ad53d690ee73438d8.png b/public/images/537ab433b4dbf1c225e5809ad53d690ee73438d8.png new file mode 100644 index 0000000..20193b2 Binary files /dev/null and b/public/images/537ab433b4dbf1c225e5809ad53d690ee73438d8.png differ diff --git a/public/images/558efce86af3043bda6ac5078e1801dc7b587de7.png b/public/images/558efce86af3043bda6ac5078e1801dc7b587de7.png new file mode 100644 index 0000000..c60368c Binary files /dev/null and b/public/images/558efce86af3043bda6ac5078e1801dc7b587de7.png differ diff --git a/public/images/68439f85ae83ce6429b845234e28de62a88b1f88.png b/public/images/68439f85ae83ce6429b845234e28de62a88b1f88.png new file mode 100644 index 0000000..76793a3 Binary files /dev/null and b/public/images/68439f85ae83ce6429b845234e28de62a88b1f88.png differ diff --git a/public/images/950172e5379f52079835a2b295e93de1d8a8fbf3.png b/public/images/950172e5379f52079835a2b295e93de1d8a8fbf3.png new file mode 100644 index 0000000..0fc8198 Binary files /dev/null and b/public/images/950172e5379f52079835a2b295e93de1d8a8fbf3.png differ diff --git a/public/images/984f18d4bcc76669bcbd8971f962d1b75716dd11.png b/public/images/984f18d4bcc76669bcbd8971f962d1b75716dd11.png new file mode 100644 index 0000000..df17df3 Binary files /dev/null and b/public/images/984f18d4bcc76669bcbd8971f962d1b75716dd11.png differ diff --git a/public/images/babb8c618b2c8eafaad9bc3fac3304a1837ac4fb.png b/public/images/babb8c618b2c8eafaad9bc3fac3304a1837ac4fb.png new file mode 100644 index 0000000..7963340 Binary files /dev/null and b/public/images/babb8c618b2c8eafaad9bc3fac3304a1837ac4fb.png differ diff --git a/public/images/bd04ee3a8e6c35c39f8670c034a5ea7fd15ca3f1.png b/public/images/bd04ee3a8e6c35c39f8670c034a5ea7fd15ca3f1.png new file mode 100644 index 0000000..7d2c9e6 Binary files /dev/null and b/public/images/bd04ee3a8e6c35c39f8670c034a5ea7fd15ca3f1.png differ diff --git a/public/images/beaee5e90d93bfafa5f5f55acb23abfd28ad180c.png b/public/images/beaee5e90d93bfafa5f5f55acb23abfd28ad180c.png new file mode 100644 index 0000000..ce520b0 Binary files /dev/null and b/public/images/beaee5e90d93bfafa5f5f55acb23abfd28ad180c.png differ diff --git a/public/images/c30260bb120bf1379f075802653c8eb86da7a7e9.png b/public/images/c30260bb120bf1379f075802653c8eb86da7a7e9.png new file mode 100644 index 0000000..47732de Binary files /dev/null and b/public/images/c30260bb120bf1379f075802653c8eb86da7a7e9.png differ diff --git a/public/images/e0de1bf39208a3c1230bca5bc69561e9d2f0971f.png b/public/images/e0de1bf39208a3c1230bca5bc69561e9d2f0971f.png new file mode 100644 index 0000000..e89f3c1 Binary files /dev/null and b/public/images/e0de1bf39208a3c1230bca5bc69561e9d2f0971f.png differ diff --git a/public/images/e89b3a70625c980c3d68869f5cdb1da9baa447f8.png b/public/images/e89b3a70625c980c3d68869f5cdb1da9baa447f8.png new file mode 100644 index 0000000..3f5a711 Binary files /dev/null and b/public/images/e89b3a70625c980c3d68869f5cdb1da9baa447f8.png differ diff --git a/public/sky.png b/public/sky.png new file mode 100644 index 0000000..58567fa Binary files /dev/null and b/public/sky.png differ diff --git a/styles/Components.module.css b/styles/Components.module.css new file mode 100644 index 0000000..e69de29