|
| 1 | +import type {Metadata} from 'next'; |
| 2 | +import {redirect} from 'next/navigation'; |
| 3 | +import {Box, Button, Card, Flex, Heading, Text, TextField} from '@radix-ui/themes'; |
| 4 | +import {getServerSession} from '@/app/lib/auth'; |
| 5 | +import {sanitizeReturnTo} from '@/app/utils/sanitizeReturnTo'; |
| 6 | +import {loginAction} from './actions'; |
| 7 | + |
| 8 | +export const metadata: Metadata = { |
| 9 | + title: 'Sign in - Districtr', |
| 10 | + description: 'Sign in to Districtr', |
| 11 | +}; |
| 12 | + |
| 13 | +export default async function LoginPage({ |
| 14 | + searchParams, |
| 15 | +}: { |
| 16 | + searchParams: Promise<{returnTo?: string; error?: string}>; |
| 17 | +}) { |
| 18 | + const params = await searchParams; |
| 19 | + const returnTo = sanitizeReturnTo(params.returnTo); |
| 20 | + |
| 21 | + // Already signed in — go straight to the destination |
| 22 | + const session = await getServerSession(); |
| 23 | + if (session?.user) { |
| 24 | + redirect(returnTo); |
| 25 | + } |
| 26 | + |
| 27 | + return ( |
| 28 | + <Flex align="center" justify="center" className="min-h-screen bg-gray-100"> |
| 29 | + <Card size="3" className="w-full max-w-md"> |
| 30 | + <Flex direction="column" gap="4" p="4"> |
| 31 | + <Heading size="5">Sign in to Districtr</Heading> |
| 32 | + {params.error && ( |
| 33 | + <Text size="2" color="red"> |
| 34 | + Invalid email or password. Please try again. |
| 35 | + </Text> |
| 36 | + )} |
| 37 | + <form action={loginAction}> |
| 38 | + <Flex direction="column" gap="3"> |
| 39 | + <input type="hidden" name="returnTo" value={returnTo} /> |
| 40 | + <Box> |
| 41 | + <Text as="label" size="2" weight="medium" htmlFor="email"> |
| 42 | + Email |
| 43 | + </Text> |
| 44 | + <TextField.Root |
| 45 | + id="email" |
| 46 | + name="email" |
| 47 | + type="email" |
| 48 | + placeholder="you@example.com" |
| 49 | + autoComplete="username" |
| 50 | + required |
| 51 | + /> |
| 52 | + </Box> |
| 53 | + <Box> |
| 54 | + <Text as="label" size="2" weight="medium" htmlFor="password"> |
| 55 | + Password |
| 56 | + </Text> |
| 57 | + <TextField.Root |
| 58 | + id="password" |
| 59 | + name="password" |
| 60 | + type="password" |
| 61 | + placeholder="Password" |
| 62 | + autoComplete="current-password" |
| 63 | + required |
| 64 | + /> |
| 65 | + </Box> |
| 66 | + <Button type="submit" size="3"> |
| 67 | + Sign in |
| 68 | + </Button> |
| 69 | + </Flex> |
| 70 | + </form> |
| 71 | + </Flex> |
| 72 | + </Card> |
| 73 | + </Flex> |
| 74 | + ); |
| 75 | +} |
0 commit comments