forked from echo-webkom/webathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Add database #42
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
Merged
Merged
Add database #42
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,34 @@ | ||
| "use server"; | ||
| import { getInterestById } from "@/types/interest"; | ||
| import { createClient } from "@/utils/supabase/server"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| export async function addEvent(formData: FormData) { | ||
| const supabase = await createClient(); | ||
| const data = await supabase.auth.getUser(); | ||
|
|
||
| const date = formData.get("date") as string; | ||
| const place = formData.get("place") as string; | ||
| const mapsLink = formData.get("mapsLink") as string; | ||
| const interest = formData.get("interest") as string; | ||
| const description = formData.get("description") as string; | ||
| const user = data.data.user?.id; | ||
|
|
||
| if (!user) { | ||
| console.error("User not authenticated"); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| const interestFull = await getInterestById(interest as unknown as number); | ||
|
|
||
| const { error } = await supabase | ||
| .from("events") | ||
| .insert([{ date, place, mapsLink, interest, description, user }]); | ||
|
|
||
| if (error) { | ||
| console.error("Feil ved innsending:", error.message); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| redirect(`/interests/${interestFull.slug}`); // Redirect to the interest page | ||
| } |
This file contains hidden or 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 @@ | ||
| "use server"; | ||
| import { createClient } from "@/utils/supabase/server"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| export async function addInterest(formData: FormData) { | ||
| const supabase = await createClient(); | ||
|
|
||
| const name = formData.get("name") as string; | ||
| const infinitiv = formData.get("infinitiv") as string; | ||
| const slug = generateSlug(name); | ||
|
|
||
| const { error } = await supabase | ||
| .from("interests") | ||
| .insert([{ name, infinitiv, slug }]); | ||
|
|
||
| if (error) { | ||
| console.error("Feil ved innsending:", error.message); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| redirect("/interests"); | ||
| } | ||
|
|
||
| function generateSlug(text: string) { | ||
| return text | ||
| .toLowerCase() | ||
| .normalize("NFD") | ||
| .replace(/[\u0300-\u036f]/g, "") | ||
| .replace(/[^a-z0-9 ]/g, "") | ||
| .trim() | ||
| .replace(/\s+/g, "-"); | ||
| } |
This file contains hidden or 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,39 @@ | ||
| "use server"; | ||
|
|
||
| import { createClient } from "@/utils/supabase/server"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| export async function addComment(formData: FormData) { | ||
| const supabase = await createClient(); | ||
| const { | ||
| data: { user }, | ||
| } = await supabase.auth.getUser(); | ||
|
|
||
| const content = formData.get("content") as string; | ||
| const event_id = Number(formData.get("event_id")); | ||
|
|
||
| if (!user) { | ||
| console.error("User not authenticated"); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| if (!content || !event_id) { | ||
| console.error("Missing content or event_id"); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| const { error } = await supabase.from("comments").insert([ | ||
| { | ||
| content, | ||
| event_id, | ||
| user_id: user.id, | ||
| }, | ||
| ]); | ||
|
|
||
| if (error) { | ||
| console.error("Feil ved innsending av kommentar:", error.message); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| redirect(`/events/${event_id}`); | ||
| } |
This file contains hidden or 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,48 @@ | ||
| // actions/handleAttendance.ts | ||
| "use server"; | ||
|
|
||
| import { createClient } from "@/utils/supabase/server"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| export async function handleAttendance(formData: FormData) { | ||
| const supabase = await createClient(); | ||
| const { | ||
| data: { user }, | ||
| } = await supabase.auth.getUser(); | ||
|
|
||
| const event_id = Number(formData.get("event_id")); | ||
| const status = formData.get("status") as "skal" | "interessert"; | ||
|
|
||
| if (!user) redirect("/auth/login"); | ||
|
|
||
| const { data: existing } = await supabase | ||
| .from("event_attendance") | ||
| .select("id") | ||
| .eq("event_id", event_id) | ||
| .eq("user_id", user.id) | ||
| .single(); | ||
|
|
||
| let error; | ||
|
|
||
| if (existing) { | ||
| ({ error } = await supabase | ||
| .from("event_attendance") | ||
| .update({ status }) | ||
| .eq("id", existing.id)); | ||
| } else { | ||
| ({ error } = await supabase.from("event_attendance").insert([ | ||
| { | ||
| user_id: user.id, | ||
| event_id, | ||
| status, | ||
| }, | ||
| ])); | ||
| } | ||
|
|
||
| if (error) { | ||
| console.error("Feil ved lagring:", error.message); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| redirect(`/events/${event_id}`); | ||
| } |
This file contains hidden or 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,26 @@ | ||
| "use server"; | ||
|
|
||
| import { createClient } from "@/utils/supabase/server"; | ||
| import { redirect } from "next/navigation"; | ||
| import { revalidatePath } from "next/cache"; | ||
|
|
||
| export async function handleFromBergen(formData: FormData) { | ||
| const supabase = await createClient(); | ||
|
|
||
| const user_id = formData.get("user_id") as string; | ||
| const is_from_bergen = formData.get("is_from_bergen") === "on"; // checkbox | ||
|
|
||
| const { error } = await supabase | ||
| .from("profiles") | ||
| .update({ is_from_bergen }) | ||
| .eq("id", user_id); | ||
|
|
||
| if (error) { | ||
| console.error("Feil ved oppdatering:", error.message); | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| // 💡 Refresh profilside | ||
| revalidatePath("/profile"); | ||
| redirect("/profile"); | ||
| } |
This file contains hidden or 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,31 @@ | ||
| import { Interest } from "@/types/interest"; | ||
| import { createClient } from "@/utils/supabase/client"; | ||
|
|
||
| export async function getInterestBySlugClient(slug: string): Promise<Interest> { | ||
| const supabase = createClient(); | ||
|
|
||
| const { data: interest, error } = await supabase | ||
| .from("interests") | ||
| .select("id, created_at, name, infinitiv, slug") | ||
| .eq("slug", slug) | ||
| .single(); | ||
|
|
||
| if (!interest || error) | ||
| throw new Error(`Interest with slug "${slug}" not found`); | ||
|
|
||
| return interest; | ||
| } | ||
|
|
||
| export async function getInterestByIdClient(id: string): Promise<Interest> { | ||
| const supabase = createClient(); | ||
|
|
||
| const { data: interest, error } = await supabase | ||
| .from("interests") | ||
| .select("id, created_at, name, infinitiv, slug") | ||
| .eq("id", id) | ||
| .single(); | ||
|
|
||
| if (!interest || error) throw new Error(`Interest with id "${id}" not found`); | ||
|
|
||
| return interest; | ||
| } |
This file contains hidden or 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 @@ | ||
| "use server"; | ||
|
|
||
| import { revalidatePath } from "next/cache"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| import { createClient } from "@/utils/supabase/server"; | ||
|
|
||
| export async function login(formData: FormData) { | ||
| const supabase = await createClient(); | ||
|
|
||
| // type-casting here for convenience | ||
| // in practice, you should validate your inputs | ||
| const data = { | ||
| email: formData.get("email") as string, | ||
| password: formData.get("password") as string, | ||
| }; | ||
|
|
||
| const { error } = await supabase.auth.signInWithPassword(data); | ||
|
|
||
| if (error) { | ||
| redirect("/error"); | ||
| } | ||
|
|
||
| if (data.email) { | ||
| console.log("Login successful, redirecting..."); | ||
| revalidatePath("/", "layout"); | ||
| redirect("/"); | ||
| } else { | ||
| console.warn("No user returned after login."); | ||
| redirect("/error"); | ||
| } | ||
| } |
This file contains hidden or 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,28 @@ | ||
| "use server"; | ||
|
|
||
| import { revalidatePath } from "next/cache"; | ||
| import { redirect } from "next/navigation"; | ||
|
|
||
| import { createClient } from "@/utils/supabase/server"; | ||
|
|
||
| export async function register(formData: FormData) { | ||
| const supabase = await createClient(); | ||
| // type-casting here for convenience | ||
| // in practice, you should validate your inputs | ||
| const data = { | ||
| email: formData.get("email") as string, | ||
| password: formData.get("password") as string, | ||
| options: { | ||
| data: { | ||
| full_name: formData.get("fullName") as string, | ||
| username: formData.get("username") as string, | ||
| }, | ||
| }, | ||
| }; | ||
| const { error } = await supabase.auth.signUp(data); | ||
| if (error) { | ||
| redirect("/error"); | ||
| } | ||
| revalidatePath("/", "layout"); | ||
| redirect("/"); | ||
| } |
This file contains hidden or 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,21 @@ | ||
| "use server"; | ||
|
|
||
| import { redirect } from "next/navigation"; | ||
|
|
||
| import { createClient } from "@/utils/supabase/server"; | ||
|
|
||
| export async function logout() { | ||
| try { | ||
| const supabase = await createClient(); | ||
| const { error } = await supabase.auth.signOut(); | ||
| if (error) { | ||
| console.error("Logout error:", error.message); | ||
| redirect("/error"); | ||
| return; | ||
| } | ||
| // Ingen direkte omdirigering her, vi lar klienten håndtere det | ||
| } catch (err) { | ||
| console.error("Unexpected error during logout:", err); | ||
| redirect("/error"); | ||
| } | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,35 @@ | ||
| import { type EmailOtpType } from "@supabase/supabase-js"; | ||
| import { type NextRequest, NextResponse } from "next/server"; | ||
|
|
||
| import { createClient } from "@/utils/supabase/server"; | ||
|
|
||
| // Creating a handler to a GET request to route /auth/confirm | ||
| export async function GET(request: NextRequest) { | ||
| const { searchParams } = new URL(request.url); | ||
| const token_hash = searchParams.get("token_hash"); | ||
| const type = searchParams.get("type") as EmailOtpType | null; | ||
| const next = "/account"; | ||
|
|
||
| // Create redirect link without the secret token | ||
| const redirectTo = request.nextUrl.clone(); | ||
| redirectTo.pathname = next; | ||
| redirectTo.searchParams.delete("token_hash"); | ||
| redirectTo.searchParams.delete("type"); | ||
|
|
||
| if (token_hash && type) { | ||
| const supabase = await createClient(); | ||
|
|
||
| const { error } = await supabase.auth.verifyOtp({ | ||
| type, | ||
| token_hash, | ||
| }); | ||
| if (!error) { | ||
| redirectTo.searchParams.delete("next"); | ||
| return NextResponse.redirect(redirectTo); | ||
| } | ||
| } | ||
|
|
||
| // return the user to an error page with some instructions | ||
| redirectTo.pathname = "/error"; | ||
| return NextResponse.redirect(redirectTo); | ||
| } |
This file contains hidden or 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,41 @@ | ||
| import { login } from "@/app/actions/login"; | ||
| import Link from "next/link"; | ||
|
|
||
| export default function LoginPage() { | ||
| return ( | ||
| <div className="flex items-center justify-center mt-12 flex-col"> | ||
| <form className="flex flex-col gap-4 items-center w-72"> | ||
| <h1 className="text-2xl font-bold mb-8">Logg inn</h1> | ||
| <input | ||
| className="border p-2 rounded w-full" | ||
| id="email" | ||
| name="email" | ||
| type="email" | ||
| placeholder="E-post" | ||
| required | ||
| /> | ||
| <input | ||
| className="border p-2 rounded w-full" | ||
| id="password" | ||
| name="password" | ||
| type="password" | ||
| placeholder="Passord" | ||
| required | ||
| /> | ||
| <button | ||
| formAction={login} | ||
| className="bg-black text-white px-4 py-2 rounded w-full" | ||
| type="submit" | ||
| > | ||
| Logg inn | ||
| </button> | ||
| </form> | ||
| <p className="mt-4"> | ||
| Er du ikke kompis enda?¿ {""} | ||
| <Link href="/auth/register" className="hover:underline"> | ||
| Registrer deg | ||
| </Link> | ||
| </p> | ||
| </div> | ||
| ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
norsk