From 87fc9f2fb9b33256ae9242296c924c5702d05b7e Mon Sep 17 00:00:00 2001 From: TacticalCoderJay Date: Sun, 30 Oct 2022 21:42:39 -0700 Subject: [PATCH] feat: v3.6.0-rc4 (#207) * feat: oauth reform for potential improvements * fix: update avatars with new pfp * fix: remove redundant include * feat: v3.6.0-rc4 Co-authored-by: dicedtomato * fix: remove console log Co-authored-by: dicedtomato --- next.config.js | 4 +- package.json | 2 +- .../20221030222208_oauth_reform/migration.sql | 31 ++++ .../migration.sql | 5 + .../20221030233448_fix_images/migration.sql | 8 + .../migration.sql | 2 + .../migration.sql | 8 + prisma/schema.prisma | 69 +++++---- src/components/Layout.tsx | 59 +++++--- src/components/icons/DiscordIcon.tsx | 20 ++- src/components/icons/GitHubIcon.tsx | 16 +- src/components/icons/GoogleIcon.tsx | 15 ++ src/components/icons/index.tsx | 2 + src/components/pages/Manage/index.tsx | 38 +++-- src/lib/config/Config.ts | 3 + src/lib/config/readConfig.ts | 3 + src/lib/config/validateConfig.ts | 3 + src/lib/middleware/getServerSideProps.ts | 8 + src/lib/middleware/withOAuth.ts | 143 ++++++++++++++++++ src/lib/middleware/withZipline.ts | 21 ++- src/lib/{oauth/index.ts => oauth.ts} | 17 +++ src/lib/recoil/user.ts | 4 +- src/lib/utils/client.ts | 4 + src/pages/api/auth/login.ts | 7 +- src/pages/api/auth/oauth/discord.ts | 125 ++++----------- src/pages/api/auth/oauth/github.ts | 115 ++++---------- src/pages/api/auth/oauth/google.ts | 65 ++++++++ src/pages/api/auth/oauth/index.ts | 34 +++-- src/pages/api/user/[id].ts | 1 + src/pages/api/user/index.ts | 116 +++++++++++--- src/pages/api/users.ts | 2 +- src/pages/auth/login.tsx | 33 ++-- 32 files changed, 683 insertions(+), 300 deletions(-) create mode 100644 prisma/migrations/20221030222208_oauth_reform/migration.sql create mode 100644 prisma/migrations/20221030224830_oauth_fix_refresh/migration.sql create mode 100644 prisma/migrations/20221030233448_fix_images/migration.sql create mode 100644 prisma/migrations/20221031011932_oauth_add_google/migration.sql create mode 100644 prisma/migrations/20221031041758_oauth_username/migration.sql create mode 100644 src/components/icons/GoogleIcon.tsx create mode 100644 src/lib/middleware/withOAuth.ts rename src/lib/{oauth/index.ts => oauth.ts} (61%) create mode 100644 src/pages/api/auth/oauth/google.ts diff --git a/next.config.js b/next.config.js index 6097f79e9..ac462823b 100644 --- a/next.config.js +++ b/next.config.js @@ -17,8 +17,8 @@ module.exports = { 'getsharex.com', // For flameshot icon, and maybe in the future other stuff from github 'raw.githubusercontent.com', - // Discord Icon - 'assets-global.website-files.com', + // Google Icon + 'madeby.google.com', ], }, poweredByHeader: false, diff --git a/package.json b/package.json index c1d9da748..c43718bc2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "zipline", - "version": "3.6.0-rc3", + "version": "3.6.0-rc4", "license": "MIT", "scripts": { "dev": "npm-run-all build:server dev:run", diff --git a/prisma/migrations/20221030222208_oauth_reform/migration.sql b/prisma/migrations/20221030222208_oauth_reform/migration.sql new file mode 100644 index 000000000..5761ff689 --- /dev/null +++ b/prisma/migrations/20221030222208_oauth_reform/migration.sql @@ -0,0 +1,31 @@ +/* + Warnings: + + - You are about to drop the column `oauth` on the `User` table. All the data in the column will be lost. + - You are about to drop the column `oauthAccessToken` on the `User` table. All the data in the column will be lost. + - You are about to drop the column `oauthProvider` on the `User` table. All the data in the column will be lost. + +*/ +-- CreateEnum +CREATE TYPE "OauthProviders" AS ENUM ('DISCORD', 'GITHUB'); + +-- AlterTable +ALTER TABLE "User" DROP COLUMN "oauth", +DROP COLUMN "oauthAccessToken", +DROP COLUMN "oauthProvider"; + +-- CreateTable +CREATE TABLE "OAuth" ( + "id" SERIAL NOT NULL, + "provider" "OauthProviders" NOT NULL, + "userId" INTEGER NOT NULL, + "token" TEXT NOT NULL, + + CONSTRAINT "OAuth_pkey" PRIMARY KEY ("id") +); + +-- CreateIndex +CREATE UNIQUE INDEX "OAuth_provider_key" ON "OAuth"("provider"); + +-- AddForeignKey +ALTER TABLE "OAuth" ADD CONSTRAINT "OAuth_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/migrations/20221030224830_oauth_fix_refresh/migration.sql b/prisma/migrations/20221030224830_oauth_fix_refresh/migration.sql new file mode 100644 index 000000000..f2f720c8c --- /dev/null +++ b/prisma/migrations/20221030224830_oauth_fix_refresh/migration.sql @@ -0,0 +1,5 @@ +-- DropIndex +DROP INDEX "OAuth_provider_key"; + +-- AlterTable +ALTER TABLE "OAuth" ADD COLUMN "refresh" TEXT; diff --git a/prisma/migrations/20221030233448_fix_images/migration.sql b/prisma/migrations/20221030233448_fix_images/migration.sql new file mode 100644 index 000000000..28aec5d26 --- /dev/null +++ b/prisma/migrations/20221030233448_fix_images/migration.sql @@ -0,0 +1,8 @@ +-- DropForeignKey +ALTER TABLE "Image" DROP CONSTRAINT "Image_userId_fkey"; + +-- AlterTable +ALTER TABLE "Image" ALTER COLUMN "userId" DROP NOT NULL; + +-- AddForeignKey +ALTER TABLE "Image" ADD CONSTRAINT "Image_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE SET NULL ON UPDATE CASCADE; diff --git a/prisma/migrations/20221031011932_oauth_add_google/migration.sql b/prisma/migrations/20221031011932_oauth_add_google/migration.sql new file mode 100644 index 000000000..c72dfcc7f --- /dev/null +++ b/prisma/migrations/20221031011932_oauth_add_google/migration.sql @@ -0,0 +1,2 @@ +-- AlterEnum +ALTER TYPE "OauthProviders" ADD VALUE 'GOOGLE'; diff --git a/prisma/migrations/20221031041758_oauth_username/migration.sql b/prisma/migrations/20221031041758_oauth_username/migration.sql new file mode 100644 index 000000000..ce8b47dcd --- /dev/null +++ b/prisma/migrations/20221031041758_oauth_username/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - Added the required column `username` to the `OAuth` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE "OAuth" ADD COLUMN "username" TEXT NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 1b180ea9c..7af859007 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -8,25 +8,23 @@ generator client { } model User { - id Int @id @default(autoincrement()) - username String - password String? - oauth Boolean @default(false) - oauthProvider String? - oauthAccessToken String? - avatar String? - token String - administrator Boolean @default(false) - superAdmin Boolean @default(false) - systemTheme String @default("system") - embedTitle String? - embedColor String @default("#2f3136") - embedSiteName String? @default("{image.file} • {user.name}") - ratelimit DateTime? - domains String[] - images Image[] - urls Url[] - Invite Invite[] + id Int @id @default(autoincrement()) + username String + password String? + avatar String? + token String + administrator Boolean @default(false) + superAdmin Boolean @default(false) + systemTheme String @default("system") + embedTitle String? + embedColor String @default("#2f3136") + embedSiteName String? @default("{image.file} • {user.name}") + ratelimit DateTime? + domains String[] + oauth OAuth[] + images Image[] + urls Url[] + Invite Invite[] } enum ImageFormat { @@ -49,8 +47,8 @@ model Image { password String? invisible InvisibleImage? format ImageFormat @default(RANDOM) - user User @relation(fields: [userId], references: [id]) - userId Int + user User? @relation(fields: [userId], references: [id], onDelete: SetNull) + userId Int? } model InvisibleImage { @@ -86,12 +84,27 @@ model Stats { } model Invite { - id Int @id @default(autoincrement()) - code String @unique - created_at DateTime @default(now()) - expires_at DateTime? - used Boolean @default(false) - - createdBy User @relation(fields: [createdById], references: [id]) + id Int @id @default(autoincrement()) + code String @unique + created_at DateTime @default(now()) + expires_at DateTime? + used Boolean @default(false) + createdBy User @relation(fields: [createdById], references: [id]) createdById Int } + +model OAuth { + id Int @id @default(autoincrement()) + provider OauthProviders + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + userId Int + username String + token String + refresh String? +} + +enum OauthProviders { + DISCORD + GITHUB + GOOGLE +} diff --git a/src/components/Layout.tsx b/src/components/Layout.tsx index 8ee19786b..788c20563 100644 --- a/src/components/Layout.tsx +++ b/src/components/Layout.tsx @@ -29,6 +29,7 @@ import { showNotification } from '@mantine/notifications'; import useFetch from 'hooks/useFetch'; import { useVersion } from 'lib/queries/version'; import { userSelector } from 'lib/recoil/user'; +import { capitalize } from 'lib/utils/client'; import { useRecoilState } from 'recoil'; import Link from 'next/link'; import { useRouter } from 'next/router'; @@ -52,6 +53,7 @@ import { UserIcon, DiscordIcon, GitHubIcon, + GoogleIcon, } from './icons'; import { friendlyThemeName, themes } from './Theming'; @@ -161,7 +163,18 @@ const admin_items = [ export default function Layout({ children, props }) { const [user, setUser] = useRecoilState(userSelector); - const { title } = props; + const { title, oauth_providers: unparsed } = props; + const oauth_providers = JSON.parse(unparsed); + const icons = { + GitHub: GitHubIcon, + Discord: DiscordIcon, + Google: GoogleIcon, + }; + + for (const provider of oauth_providers) { + provider.Icon = icons[provider.name]; + } + const external_links = JSON.parse(props.external_links ?? '[]'); const [token, setToken] = useState(user?.token); @@ -393,24 +406,34 @@ export default function Layout({ children, props }) { Logout - {user.oauth ? ( - <> - - ) : ( - - ) - } - > - Logged in with{' '} - {user.oauthProvider} - - + <> + {oauth_providers + .filter((x) => + user.oauth + ?.map(({ provider }) => provider.toLowerCase()) + .includes(x.name.toLowerCase()) + ) + .map(({ name, Icon }, i) => ( + <> + } + > + Logged in with {capitalize(name)} + + + ))} + {oauth_providers.filter((x) => + user.oauth + ?.map(({ provider }) => provider.toLowerCase()) + .includes(x.name.toLowerCase()) + ).length ? ( - - ) : null} + ) : null} + }>