Skip to content

Commit

Permalink
chore: migrate to v7 safe-action
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorpfiz committed Aug 1, 2024
1 parent df5f64a commit 7f4772c
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 23 deletions.
2 changes: 1 addition & 1 deletion apps/nextjs/src/components/auth/card-wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Card, CardContent, CardFooter, CardHeader } from "@wellchart/ui/card";
import { Card, CardContent, CardFooter, CardHeader } from "@hyper/ui/card";

import { BackButton } from "~/components/auth/back-button";
import { Header } from "~/components/auth/header";
Expand Down
10 changes: 5 additions & 5 deletions apps/nextjs/src/components/auth/sign-in-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { useAction } from "next-safe-action/hooks";

import type { SignInSchemaType } from "@wellchart/validators";
import { Button } from "@wellchart/ui/button";
import type { SignInSchemaType } from "@hyper/validators";
import { Button } from "@hyper/ui/button";
import {
Form,
FormControl,
Expand All @@ -12,9 +12,9 @@ import {
FormLabel,
FormMessage,
useForm,
} from "@wellchart/ui/form";
import { Input } from "@wellchart/ui/input";
import { SignInSchema } from "@wellchart/validators";
} from "@hyper/ui/form";
import { Input } from "@hyper/ui/input";
import { SignInSchema } from "@hyper/validators";

import { FormError } from "~/components/auth/form-error";
import { signInWithPassword } from "~/lib/actions/auth";
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/components/auth/sign-out-button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button } from "@wellchart/ui/button";
import { Button } from "@hyper/ui/button";

import { signOut } from "~/lib/actions/auth";

Expand Down
10 changes: 5 additions & 5 deletions apps/nextjs/src/components/auth/sign-up-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { useAction } from "next-safe-action/hooks";

import type { SignUpSchemaType } from "@wellchart/validators";
import { Button } from "@wellchart/ui/button";
import type { SignUpSchemaType } from "@hyper/validators";
import { Button } from "@hyper/ui/button";
import {
Form,
FormControl,
Expand All @@ -12,9 +12,9 @@ import {
FormLabel,
FormMessage,
useForm,
} from "@wellchart/ui/form";
import { Input } from "@wellchart/ui/input";
import { SignUpSchema } from "@wellchart/validators";
} from "@hyper/ui/form";
import { Input } from "@hyper/ui/input";
import { SignUpSchema } from "@hyper/validators";

import { FormError } from "~/components/auth/form-error";
import { FormSuccess } from "~/components/auth/form-success";
Expand Down
2 changes: 1 addition & 1 deletion apps/nextjs/src/components/auth/social.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FcGoogle } from "react-icons/fc";

import { Button } from "@wellchart/ui/button";
import { Button } from "@hyper/ui/button";

import { signInWithGoogle } from "~/lib/actions/auth";

Expand Down
99 changes: 99 additions & 0 deletions apps/nextjs/src/lib/actions/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"use server";

import { revalidatePath } from "next/cache";
import { headers } from "next/headers";
import { redirect } from "next/navigation";

import { SignInSchema, SignUpSchema } from "@hyper/validators";

import { DEFAULT_LOGIN_REDIRECT } from "~/config/routes";
import { actionClient } from "~/lib/safe-action";
import { createClient } from "~/utils/supabase/server";

export const signInWithPassword = actionClient
.schema(SignInSchema)
.action(async ({ parsedInput: { email, password } }) => {
const supabase = createClient();

const { error } = await supabase.auth.signInWithPassword({
email,
password,
});

if (error) {
throw error;
}

revalidatePath("/", "layout");
redirect(DEFAULT_LOGIN_REDIRECT);
});

export const signUp = actionClient
.schema(SignUpSchema)
.action(async ({ parsedInput: { email, password } }) => {
const origin = headers().get("origin");
const supabase = createClient();

const redirectUrl = `${origin}/auth/confirm?next=${encodeURIComponent(DEFAULT_LOGIN_REDIRECT)}`;

const { error, data } = await supabase.auth.signUp({
email,
password,
options: {
emailRedirectTo: redirectUrl,
},
});

// User already exists, so fake data is returned. See https://supabase.com/docs/reference/javascript/auth-signup
if (data.user?.identities && data.user.identities.length === 0) {
throw new Error("An error occurred. Please try again.");
}

if (error) {
throw error;
}

return data.user;
});

export const signInWithGithub = async () => {
const origin = headers().get("origin");
const supabase = createClient();

const res = await supabase.auth.signInWithOAuth({
provider: "github",
options: { redirectTo: `${origin}/auth/callback` },
});

if (res.data.url) {
redirect(res.data.url);
}
if (res.error) {
throw new Error(res.error.message);
}
};

export const signInWithGoogle = async () => {
const origin = headers().get("origin");
const supabase = createClient();

const redirectUrl = `${origin}/auth/callback?next=${encodeURIComponent(DEFAULT_LOGIN_REDIRECT)}`;

const res = await supabase.auth.signInWithOAuth({
provider: "google",
options: { redirectTo: redirectUrl },
});

if (res.data.url) {
redirect(res.data.url);
}
if (res.error) {
throw new Error(res.error.message);
}
};

export const signOut = async () => {
const supabase = createClient();
await supabase.auth.signOut();
redirect("/");
};
21 changes: 11 additions & 10 deletions apps/nextjs/src/lib/safe-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ import { createSafeActionClient } from "next-safe-action";

import { createClient } from "~/utils/supabase/server";

export const action = createSafeActionClient();
// Base client
export const actionClient = createSafeActionClient();

export const authAction = createSafeActionClient({
async middleware() {
const supabase = createClient();
const { data, error } = await supabase.auth.getUser();
// Auth client
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const authActionClient = actionClient.use(async ({ next, ctx }) => {
const supabase = createClient();
const { data, error } = await supabase.auth.getUser();

if (error ?? !data.user) {
throw new Error("Unauthorized");
}
if (error ?? !data.user) {
throw new Error("Unauthorized");
}

return { user: data.user };
},
return next({ ctx: { user: data.user } });
});

0 comments on commit 7f4772c

Please sign in to comment.