-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.tsx
103 lines (90 loc) · 2.53 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as React from 'react'
import type { LoaderFunction } from 'remix'
import { json, useLoaderData } from 'remix'
import type { User } from '~/lib/users.server'
import { getAllUsers } from '~/lib/users.server'
type LoaderData = {
readonly users: readonly User[]
}
export const loader: LoaderFunction = function () {
const users = getAllUsers()
return json<LoaderData>({ users })
}
export default function Admin() {
const data = useLoaderData<LoaderData>()
return (
<div className="p-4">
<div className="mx-auto max-w-4xl">
<h1 className="text-xl font-semibold">Users</h1>
<ul>
{data.users.map((user) => (
<li
key={user.id}
className="flex justify-between p-2 even:bg-gray-100 hover:bg-gray-200"
>
<span>
{user.name} ({user.email})
</span>
<span className="space-x-4 text-sm">
<LoginWithGet userId={user.id} />
<LoginWithPost userId={user.id} />
</span>
</li>
))}
</ul>
</div>
</div>
)
}
function LoginWithGet({ userId }: { userId: number }) {
async function loginWithGet() {
const w = window.open('/admin/login-as', '_blank')
if (!w) {
return
}
try {
const token = await getAuthToken(userId)
w.location.href = `/app/api/login?token=${token}`
} catch (e: unknown) {
w.location.href = `/admin/login-as?error=${e}`
}
}
return (
<button className="underline hover:text-blue-600" onClick={loginWithGet}>
Log in with GET
</button>
)
}
function LoginWithPost({ userId }: { userId: number }) {
async function loginWithPost() {
const w = window.open('/admin/login-as', '_blank')
if (!w) {
return
}
try {
const token = await getAuthToken(userId)
w.location.href = `/admin/login-as?token=${token}`
} catch (e: unknown) {
w.location.href = `/admin/login-as?error=${e}`
}
}
return (
<button className="underline hover:text-blue-600" onClick={loginWithPost}>
Log in with POST
</button>
)
}
async function getAuthToken(userId: number) {
const url = new URL('/admin/api/auth-token', window.location.origin)
url.searchParams.append('userId', String(userId))
if (userId % 3 === 0) {
url.searchParams.set('error', 'Unable to generate auth token')
}
const response = await fetch(url.href)
const data = await response.json()
if (response.ok) {
return data
} else {
throw data
}
}