Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/auth #18

Merged
merged 5 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@radix-ui/react-tabs": "^1.0.4",
"@radix-ui/react-toast": "^1.1.5",
"@radix-ui/react-tooltip": "^1.1.2",
"@supabase/ssr": "^0.5.1",
"@supabase/supabase-js": "^2.43.4",
"@t3-oss/env-nextjs": "^0.10.1",
"@tabler/icons-react": "^3.5.0",
Expand Down
33 changes: 33 additions & 0 deletions src/lib/supabaseSSR.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createServerClient, serializeCookieHeader } from "@supabase/ssr";
import { type GetServerSidePropsContext } from "next";

import { env } from "@/env";

import type { Database } from "./types";

export function createSSRClient({ req, res }: GetServerSidePropsContext) {
const supabase = createServerClient<Database>(
env.NEXT_PUBLIC_SUPABASE_URL,
env.NEXT_PUBLIC_SUPABASE_ANON_KEY,
{
cookies: {
getAll() {
return Object.keys(req.cookies).map((name) => ({
name,
value: req.cookies[name] ?? "",
}));
},
setAll(cookiesToSet) {
res.setHeader(
"Set-Cookie",
cookiesToSet.map(({ name, value, options }) =>
serializeCookieHeader(name, value, options),
),
);
},
},
},
);

return supabase;
}
63 changes: 52 additions & 11 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
export type Json =
| Json[]
| boolean
| number
| string
| number
| boolean
| null
| { [key: string]: Json | undefined }
| null;
| Json[];

