hardening
This commit is contained in:
@@ -2,7 +2,13 @@
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { authFetch, clearToken, getApiBase, getToken } from '../lib/auth'
|
||||
import {
|
||||
authFetchOrThrow,
|
||||
getApiBase,
|
||||
getToken,
|
||||
readResponseText,
|
||||
UnauthorizedError,
|
||||
} from '../lib/auth'
|
||||
|
||||
type ProfileInfo = {
|
||||
username: string
|
||||
@@ -26,9 +32,8 @@ export default function ProfilePage() {
|
||||
const load = async () => {
|
||||
try {
|
||||
const baseUrl = getApiBase()
|
||||
const response = await authFetch(`${baseUrl}/auth/me`)
|
||||
const response = await authFetchOrThrow(`${baseUrl}/auth/me`)
|
||||
if (!response.ok) {
|
||||
clearToken()
|
||||
router.push('/login')
|
||||
return
|
||||
}
|
||||
@@ -39,6 +44,10 @@ export default function ProfilePage() {
|
||||
auth_provider: data?.auth_provider ?? 'local',
|
||||
})
|
||||
} catch (err) {
|
||||
if (err instanceof UnauthorizedError) {
|
||||
router.push('/login')
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
setStatus('Could not load your profile.')
|
||||
} finally {
|
||||
@@ -57,7 +66,7 @@ export default function ProfilePage() {
|
||||
}
|
||||
try {
|
||||
const baseUrl = getApiBase()
|
||||
const response = await authFetch(`${baseUrl}/auth/password`, {
|
||||
const response = await authFetchOrThrow(`${baseUrl}/auth/password`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
@@ -66,13 +75,17 @@ export default function ProfilePage() {
|
||||
}),
|
||||
})
|
||||
if (!response.ok) {
|
||||
const text = await response.text()
|
||||
const text = await readResponseText(response)
|
||||
throw new Error(text || 'Update failed')
|
||||
}
|
||||
setCurrentPassword('')
|
||||
setNewPassword('')
|
||||
setStatus('Password updated.')
|
||||
} catch (err) {
|
||||
if (err instanceof UnauthorizedError) {
|
||||
router.push('/login')
|
||||
return
|
||||
}
|
||||
console.error(err)
|
||||
setStatus('Could not update password. Check your current password.')
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user