-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user authentication and session management
- Loading branch information
Showing
12 changed files
with
202 additions
and
257 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import bcrypt from 'bcrypt'; | ||
import * as argon2 from 'argon2'; | ||
|
||
export const hashPassword = async (plainTextPassword: string) => bcrypt.hash(plainTextPassword, 10); | ||
export const hashPassword = async (plainTextPassword: string) => argon2.hash(plainTextPassword); | ||
|
||
export const verifyPassword = async (plainTextPassword: string, hashedPassword: string) => | ||
bcrypt.compare(plainTextPassword, hashedPassword); | ||
argon2.verify(hashedPassword, plainTextPassword); |
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,62 @@ | ||
import type { ActionFunctionArgs, LoaderFunctionArgs } from '@remix-run/node'; | ||
import { json } from '@remix-run/node'; | ||
import { useLoaderData } from '@remix-run/react'; | ||
import { useId } from 'react'; | ||
import { Button, Input } from 'reactstrap'; | ||
import { Authenticator } from 'remix-auth'; | ||
import { CREDENTIALS_STRATEGY } from '../auth/auth.server'; | ||
import type { SessionStorage } from '../auth/session.server'; | ||
import { serverContainer } from '../container/container.server'; | ||
|
||
export async function action( | ||
{ request }: ActionFunctionArgs, | ||
authenticator: Authenticator = serverContainer[Authenticator.name], | ||
) { | ||
const { searchParams } = new URL(request.url); | ||
return authenticator.authenticate(CREDENTIALS_STRATEGY, request, { | ||
successRedirect: searchParams.get('redirect-to') ?? '/', // TODO Make sure "redirect-to" is a relative URL | ||
failureRedirect: request.url, | ||
}); | ||
} | ||
|
||
export async function loader( | ||
{ request }: LoaderFunctionArgs, | ||
authenticator: Authenticator = serverContainer[Authenticator.name], | ||
{ getSession, commitSession }: SessionStorage = serverContainer.sessionStorage, | ||
) { | ||
// If the user is already authenticated redirect to home | ||
await authenticator.isAuthenticated(request, { successRedirect: '/' }); | ||
|
||
const session = await getSession(request.headers.get('cookie')); | ||
const error = session.get(authenticator.sessionErrorKey); | ||
return json({ hasError: !!error }, { | ||
headers: { | ||
'Set-Cookie': await commitSession(session), | ||
}, | ||
}); | ||
} | ||
|
||
export default function Login() { | ||
const usernameId = useId(); | ||
const passwordId = useId(); | ||
const { hasError } = useLoaderData<typeof loader>(); | ||
|
||
return ( | ||
<form | ||
method="post" | ||
className="d-flex flex-column gap-3 p-3 mt-5 mx-auto w-50 rounded-2 border-opacity-25 border-secondary" | ||
style={{ borderWidth: '1px', borderStyle: 'solid' }} | ||
> | ||
<div> | ||
<label htmlFor={usernameId}>Username:</label> | ||
<Input id={usernameId} name="username" /> | ||
</div> | ||
<div> | ||
<label htmlFor={passwordId}>Password:</label> | ||
<Input id={passwordId} type="password" name="password" /> | ||
</div> | ||
<Button color="primary" type="submit">Login</Button> | ||
{hasError && <div className="text-danger">Username or password are incorrect</div>} | ||
</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
Oops, something went wrong.