export interface Database {
export type Database = {
public: {
Tables: {
blocks: {
Expand Down Expand Up @@ -56,39 +56,62 @@ export interface Database {
};
events: {
Row: {
closingRegistrationAt: string | null;
content: Json | null;
createdAt: string;
description: string | null;
displayOnEverySection: boolean | null;
eventDate: string | null;
eventId: string;
name: string;
openingRegistrationAt: string | null;
organizerName: string;
ownersSlug: string;
ownerUUID: string | null;
participantsSlug: string;
updatedAt: string | null;
usersSlug: string;
};
Insert: {
closingRegistrationAt?: string | null;
content?: Json | null;
createdAt?: string;
description?: string | null;
displayOnEverySection?: boolean | null;
eventDate?: string | null;
eventId?: string;
name: string;
openingRegistrationAt?: string | null;
organizerName: string;
ownersSlug: string;
ownerUUID?: string | null;
participantsSlug: string;
updatedAt?: string | null;
usersSlug: string;
};
Update: {
closingRegistrationAt?: string | null;
content?: Json | null;
createdAt?: string;
description?: string | null;
displayOnEverySection?: boolean | null;
eventDate?: string | null;
eventId?: string;
name?: string;
openingRegistrationAt?: string | null;
organizerName?: string;
ownersSlug?: string;
ownerUUID?: string | null;
participantsSlug?: string;
updatedAt?: string | null;
usersSlug?: string;
};
Relationships: [];
Relationships: [
{
foreignKeyName: "events_ownerUUID_fkey";
columns: ["ownerUUID"];
isOneToOne: false;
referencedRelation: "users";
referencedColumns: ["id"];
},
];
};
reservations: {
Row: {
Expand Down Expand Up @@ -133,7 +156,25 @@ export interface Database {
[_ in never]: never;
};
Functions: {
[_ in never]: never;
generate_unique_slug: {
Args: {
event_name: string;
};
Returns: string;
};
get_section_data: {
Args: {
section_uuid: string;
};
Returns: {
lp: number;
imie: string;
nazwisko: string;
data_dodania: string;
sekcja_koncowa: string;
drzewko_sekcji: string;
}[];
};
};
Enums: {
[_ in never]: never;
Expand All @@ -142,7 +183,7 @@ export interface Database {
[_ in never]: never;
};
};
}
};

type PublicSchema = Database[Extract<keyof Database, "public">];

Expand Down
3 changes: 1 addition & 2 deletions src/lib/useCreateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ export const useCreateEvent = () => {
description: "",
organizerName: "",
ownersSlug: v4(),
usersSlug: v4(),
participantsSlug: v4(),
eventId: v4(),
})
.select("*")
.limit(1)
.single()
.throwOnError();

if (!event.data) {
throw new Error("Event creation failed");
}
Expand Down
32 changes: 30 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import i18next from "i18next";
import type { AppProps } from "next/app";
import { Space_Grotesk } from "next/font/google";
import Head from "next/head";
import { useState } from "react";
import { Suspense, useEffect, useState } from "react";
import { v4 } from "uuid";
import { z } from "zod";
import { zodI18nMap } from "zod-i18n-map";
import translation from "zod-i18n-map/locales/pl/zod.json";

import { Toaster } from "@/components/ui/sonner";
import { ThemeProvider } from "@/components/ui/theme-provider";
import { supabase } from "@/lib/supabase";
import "@/styles/globals.css";

setDefaultOptions({ locale: pl });
Expand Down Expand Up @@ -50,14 +52,40 @@ const ReactQueryClientProvider = ({
};

export default function App({ Component, pageProps }: AppProps) {
useEffect(() => {
const signUp = async () => {
const session = await supabase.auth.getSession();
if (!session.data.session) {
await supabase.auth.signUp({
email: `${v4()}@eventownik.solvro.pl`,
password: v4(),
});
}
};

void signUp();

const listener = supabase.auth.onAuthStateChange((_, session) => {
if (!session) {
void signUp();
}
});

return () => {
listener.data.subscription.unsubscribe();
};
}, []);

return (
<ThemeProvider>
<ReactQueryClientProvider>
<Head>
<title>Eventownik</title>
</Head>
<main className={spaceGrotesk.className}>
<Component {...pageProps} />
<Suspense fallback={null}>
<Component {...pageProps} />
</Suspense>
<Toaster />
</main>
</ReactQueryClientProvider>
Expand Down
3 changes: 2 additions & 1 deletion src/pages/event/[slug]/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { supabase } from "@/lib/supabase";
import { createSSRClient } from "@/lib/supabaseSSR";
import type { Tables, TablesInsert } from "@/lib/types";
import { useEvent } from "@/lib/useEvent";
import { useZodForm } from "@/lib/useZodForm";
Expand Down Expand Up @@ -361,7 +362,7 @@ export const getServerSideProps = async (
) => {
const slug = z.string().uuid().parse(ctx.params.slug);

const event = await supabase
const event = await createSSRClient(ctx)
.from("events")
.select("*")
.eq("ownersSlug", slug)
Expand Down
7 changes: 4 additions & 3 deletions src/pages/event/[slug]/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Textarea } from "@/components/ui/textarea";
import { supabase } from "@/lib/supabase";
import { createSSRClient } from "@/lib/supabaseSSR";
import type { TablesUpdate } from "@/lib/types";
import { useEvent } from "@/lib/useEvent";
import { useZodForm } from "@/lib/useZodForm";
Expand Down Expand Up @@ -266,13 +267,13 @@ export default function Dashboard({
type="url"
disabled={true}
className="cursor-copy"
value={`https://eventownik.solvro.pl/event/${event.data?.usersSlug}`}
value={`https://eventownik.solvro.pl/event/${event.data?.participantsSlug}`}
/>
<Button
onClick={() => {
void navigator.clipboard
.writeText(
`https://eventownik.solvro.pl/event/${event.data?.usersSlug}`,
`https://eventownik.solvro.pl/event/${event.data?.participantsSlug}`,
)
.then(() => {
toast("Link skopiowany do schowka");
Expand Down Expand Up @@ -347,7 +348,7 @@ export const getServerSideProps = async (
) => {
const slug = z.string().uuid().parse(ctx.params.slug);

const event = await supabase
const event = await createSSRClient(ctx)
.from("events")
.select("*")
.eq("ownersSlug", slug)
Expand Down
Loading