initial commit

This commit is contained in:
rustmailer
2025-11-19 02:14:37 +08:00
commit 1a8f95117e
355 changed files with 54089 additions and 0 deletions
@@ -0,0 +1,61 @@
//
// Copyright (c) 2025 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { IconLogout } from '@tabler/icons-react'
import { ConfirmDialog } from '@/components/confirm-dialog'
interface Props {
open: boolean
onOpenChange: (open: boolean) => void
handleConfirm: () => void
}
export function LogoutConfirmDialog({ open, onOpenChange, handleConfirm }: Props) {
const handleLogout = () => {
handleConfirm();
onOpenChange(false);
};
return (
<ConfirmDialog
open={open}
onOpenChange={onOpenChange}
handleConfirm={handleLogout}
className="max-w-md"
title={
<span className='text-destructive'>
<IconLogout
className='mr-1 inline-block stroke-destructive'
size={18}
/>{' '}
Log out
</span>
}
desc={
<p>
Are you sure you want to log out?
<br />
You will need to log in again to access your account.
</p>
}
confirmText='Log out'
cancelBtnText='Cancel'
/>
)
}
@@ -0,0 +1,146 @@
//
// Copyright (c) 2025 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import { HTMLAttributes, useState } from 'react'
import { z } from 'zod'
import { useForm } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { cn } from '@/lib/utils'
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { PasswordInput } from '@/components/password-input'
import { useMutation } from '@tanstack/react-query'
import { login } from '@/api/access-tokens/api'
import { setAccessToken } from '@/stores/authStore'
import { toast } from '@/hooks/use-toast'
import { AxiosError } from 'axios'
import { ToastAction } from '@/components/ui/toast'
import { useLocation, useNavigate } from '@tanstack/react-router'
import { Button } from '@/components/button'
type UserAuthFormProps = HTMLAttributes<HTMLDivElement>
const formSchema = z.object({
username: z
.string(),
password: z
.string()
.min(1, { message: 'Please enter your password' })
.min(4, { message: 'Password must be at least 4 characters long' }),
});
export function UserAuthForm({ className, ...props }: UserAuthFormProps) {
const [isLoading, setIsLoading] = useState(false)
const navigate = useNavigate()
const { search } = useLocation();
const redirect = new URLSearchParams(search).get('redirect') || '/';
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
username: 'root',
password: '',
},
})
const mutation = useMutation({
mutationFn: (password: string) => login(password),
retry: 0,
});
async function onSubmit(data: z.infer<typeof formSchema>) {
setIsLoading(true)
mutation.mutate(data.password, {
onSuccess: (rootToken) => {
setAccessToken(rootToken);
setIsLoading(false);
navigate({ to: redirect });
},
onError: (error) => {
if (error instanceof AxiosError && error.response && error.response.status === 401) {
toast({
variant: "destructive",
title: "Login Failed",
description: "Invalid password. Please try again.",
action: <ToastAction altText="Try again">Try again</ToastAction>,
})
} else {
toast({
variant: "destructive",
title: "Something went wrong",
description: (error as Error).message,
action: <ToastAction altText="Try again">Try again</ToastAction>,
})
}
setIsLoading(false)
}
});
}
return (
<div className={cn('grid gap-6', className)} {...props}>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)}>
<div className='grid gap-2'>
<FormField
control={form.control}
name='username'
render={({ field }) => (
<FormItem className='space-y-1'>
<FormLabel>Username</FormLabel>
<FormControl>
<Input disabled {...field} value={"root"} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name='password'
render={({ field }) => (
<FormItem className='space-y-1'>
<div className='flex items-center justify-between'>
<FormLabel>Password</FormLabel>
</div>
<FormControl>
<PasswordInput placeholder='********' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button className='mt-2' loading={isLoading}>
Login
</Button>
</div>
</form>
</Form>
</div>
)
}
+43
View File
@@ -0,0 +1,43 @@
//
// Copyright (c) 2025 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import Logo from '@/assets/logo.svg'
import { UserAuthForm } from './components/user-auth-form'
export default function SignIn() {
return (
<div className='container relative flex h-svh flex-col items-center justify-center'>
<div className='p-8 flex flex-col items-center'>
<img
src={Logo}
className='mb-6'
width={350}
height={350}
alt='Bichon Logo'
/>
<h2 className='mb-4 text-lg font-medium text-muted-foreground'>
Welcome to Bichon
</h2>
<div className='mx-auto flex w-full flex-col justify-center space-y-2 sm:w-[350px]'>
<UserAuthForm />
</div>
</div>
</div>
)
}