Skip to content

Commit

Permalink
split actions into 2
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge committed Dec 14, 2023
1 parent 4f56dce commit 5506b75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
38 changes: 20 additions & 18 deletions apps/nextjs/src/app/auth/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,35 @@ import { cookies, headers } from "next/headers";
import { redirect } from "next/navigation";
import { createServerActionClient } from "@supabase/auth-helpers-nextjs";

export const signInWithPassword = async (
email: string,
password: string,
signUp = false,
) => {
export const signInWithPassword = async (email: string, password: string) => {
const supabase = createServerActionClient({ cookies });

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

if (error) throw error;
return data.user;
};

export const signUp = async (email: string, password: string) => {
const supabase = createServerActionClient({ cookies });
const origin = headers().get("origin");

const { error, data } = signUp
? await supabase.auth.signUp({
email,
password,
options: {
emailRedirectTo: `${origin}/auth/callback`,
},
})
: await supabase.auth.signInWithPassword({
email,
password,
});
const { error, data } = await supabase.auth.signUp({
email,
password,
options: {
emailRedirectTo: `${origin}/auth/callback`,
},
});

if (error) throw error;
return data.user;
};

export const signInWithGithub = async () => {
"use server";
const origin = headers().get("origin");
const supabase = createServerActionClient({ cookies });

Expand Down
4 changes: 2 additions & 2 deletions apps/nextjs/src/app/auth/login/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useRouter } from "next/navigation";
import { Github } from "lucide-react";

import { signInWithGithub, signInWithPassword } from "../actions";
import { signInWithGithub, signInWithPassword, signUp } from "../actions";

export default function LoginPage() {
const router = useRouter();
Expand Down Expand Up @@ -44,7 +44,7 @@ export default function LoginPage() {
const email = formData.get("email") as string;
const password = formData.get("password") as string;

await signInWithPassword(email, password, true);
await signUp(email, password);
alert("Check your email for a confirmation link.");
}}
>
Expand Down

0 comments on commit 5506b75

Please sign in to comment.