Add a way to login to your account on the website
This commit is contained in:
92
src/app/account/login/page.tsx
Normal file
92
src/app/account/login/page.tsx
Normal file
@@ -0,0 +1,92 @@
|
||||
'use client'
|
||||
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { getCookie, setCookie } from '@/util/cookie'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { HomeButton } from '@/app/components/HomeButton'
|
||||
import { DiscordButton } from '@/app/components/DiscordButton'
|
||||
import axios from 'axios'
|
||||
|
||||
export default function AccountLoginPage () {
|
||||
const [loading, setLoading] = useState<boolean>(true)
|
||||
const router = useRouter()
|
||||
|
||||
useEffect(() => {
|
||||
const token = getCookie('accountToken', '-1')
|
||||
if (token !== '-1') {
|
||||
router.push('/account')
|
||||
} else setLoading(false)
|
||||
}, [router])
|
||||
|
||||
return (
|
||||
<div className='box'>
|
||||
<HomeButton />
|
||||
<DiscordButton />
|
||||
<p className={`px-8 ${loading ? '-my-2' : 'mb-4 -mt-2'} text-center`}>
|
||||
{loading ? 'Loading...' : 'Lncvrt Games Login'}
|
||||
</p>
|
||||
{!loading && (
|
||||
<form
|
||||
className='flex flex-col gap-2'
|
||||
onSubmit={async e => {
|
||||
e.preventDefault()
|
||||
|
||||
const form = e.currentTarget
|
||||
const formData = new FormData(form)
|
||||
const username = formData.get('username') as string
|
||||
const password = formData.get('password') as string
|
||||
|
||||
try {
|
||||
const result = await axios.post('/api/account/login', {
|
||||
username,
|
||||
password
|
||||
})
|
||||
if (result.data.success) {
|
||||
if (result.data.data.session)
|
||||
setCookie('accountToken', result.data.data.session)
|
||||
if (result.data.data.username)
|
||||
setCookie('accountUsername', result.data.data.username)
|
||||
if (result.data.data.id)
|
||||
setCookie('accountId', result.data.data.id)
|
||||
|
||||
router.push('/account')
|
||||
} else {
|
||||
alert(
|
||||
'Failed to login, error: ' + (result.data.message || 'n/a')
|
||||
)
|
||||
}
|
||||
} catch (e: any) {
|
||||
if (e.response) {
|
||||
alert(
|
||||
'Failed to login, error: ' +
|
||||
(e.response.data?.message ||
|
||||
JSON.stringify(e.response.data))
|
||||
)
|
||||
} else if (e.request) {
|
||||
alert('Failed to login, no response from server.')
|
||||
} else {
|
||||
alert('Failed to login, error: ' + e.message)
|
||||
}
|
||||
}
|
||||
}}
|
||||
>
|
||||
<input
|
||||
id='username'
|
||||
name='username'
|
||||
placeholder='Username'
|
||||
type='username'
|
||||
required
|
||||
/>
|
||||
<input
|
||||
id='password'
|
||||
name='password'
|
||||
placeholder='Password'
|
||||
type='password'
|
||||
required
|
||||
/>
|
||||
<button type='submit'>Login</button>
|
||||
</form>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user