Skip to content
This repository was archived by the owner on Oct 28, 2022. It is now read-only.

Commit 65112c9

Browse files
committed
Fix nextjs bug not allowing users to login
1 parent 5a3dadf commit 65112c9

File tree

2 files changed

+19
-21
lines changed

2 files changed

+19
-21
lines changed

dashboard/pages/login.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import React, { useEffect, useState } from 'react'
99
import { fetcher } from '../src/auth'
1010

1111
export default function Home() {
12-
const [apiKeyId, setApiKeyId] = useState('')
13-
const [apiKey, setApiKey] = useState('')
12+
const [form, setForm] = useState({ id: '', key: '' })
1413
const [loading, setLoading] = useState(false)
1514
const [error, setError] = useState('')
1615
const router = useRouter()
@@ -20,7 +19,7 @@ export default function Home() {
2019
try {
2120
setLoading(true)
2221
setError('')
23-
await fetcher.login(apiKey, apiKeyId)
22+
await fetcher.login(form)
2423

2524
const url = new URL(location.href)
2625
const redirectTo = url.searchParams.get('redirectTo')
@@ -37,8 +36,10 @@ export default function Home() {
3736
}
3837

3938
useEffect(() => {
40-
setApiKeyId(fetcher.getApiKeyId)
41-
setApiKey(fetcher.getApiKey)
39+
setForm({
40+
id: fetcher.getApiKeyId,
41+
key: fetcher.getApiKey,
42+
})
4243
}, [])
4344

4445
return (
@@ -51,23 +52,23 @@ export default function Home() {
5152
<form noValidate onSubmit={submit} >
5253
<p>Insert a api key with the <b>Dashboard</b> role</p>
5354
<TextField
54-
value={apiKeyId}
55+
value={form.id}
5556
fullWidth
5657
id="id"
5758
label="API Key ID"
5859
variant="filled"
59-
onChange={e => setApiKeyId(e.target.value)}
60+
onChange={e => setForm(f => ({ ...f, id: e.target.value }))}
6061
disabled={loading}
6162
error={!!error}
6263
/>
6364
<div className="marginTop" >
6465
<TextField
65-
value={apiKey}
66+
value={form.key}
6667
fullWidth
6768
id="key"
6869
label="API Key"
6970
variant="filled"
70-
onChange={e => setApiKey(e.target.value)}
71+
onChange={e => setForm(f => ({ ...f, key: e.target.value }))}
7172
disabled={loading}
7273
error={!!error}
7374
helperText={error}

dashboard/src/auth.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
import { Roles } from './roles'
2-
import { ApiKey } from './types';
2+
import { ApiKey } from './types'
33

44
class AuthenticatedFetcher {
55
private triedToRestoreCredentials = false;
66
private apiKeyHashed = '';
77
private apiKeyId = '';
88

9-
async login(key: string, keyId: string) {
9+
async login({ id, key }: { id: string, key: string }) {
1010
if (!this.triedToRestoreCredentials) this.tryRestoreCredentials();
1111

1212
if (key == '')
1313
throw 'api key not set'
14-
if (keyId == '')
14+
if (id == '')
1515
throw 'api key id not set'
1616

1717
this.apiKeyHashed = await this.hash(key)
18-
this.apiKeyId = keyId
18+
this.apiKeyId = id
1919
try {
2020
const keyInfo = await this.fetch('/api/v1/auth/keyinfo')
2121

@@ -150,14 +150,11 @@ class AuthenticatedFetcher {
150150
}
151151

152152
private async hash(data: string): Promise<string> {
153-
return Buffer.from(
154-
new Uint8Array(
155-
await crypto.subtle.digest(
156-
'SHA-512',
157-
new TextEncoder().encode(data),
158-
),
159-
),
160-
).toString('hex')
153+
const textEncoder = new TextEncoder()
154+
const dataBuffer = textEncoder.encode(data)
155+
const hashedData = await crypto.subtle.digest('SHA-512', dataBuffer)
156+
const hashedDataAsUint8 = new Uint8Array(hashedData)
157+
return Buffer.from(hashedDataAsUint8).toString('hex')
161158
}
162159
}
163160

0 commit comments

Comments
 (0)