-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix a bunch of broken behaviors around authentication, basic homepage (…
…#87)
- Loading branch information
Showing
9 changed files
with
216 additions
and
221 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,31 +1,27 @@ | ||
import { Form, useSearchParams } from "@remix-run/react"; | ||
import { Form } from "@remix-run/react"; | ||
import type { ButtonHTMLAttributes } from "react"; | ||
|
||
export function Login({ errors }: { errors?: { [k: string]: string } }) { | ||
const [searchParams] = useSearchParams(); | ||
const redirectTo = searchParams.get("redirectTo") || "/notes"; | ||
interface LoginProps extends ButtonHTMLAttributes<Element> { | ||
errors?: { [k: string]: string }; | ||
redirectTo?: string; | ||
} | ||
|
||
export function Login({ | ||
children = "Log in with Discord", | ||
errors, | ||
redirectTo, | ||
...props | ||
}: LoginProps) { | ||
return ( | ||
<div className="flex min-h-full flex-col justify-center"> | ||
<div className="mx-auto w-full max-w-md px-8"> | ||
<Form method="post" className="space-y-6" action="/auth"> | ||
<input type="hidden" name="redirectTo" value={redirectTo} /> | ||
<button | ||
type="submit" | ||
className="w-full rounded bg-blue-500 px-4 py-2 text-white hover:bg-blue-600 focus:bg-blue-400" | ||
> | ||
Log in with Discord | ||
</button> | ||
<div className="flex items-center justify-between"> | ||
<div className="flex items-center"> | ||
{Object.values(errors || {}).map((error) => ( | ||
<p key={error} className="text-red-500"> | ||
{error} | ||
</p> | ||
))} | ||
</div> | ||
</div> | ||
</Form> | ||
</div> | ||
</div> | ||
<Form method="post" className="space-y-6" action="/auth"> | ||
<input type="hidden" name="redirectTo" value={redirectTo} /> | ||
<button | ||
className="w-full rounded bg-blue-500 px-4 py-2 text-white hover:bg-blue-600 focus:bg-blue-400" | ||
{...props} | ||
type="submit" | ||
> | ||
{children} | ||
</button> | ||
</Form> | ||
); | ||
} |
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,26 @@ | ||
import { Form } from "@remix-run/react"; | ||
import type { ButtonHTMLAttributes } from "react"; | ||
|
||
interface LoginProps extends ButtonHTMLAttributes<Element> { | ||
errors?: { [k: string]: string }; | ||
redirectTo?: string; | ||
} | ||
|
||
export function Logout({ | ||
children = "Log out", | ||
errors, | ||
redirectTo, | ||
...props | ||
}: LoginProps) { | ||
return ( | ||
<Form method="post" action="/logout" className="space-y-6"> | ||
<button | ||
type="submit" | ||
{...props} | ||
className="w-full rounded bg-blue-500 px-4 py-2 text-white hover:bg-blue-600 focus:bg-blue-400" | ||
> | ||
{children} | ||
</button> | ||
</Form> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,24 +1,30 @@ | ||
import type { ActionFunction, LoaderFunction } from "@remix-run/node"; | ||
import { redirect } from "@remix-run/node"; | ||
|
||
import { getUser, initOauthLogin } from "~/models/session.server"; | ||
import { initOauthLogin } from "~/models/session.server"; | ||
import { Login } from "~/components/login"; | ||
|
||
export const loader: LoaderFunction = async ({ request }) => { | ||
const user = await getUser(request); | ||
if (user) return redirect("/"); | ||
return redirect("/login"); | ||
return redirect("/"); | ||
}; | ||
|
||
export const action: ActionFunction = async ({ request }) => { | ||
// fetch user from db | ||
// if doesn't exist, create it with discord ID + email | ||
const form = await request.formData(); | ||
|
||
return initOauthLogin({ | ||
request, | ||
redirectTo: "http://localhost:3000/discord-oauth", | ||
redirectTo: form.get("redirectTo")?.toString() ?? undefined, | ||
}); | ||
}; | ||
|
||
export default function LoginPage() { | ||
return <Login errors={undefined} />; | ||
return ( | ||
<div className="flex min-h-full flex-col justify-center"> | ||
<div className="mx-auto w-full max-w-md px-8"> | ||
<Login redirectTo="/dashboard" />; | ||
</div> | ||
</div> | ||
); | ||
} |
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,6 +1,22 @@ | ||
import type { LoaderFunction } from "@remix-run/server-runtime"; | ||
import { redirect, type LoaderFunction } from "@remix-run/node"; | ||
import { completeOauthLogin } from "~/models/session.server"; | ||
|
||
export const loader: LoaderFunction = async ({ request }) => { | ||
return await completeOauthLogin(request); | ||
const url = new URL(request.url); | ||
const code = url.searchParams.get("code"); | ||
const cookie = request.headers.get("Cookie"); | ||
if (!code) { | ||
console.error("No code provided by Discord"); | ||
return redirect("/"); | ||
} | ||
if (!cookie) { | ||
console.error("No cookie found when responding to Discord oauth"); | ||
throw redirect("/login", 500); | ||
} | ||
|
||
return await completeOauthLogin( | ||
code, | ||
cookie, | ||
url.searchParams.get("state") ?? undefined, | ||
); | ||
}; |
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
Oops, something went wrong.