-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df5f64a
commit 7f4772c
Showing
7 changed files
with
123 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,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("/"); | ||
}; |
This file contains 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