-
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.
- Loading branch information
Showing
9 changed files
with
287 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {type LoaderFunctionArgs, json} from '@remix-run/node' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
|
||
export const loader = async ({request}: LoaderFunctionArgs) => { | ||
const user = await ensureUser(request, 'dashboard', {}) | ||
|
||
console.log('HIYA') | ||
|
||
return json({user}) | ||
} |
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,21 @@ | ||
import {redirect, type LoaderFunctionArgs} from '@remix-run/node' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {session} from '~/lib/cookies' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
|
||
export const loader = async ({request}: LoaderFunctionArgs) => { | ||
const {sessionId} = await ensureUser(request, 'logout', {}) | ||
|
||
const prisma = getPrisma() | ||
|
||
await prisma.session.delete({where: {id: sessionId}}) | ||
|
||
return redirect(`/app/login`, { | ||
headers: { | ||
'Set-Cookie': await session.serialize('', { | ||
maxAge: 1 | ||
}) | ||
} | ||
}) | ||
} |
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,93 @@ | ||
import { | ||
type LoaderFunctionArgs, | ||
type ActionFunctionArgs, | ||
json, | ||
redirect | ||
} from '@remix-run/node' | ||
import {useLoaderData} from '@remix-run/react' | ||
import {invariant} from '@arcath/utils' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
import {Button} from '~/lib/components/button' | ||
import {Label, Input, Select} from '~/lib/components/input' | ||
import {hashPassword} from '~/lib/user.server' | ||
|
||
export const loader = async ({request, params}: LoaderFunctionArgs) => { | ||
const currentUser = await ensureUser(request, 'user-manager:edit', { | ||
userId: params.user | ||
}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const user = await prisma.user.findFirstOrThrow({where: {id: params.user}}) | ||
|
||
return json({currentUser, user}) | ||
} | ||
|
||
export const action = async ({request, params}: ActionFunctionArgs) => { | ||
const currentUser = await ensureUser(request, 'user-manager:edit', { | ||
userId: params.user | ||
}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const formData = await request.formData() | ||
|
||
const name = formData.get('name') as string | undefined | ||
const email = formData.get('email') as string | undefined | ||
const password = formData.get('password') as string | undefined | ||
const role = formData.get('role') as string | undefined | ||
|
||
invariant(name) | ||
invariant(email) | ||
invariant(role) | ||
|
||
let newPassword: string | undefined = undefined | ||
|
||
if (password) { | ||
newPassword = await hashPassword(password) | ||
} | ||
|
||
const user = await prisma.user.update({ | ||
where: {id: params.user}, | ||
data: {name, email, passwordHash: newPassword, role} | ||
}) | ||
|
||
return redirect(`/app/user-manager/${user.id}`) | ||
} | ||
|
||
const UserManagerUser = () => { | ||
const {user} = useLoaderData<typeof loader>() | ||
|
||
return ( | ||
<div> | ||
<h4 className="text-xl">{user.name}</h4> | ||
<form method="POST"> | ||
<Label> | ||
Name | ||
<Input name="name" defaultValue={user.name} /> | ||
</Label> | ||
<Label> | ||
<Input name="email" type="email" defaultValue={user.email} /> | ||
</Label> | ||
<Label> | ||
Password | ||
<Input name="password" type="password" /> | ||
</Label> | ||
<Label> | ||
Role | ||
<Select name="role" defaultValue={user.role}> | ||
<option value="user">User</option> | ||
<option value="manager">Manager</option> | ||
<option value="admin">Admin</option> | ||
</Select> | ||
</Label> | ||
<Button className="bg-success">Update User</Button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
|
||
export default UserManagerUser |
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,53 @@ | ||
import {type LoaderFunctionArgs, json} from '@remix-run/node' | ||
import {useLoaderData} from '@remix-run/react' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
import {AButton} from '~/lib/components/button' | ||
|
||
export const loader = async ({request}: LoaderFunctionArgs) => { | ||
const user = await ensureUser(request, 'user-manager:list', {}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const users = await prisma.user.findMany({ | ||
select: {id: true, name: true, email: true}, | ||
orderBy: {name: 'asc'} | ||
}) | ||
|
||
return json({user, users}) | ||
} | ||
|
||
const UserManagerList = () => { | ||
const {users} = useLoaderData<typeof loader>() | ||
|
||
return ( | ||
<div> | ||
<AButton className="bg-success" href="/app/user-manager/add"> | ||
Add User | ||
</AButton> | ||
<table className=""> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Email</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{users.map(({id, name, email}) => { | ||
return ( | ||
<tr key={id}> | ||
<td> | ||
<a href={`/app/user-manager/${id}`}>{name}</a> | ||
</td> | ||
<td>{email}</td> | ||
</tr> | ||
) | ||
})} | ||
</tbody> | ||
</table> | ||
</div> | ||
) | ||
} | ||
|
||
export default UserManagerList |
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,81 @@ | ||
import { | ||
type LoaderFunctionArgs, | ||
type ActionFunctionArgs, | ||
json, | ||
redirect | ||
} from '@remix-run/node' | ||
import {useLoaderData} from '@remix-run/react' | ||
import {invariant} from '@arcath/utils' | ||
|
||
import {ensureUser} from '~/lib/utils/ensure-user' | ||
import {getPrisma} from '~/lib/prisma.server' | ||
import {Button} from '~/lib/components/button' | ||
import {Label, Input, Select} from '~/lib/components/input' | ||
import {hashPassword} from '~/lib/user.server' | ||
|
||
export const loader = async ({request, params}: LoaderFunctionArgs) => { | ||
const currentUser = await ensureUser(request, 'user-manager:edit', { | ||
userId: params.user | ||
}) | ||
|
||
return json({currentUser}) | ||
} | ||
|
||
export const action = async ({request, params}: ActionFunctionArgs) => { | ||
await ensureUser(request, 'user-manager:add', { | ||
userId: params.user | ||
}) | ||
|
||
const prisma = getPrisma() | ||
|
||
const formData = await request.formData() | ||
|
||
const name = formData.get('name') as string | undefined | ||
const email = formData.get('email') as string | undefined | ||
const password = formData.get('password') as string | undefined | ||
const role = formData.get('role') as string | undefined | ||
|
||
invariant(name) | ||
invariant(email) | ||
invariant(password) | ||
invariant(role) | ||
|
||
const user = await prisma.user.create({ | ||
data: {name, email, passwordHash: await hashPassword(password), role} | ||
}) | ||
|
||
return redirect(`/app/user-manager/${user.id}`) | ||
} | ||
|
||
const UserManagerAdd = () => { | ||
return ( | ||
<div> | ||
<h4 className="text-xl">Add User</h4> | ||
<form method="POST"> | ||
<Label> | ||
Name | ||
<Input name="name" /> | ||
</Label> | ||
<Label> | ||
<Input name="email" type="email" /> | ||
</Label> | ||
<Label> | ||
Password | ||
<Input name="password" type="password" /> | ||
</Label> | ||
<Label> | ||
Role | ||
<Select name="role"> | ||
<option value="user">User</option> | ||
<option value="manager">Manager</option> | ||
<option value="admin">Admin</option> | ||
</Select> | ||
</Label> | ||
<Button className="bg-success">Add User</Button> | ||
</form> | ||
</div> | ||
) | ||
} | ||
|
||
export default UserManagerAdd |
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,14 @@ | ||
import {Outlet} from '@remix-run/react' | ||
|
||
import {Header} from '~/lib/components/header' | ||
|
||
const UserManager = () => { | ||
return ( | ||
<div> | ||
<Header title="User Manager" /> | ||
<Outlet /> | ||
</div> | ||
) | ||
} | ||
|
||
export default UserManager |