(https://carlosazaustre.es/)",
"license": "MIT",
diff --git a/pages/[shortId].js b/pages/[shortId].js
index b181861..8c4b5cc 100644
--- a/pages/[shortId].js
+++ b/pages/[shortId].js
@@ -1,4 +1,4 @@
-import db from "@/lib/client";
+import { getURLbyShortLink, updateShortLinkClicks } from "@/lib/db";
export default function ShortIdPage() {
return ShortID Redirect
;
@@ -6,19 +6,19 @@ export default function ShortIdPage() {
export async function getServerSideProps({ params }) {
const { shortId } = params;
-
- const data = await db.link.findUnique({
- where: { shortUrl: shortId },
- });
+ const data = await getURLbyShortLink(shortId);
if (!data) {
- return { redirect: { destination: "/" } };
+ return {
+ redirect: { destination: "/" },
+ };
}
- await db.link.update({
- where: { shortUrl: shortId },
- data: { clicks: data.clicks + 1 },
- });
+ await updateShortLinkClicks(shortId, data);
- return { redirect: { destination: data?.url || "/" } };
+ return {
+ redirect: {
+ destination: data.url,
+ },
+ };
}
diff --git a/pages/_app.js b/pages/_app.js
index 3609eb0..4a3bf33 100644
--- a/pages/_app.js
+++ b/pages/_app.js
@@ -1,12 +1,16 @@
import "@/styles/fonts";
+import { UserProvider } from "@auth0/nextjs-auth0";
+
import theme, { ThemeProvider } from "@/styles/theme";
function MyApp({ Component, pageProps }) {
return (
-
-
-
+
+
+
+
+
);
}
diff --git a/pages/api/auth/[...auth0].js b/pages/api/auth/[...auth0].js
new file mode 100644
index 0000000..a605c53
--- /dev/null
+++ b/pages/api/auth/[...auth0].js
@@ -0,0 +1,23 @@
+import { handleAuth, handleCallback } from "@auth0/nextjs-auth0";
+
+import { createUser } from "@/lib/db";
+
+const afterCallback = async (_req, _res, session) => {
+ try {
+ await createUser(session.user.email);
+ } catch (error) {
+ console.error(error);
+ }
+
+ return session;
+};
+
+export default handleAuth({
+ async callback(req, res) {
+ try {
+ await handleCallback(req, res, { afterCallback });
+ } catch (error) {
+ res.status(error.status || 500).send(error.message);
+ }
+ },
+});
diff --git a/pages/api/shortUrl.js b/pages/api/shortUrl.js
index 2d97e27..e1c2461 100644
--- a/pages/api/shortUrl.js
+++ b/pages/api/shortUrl.js
@@ -1,14 +1,17 @@
-import db from "@/lib/client";
+import { getSession } from "@auth0/nextjs-auth0";
+
+import { createShortLink, getUserByEmail } from "@/lib/db";
export default async function handler(req, res) {
+ const { user } = await getSession(req, res);
const { url } = req.body;
const shortUrl = Math.random().toString(36).substr(2, 7);
try {
- const data = await db.link.create({
- data: { url, shortUrl },
- });
- console.log(data);
+ const userDB = await getUserByEmail(user.email);
+ console.log("user data", userDB);
+ const data = await createShortLink(url, shortUrl, userDB.id);
+
return res.status(200).json({ data });
} catch (err) {
console.log(err);
diff --git a/pages/dashboard.js b/pages/dashboard.js
new file mode 100644
index 0000000..62ddfa7
--- /dev/null
+++ b/pages/dashboard.js
@@ -0,0 +1,97 @@
+import { getSession, withPageAuthRequired } from "@auth0/nextjs-auth0";
+import {
+ CalendarIcon,
+ DownloadIcon,
+ ExternalLinkIcon,
+ LinkIcon,
+} from "@chakra-ui/icons";
+import {
+ Box,
+ Center,
+ Link,
+ Table,
+ TableCaption,
+ Tbody,
+ Td,
+ Th,
+ Thead,
+ Tr,
+} from "@chakra-ui/react";
+import Head from "next/head";
+
+import { Footer, Nav } from "@/components";
+import { formatDate } from "@/lib/date";
+import { getUserByEmail } from "@/lib/db";
+
+export default function Dashboard({ links }) {
+ return (
+
+
+
Self-Made URL Shortener
+
+
+
+
+
+
+
+
+
+ List of all my short links
+
+
+
+ Created |
+ ShortLink |
+ URL |
+ Clicks |
+
+
+
+ {links.map((link) => (
+
+
+
+ {formatDate(link.createdAt)}
+ |
+
+
+
+ czstr.link/{link.shortUrl}
+
+ |
+
+
+ {link.url}
+
+
+ |
+
+
+ {link.clicks}
+ |
+
+ ))}
+
+
+
+
+
+
+ );
+}
+
+export const getServerSideProps = withPageAuthRequired({
+ async getServerSideProps({ req, res }) {
+ const {
+ user: { email },
+ } = await getSession(req, res);
+
+ const { links } = await getUserByEmail(email);
+
+ return { props: { links: JSON.parse(JSON.stringify(links)) } };
+ },
+});
diff --git a/pages/index.js b/pages/index.js
index 1dee1e8..9bf4e0f 100644
--- a/pages/index.js
+++ b/pages/index.js
@@ -1,9 +1,14 @@
-import { Center } from "@chakra-ui/react";
+import { useUser } from "@auth0/nextjs-auth0";
+import { Center, LinkBox, LinkOverlay } from "@chakra-ui/react";
import Head from "next/head";
-import { Footer, FormAdd, Header } from "@/components";
+import { Footer, FormAdd, Header, Nav } from "@/components";
export default function Home() {
+ const { user, error } = useUser();
+
+ if (error) return Error: {error}
;
+
return (
@@ -12,10 +17,30 @@ export default function Home() {
+
-
+ {user ? (
+
+ ) : (
+
+
+ Start creating your shortlinks
+
+
+ )}
diff --git a/prisma/migrations/20210709153322_add_user_table/migration.sql b/prisma/migrations/20210709153322_add_user_table/migration.sql
new file mode 100644
index 0000000..a81baa1
--- /dev/null
+++ b/prisma/migrations/20210709153322_add_user_table/migration.sql
@@ -0,0 +1,23 @@
+/*
+ Warnings:
+
+ - Added the required column `userId` to the `Link` table without a default value. This is not possible if the table is not empty.
+
+*/
+-- AlterTable
+ALTER TABLE "Link" ADD COLUMN "userId" INTEGER NOT NULL;
+
+-- CreateTable
+CREATE TABLE "User" (
+ "id" SERIAL NOT NULL,
+ "email" TEXT NOT NULL,
+ "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
+
+ PRIMARY KEY ("id")
+);
+
+-- CreateIndex
+CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");
+
+-- AddForeignKey
+ALTER TABLE "Link" ADD FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE;
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index 291e54f..bde4ffb 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -10,10 +10,19 @@ generator client {
provider = "prisma-client-js"
}
+model User {
+ id Int @id @default(autoincrement())
+ email String @unique
+ links Link[]
+ createdAt DateTime @default(now())
+}
+
model Link {
- id Int @id @default(autoincrement())
- url String @unique
- createdAt DateTime @default(now())
- shortUrl String @unique
- clicks Int @default(0)
+ id Int @id @default(autoincrement())
+ url String @unique
+ createdAt DateTime @default(now())
+ shortUrl String @unique
+ clicks Int @default(0)
+ user User @relation(fields: [userId], references: [id])
+ userId Int
}
diff --git a/public/czstr-link-logo.png b/public/czstr-link-logo.png
new file mode 100644
index 0000000..7cd4154
Binary files /dev/null and b/public/czstr-link-logo.png differ