Last active
May 11, 2024 20:32
-
-
Save jean-leonco/9197ee4d9ffb16a40aa6eb7334f04018 to your computer and use it in GitHub Desktop.
Remix Simple Authentication
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
import { redirect, type LoaderFunctionArgs } from '@remix-run/node' | |
import { Form, useLoaderData } from '@remix-run/react' | |
import { sessionCookie } from '../session.server' | |
export async function loader({ request }: LoaderFunctionArgs) { | |
const cookieHeader = request.headers.get('Cookie') | |
const cookie = await sessionCookie.parse(cookieHeader) | |
if (!cookie) { | |
return redirect('/login') | |
} | |
return { userId: cookie.userId } | |
} | |
export default function Index() { | |
const { userId } = useLoaderData<typeof loader>() | |
return ( | |
<Form action="/logout" method="post"> | |
<h1>Welcome to Remix! User {userId}</h1> | |
<button type="submit">Sair</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
import { | |
ActionFunctionArgs, | |
LoaderFunctionArgs, | |
redirect, | |
} from '@remix-run/node' | |
import { Form } from '@remix-run/react' | |
import { sessionCookie } from '~/session.server' | |
async function getUser({ | |
email, | |
password, | |
}: { | |
email: string | |
password: string | |
}) { | |
if (email === '[email protected]' && password === '123123') { | |
return { id: 123 } | |
} | |
return null | |
} | |
async function createUserSession({ userId }: { userId: number }) { | |
return { id: 123, userId } | |
} | |
export async function loader({ request }: LoaderFunctionArgs) { | |
const cookieHeader = request.headers.get('Cookie') | |
const cookie = await sessionCookie.parse(cookieHeader) | |
// user is logged in | |
if (cookie) { | |
return redirect('/') | |
} | |
return null | |
} | |
export async function action({ request }: ActionFunctionArgs) { | |
const formData = await request.formData() | |
const email = String(formData.get('email')) | |
const password = String(formData.get('password')) | |
const user = await getUser({ email, password }) | |
if (!user) { | |
return new Response('Unable to authenticate', { status: 400 }) | |
} | |
const session = await createUserSession({ userId: user.id }) | |
return redirect('/', { | |
headers: { | |
'Set-Cookie': await sessionCookie.serialize(session), | |
}, | |
}) | |
} | |
export default function Login() { | |
return ( | |
<Form | |
method="post" | |
style={{ | |
display: 'flex', | |
flexDirection: 'column', | |
gap: 8, | |
maxWidth: 300, | |
}} | |
> | |
<input name="email" type="email" defaultValue="[email protected]" /> | |
<input name="password" type="password" defaultValue="123123" /> | |
<button type="submit">Entrar</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
import { ActionFunctionArgs, redirect } from '@remix-run/node' | |
import { sessionCookie } from '~/session.server' | |
async function destroySession({ userId }: { userId: number }) { | |
// session exists? user exists? | |
return { id: 123123 } | |
} | |
export async function action({ request }: ActionFunctionArgs) { | |
const cookieHeader = request.headers.get('Cookie') | |
const cookie = await sessionCookie.parse(cookieHeader) | |
if (!cookie) { | |
return redirect('/login') | |
} | |
const destroyedSession = await destroySession({ userId: cookie.userId }) | |
if (!destroyedSession) { | |
return new Response('Unable to logout', { status: 400 }) | |
} | |
return redirect('/login', { | |
headers: { | |
'Set-Cookie': await sessionCookie.serialize(''), | |
}, | |
}) | |
} |
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
import { createCookie } from '@remix-run/node' | |
export const sessionCookie = createCookie('_session', { | |
httpOnly: true, | |
secrets: ['verysecretsecret'], | |
secure: true, | |
maxAge: 604800, // one week | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment