-
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.
moving all auth handlers to one file
- Loading branch information
Showing
6 changed files
with
168 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,3 @@ | ||
import { object, safeParseAsync, string } from "valibot"; | ||
import { invalidRequestError, redirectToPath } from "../../../../server/errors"; | ||
import { callback } from "../../../../server/auth"; | ||
|
||
export const GET: MarkoRun.Handler = async (context) => { | ||
console.log({ context }); | ||
const parsed = await safeParseAsync( | ||
object({ code: string() }), | ||
Object.fromEntries(context.url.searchParams.entries()), | ||
); | ||
|
||
if (!parsed.success) { | ||
return invalidRequestError(parsed.issues); | ||
} | ||
|
||
const response = await context.supabase.auth.exchangeCodeForSession( | ||
parsed.output.code, | ||
); | ||
|
||
console.log({ response }); | ||
|
||
return redirectToPath({ path: "/" }); | ||
}; | ||
export const GET: MarkoRun.Handler = callback; |
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 |
---|---|---|
@@ -1,11 +1,3 @@ | ||
import { redirectToPath } from "../../../../server/errors"; | ||
import { signOut } from "../../../../server/auth"; | ||
|
||
export const GET: MarkoRun.Handler = async (context) => { | ||
console.log({ context }); | ||
|
||
const response = await context.supabase.auth.signOut(); | ||
|
||
console.log({ response }); | ||
|
||
return redirectToPath({ path: "/" }); | ||
}; | ||
export const GET: MarkoRun.Handler = signOut; |
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 |
---|---|---|
@@ -1,33 +1,3 @@ | ||
import { decode } from "decode-formdata"; | ||
import { email, object, safeParseAsync, string } from "valibot"; | ||
import { invalidRequestError, redirectToPath } from "../../server/errors"; | ||
import { passwordSignUp } from "../../server/auth"; | ||
|
||
export const POST: MarkoRun.Handler = async (context) => { | ||
const parsed = await safeParseAsync( | ||
object({ email: string([email()]), password: string() }), | ||
decode(await context.request.formData()), | ||
); | ||
|
||
if (!parsed.success) { | ||
return invalidRequestError(parsed.issues); | ||
} | ||
|
||
console.log({ context, form: parsed }); | ||
|
||
const response = await context.supabase.auth.signUp({ | ||
email: parsed.output.email, | ||
password: parsed.output.password, | ||
}); | ||
|
||
if (response.error) { | ||
return redirectToPath({ | ||
path: "/sign-up", | ||
query: { message: response.error.message }, | ||
}); | ||
} | ||
|
||
return redirectToPath({ | ||
path: "/sign-up", | ||
query: { message: "Success", variant: "success" }, | ||
}); | ||
}; | ||
export const POST: MarkoRun.Handler = passwordSignUp; |
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,156 @@ | ||
import { decode } from "decode-formdata"; | ||
import { email, literal, object, safeParseAsync, string } from "valibot"; | ||
import { buildPath } from "../utils/paths"; | ||
import { invalidRequestError, redirectToPath } from "./errors"; | ||
|
||
const getCallbackUrl = (context: MarkoRun.Context) => { | ||
const callbackPath = buildPath({ path: "/api/auth/callback" }); | ||
return `${context.url.origin}${callbackPath}`; | ||
}; | ||
|
||
type SignInArgs = { | ||
context: MarkoRun.Context; | ||
decoded: ReturnType<typeof decode>; | ||
}; | ||
|
||
export const magicLinkSignIn = async ({ context, decoded }: SignInArgs) => { | ||
const parsed = await safeParseAsync( | ||
object({ email: string([email()]) }), | ||
decoded, | ||
); | ||
|
||
if (!parsed.success) { | ||
return invalidRequestError(parsed.issues); | ||
} | ||
|
||
const response = await context.supabase.auth.signInWithOtp({ | ||
email: parsed.output.email, | ||
options: { emailRedirectTo: getCallbackUrl(context) }, | ||
}); | ||
|
||
if (response.error) { | ||
return redirectToPath({ | ||
path: "/sign-in", | ||
query: { message: response.error.message }, | ||
}); | ||
} | ||
|
||
console.log({ response }); | ||
|
||
return redirectToPath({ | ||
path: "/sign-in", | ||
query: { message: "Success", variant: "success" }, | ||
}); | ||
}; | ||
|
||
export const oauthSignIn = async ({ context, decoded }: SignInArgs) => { | ||
const parsed = await safeParseAsync( | ||
object({ provider: literal("google") }), | ||
decoded, | ||
); | ||
|
||
if (!parsed.success) { | ||
return invalidRequestError(parsed.issues); | ||
} | ||
|
||
const response = await context.supabase.auth.signInWithOAuth({ | ||
provider: parsed.output.provider, | ||
options: { redirectTo: getCallbackUrl(context) }, | ||
}); | ||
|
||
if (response.error) { | ||
return redirectToPath({ | ||
path: "/sign-in", | ||
query: { message: response.error.message }, | ||
}); | ||
} | ||
|
||
return new Response(null, { | ||
headers: { location: response.data.url }, | ||
}); | ||
}; | ||
|
||
export const passwordSignIn = async ({ context, decoded }: SignInArgs) => { | ||
const parsed = await safeParseAsync( | ||
object({ email: string([email()]), password: string() }), | ||
decoded, | ||
); | ||
|
||
if (!parsed.success) { | ||
return invalidRequestError(parsed.issues); | ||
} | ||
|
||
const response = await context.supabase.auth.signInWithPassword({ | ||
email: parsed.output.email, | ||
password: parsed.output.password, | ||
}); | ||
|
||
if (response.error) { | ||
return redirectToPath({ | ||
path: "/sign-in", | ||
query: { message: response.error.message }, | ||
}); | ||
} | ||
|
||
return redirectToPath({ path: "/todos" }); | ||
}; | ||
|
||
export const passwordSignUp = async (context: MarkoRun.Context) => { | ||
const parsed = await safeParseAsync( | ||
object({ email: string([email()]), password: string() }), | ||
decode(await context.request.formData()), | ||
); | ||
|
||
if (!parsed.success) { | ||
return invalidRequestError(parsed.issues); | ||
} | ||
|
||
const response = await context.supabase.auth.signUp({ | ||
email: parsed.output.email, | ||
password: parsed.output.password, | ||
options: { emailRedirectTo: getCallbackUrl(context) }, | ||
}); | ||
|
||
if (response.error) { | ||
return redirectToPath({ | ||
path: "/sign-up", | ||
query: { message: response.error.message }, | ||
}); | ||
} | ||
|
||
return redirectToPath({ | ||
path: "/sign-up", | ||
query: { message: "Success", variant: "success" }, | ||
}); | ||
}; | ||
|
||
export const signOut = async (context: MarkoRun.Context) => { | ||
console.log({ context }); | ||
|
||
const response = await context.supabase.auth.signOut(); | ||
|
||
console.log({ response }); | ||
|
||
return redirectToPath({ path: "/" }); | ||
}; | ||
|
||
export const callback = async (context: MarkoRun.Context) => { | ||
const parsed = await safeParseAsync( | ||
object({ code: string() }), | ||
Object.fromEntries(context.url.searchParams.entries()), | ||
); | ||
|
||
if (!parsed.success) { | ||
return invalidRequestError(parsed.issues); | ||
} | ||
|
||
const response = await context.supabase.auth.exchangeCodeForSession( | ||
parsed.output.code, | ||
); | ||
|
||
if (response.error) { | ||
return redirectToPath({ path: "/todos" }); | ||
} | ||
|
||
return redirectToPath({ path: "/" }); | ||
}; |