Skip to content

Commit 5d4f153

Browse files
committed
Improve admin login UX
1 parent d5c69f9 commit 5d4f153

File tree

8 files changed

+62
-19
lines changed

8 files changed

+62
-19
lines changed

src/app/[locale]/(app)/profile/login/_components/profile-login.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { ContinueWithEmail } from './continue-with-email'
55
import { ContinueWithProvider } from './continue-with-provider'
66
import { RegisterBanner } from './register-banner'
77

8-
export const ProfileLogin: FC = () => {
8+
export const ProfileLogin: FC<{ registerDisabled?: boolean }> = ({
9+
registerDisabled,
10+
}) => {
911
const t = useTranslations('auth')
1012

1113
return (
1214
<>
13-
<RegisterBanner />
15+
{!registerDisabled && <RegisterBanner />}
1416

1517
<div className="mx-auto mt-10 max-w-sm">
1618
<h2 className="mb-2 text-center font-title text-2xl font-semibold uppercase text-stone-800">
@@ -27,12 +29,21 @@ export const ProfileLogin: FC = () => {
2729
</div>
2830
<ContinueWithProvider />
2931

30-
<h2 className="mb-2 mt-10 text-center font-title text-2xl font-semibold uppercase text-stone-800">
31-
{t('register')}
32-
</h2>
33-
<LinkButton href="/register" variant="solid" color="primary" fullWidth>
34-
{t('register')}
35-
</LinkButton>
32+
{!registerDisabled && (
33+
<>
34+
<h2 className="mb-2 mt-10 text-center font-title text-2xl font-semibold uppercase text-stone-800">
35+
{t('register')}
36+
</h2>
37+
<LinkButton
38+
href="/register"
39+
variant="solid"
40+
color="primary"
41+
fullWidth
42+
>
43+
{t('register')}
44+
</LinkButton>
45+
</>
46+
)}
3647
</div>
3748
</>
3849
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use client'
2+
3+
import { Button } from '@nextui-org/button'
4+
import { signOut } from 'next-auth/react'
5+
import { useTranslations } from 'next-intl'
6+
import { FC } from 'react'
7+
8+
export const LogoutButton: FC = () => {
9+
const t = useTranslations('admin.more-options')
10+
11+
return (
12+
<Button
13+
onClick={() => signOut({ callbackUrl: '/admin-login' })}
14+
className="mt-6 bg-brand-600 px-8 py-3 uppercase text-white"
15+
>
16+
{t('logout')}
17+
</Button>
18+
)
19+
}

src/app/[locale]/admin-login/page.tsx

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { IconBarrierBlockOff } from '@tabler/icons-react'
22
import type { Metadata } from 'next'
33
import { useTranslations } from 'next-intl'
4-
import { getTranslator } from 'next-intl/server'
4+
import { getTranslator, redirect } from 'next-intl/server'
55
import type { FC } from 'react'
66
import type { LocaleRouteParams } from '~/i18n'
7+
import { getSession } from '~/server/get-server-thing'
78
import { ProfileLogin } from '../(app)/profile/login/_components/profile-login'
9+
import { LogoutButton } from './__components/logout-button'
810

911
export async function generateMetadata({
1012
params,
@@ -19,7 +21,19 @@ export async function generateMetadata({
1921
}
2022
}
2123

22-
const AdminLoginPage: FC<LocaleRouteParams> = () => {
24+
const AdminLoginPage: FC<LocaleRouteParams> = async () => {
25+
const session = await getSession()
26+
27+
if (session?.user.role === 'admin') {
28+
redirect('/admin')
29+
}
30+
31+
return <NestedAdminLoginPage isLoggedInAsNotAdmin={!!session} />
32+
}
33+
34+
const NestedAdminLoginPage: FC<{ isLoggedInAsNotAdmin: boolean }> = ({
35+
isLoggedInAsNotAdmin,
36+
}) => {
2337
const t = useTranslations('admin-login')
2438

2539
return (
@@ -36,8 +50,11 @@ const AdminLoginPage: FC<LocaleRouteParams> = () => {
3650
</h1>
3751

3852
<p className="mt-4 text-xl">{t('subtitle')}</p>
39-
40-
<ProfileLogin />
53+
{isLoggedInAsNotAdmin ? (
54+
<LogoutButton />
55+
) : (
56+
<ProfileLogin registerDisabled={true} />
57+
)}
4158
</main>
4259
</>
4360
)

src/components/links/link-button-custom.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const LinkButtonCustom: FC<{
1313
radius="full"
1414
variant="solid"
1515
color="primary"
16-
className=" mt-6 bg-brand-600 px-8 py-3 uppercase text-white"
16+
className="mt-6 bg-brand-600 px-8 py-3 uppercase text-white"
1717
>
1818
{children}
1919
</LinkButton>

src/middleware.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'server-only'
2-
31
import { NextRequestWithAuth } from 'next-auth/middleware'
42
import { NextFetchEvent } from 'next/server'
53
import { authI18nMiddleware } from './server/helpers/auth/auth-i18n-middleware'

src/server/helpers/auth/auth-i18n-middleware.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'server-only'
2-
31
import { NextResponse, type NextRequest } from 'next/server'
42
import { defaultLocale, getLocale } from '~/i18n'
53

src/server/helpers/auth/auth-middleware.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import 'server-only'
2-
31
import {
42
NextMiddlewareWithAuth,
53
NextRequestWithAuth,

src/server/helpers/auth/initialize-user.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'server-only'
2+
13
import bcrypt from 'bcryptjs'
24
import { eq } from 'drizzle-orm'
35
import { v4 as uuidv4 } from 'uuid'

0 commit comments

Comments
 (0)