-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f318a3e
commit 959d3cb
Showing
6 changed files
with
301 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
"use client"; | ||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog" | ||
|
||
import { z } from "zod" | ||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "../ui/form"; | ||
import { Button } from "../ui/button"; | ||
import { Input } from "../ui/input"; | ||
import { useForm } from "react-hook-form"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { PropsWithChildren } from "react"; | ||
import { useHeadlessAIS } from "@/hooks/contexts/useHeadlessAIS"; | ||
import { Loader2 } from "lucide-react"; | ||
import { toast } from "../ui/use-toast"; | ||
|
||
// * 8~16字元 | ||
// * 至少包含1大寫英文字母 | ||
// * 至少包含1小寫英文字母 | ||
// * 至少包含1個數字 | ||
// * 密碼三代不重覆 | ||
const formSchema = z.object({ | ||
newPassword: z.string().min(8).max(16).refine((value) => { | ||
const hasLowerCase = /[a-z]/.test(value) | ||
const hasUpperCase = /[A-Z]/.test(value) | ||
const hasNumber = /[0-9]/.test(value) | ||
return hasLowerCase && hasUpperCase && hasNumber | ||
}, { | ||
message: "Password must contain at least 1 uppercase letter, 1 lowercase letter, and 1 number", | ||
}), | ||
}) | ||
|
||
const ChangePasswordDialog = ({ open, setOpen, children }: PropsWithChildren<{ open: boolean, setOpen: (s: boolean) => void }>) => { | ||
const { setAISCredentials, user } = useHeadlessAIS(); | ||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: { | ||
newPassword: "", | ||
}, | ||
}) | ||
|
||
async function onSubmit(values: z.infer<typeof formSchema>) { | ||
if (!user) return; | ||
await setAISCredentials(user.studentid, values.newPassword) | ||
setOpen(false); | ||
toast({ | ||
title: "密碼更新成功", | ||
}) | ||
} | ||
|
||
return ( | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild>{children}</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>更新密碼</DialogTitle> | ||
<DialogDescription>同學~你好像在校務資訊系統有跟新密碼哦,請也在這邊跟新!</DialogDescription> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8"> | ||
<FormField | ||
control={form.control} | ||
name="newPassword" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>New Password</FormLabel> | ||
<FormControl> | ||
<Input placeholder="Password" type="password" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild> | ||
<Button variant='destructive'>Logout</Button> | ||
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>確定要登出嗎?</DialogTitle> | ||
<DialogDescription>登出後將無法使用校務資訊系統相關功能,確定要登出嗎?</DialogDescription> | ||
</DialogHeader> | ||
<DialogClose asChild> | ||
<Button >Cancel</Button> | ||
</DialogClose> | ||
<DialogClose asChild> | ||
<Button variant='destructive' onClick={() => { | ||
setAISCredentials("", "") | ||
setOpen(false) | ||
}}>Logout</Button> | ||
</DialogClose> | ||
</DialogContent> | ||
</Dialog> | ||
<Button type="submit">{form.formState.isSubmitting ? <Loader2 className="animate-spin" /> : "Submit"}</Button> | ||
</form> | ||
</Form> | ||
</DialogContent> | ||
</Dialog> | ||
|
||
) | ||
} | ||
|
||
export default ChangePasswordDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
"use client"; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog" | ||
|
||
import { z } from "zod" | ||
import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from "../ui/form"; | ||
import { Button } from "../ui/button"; | ||
import { Input } from "../ui/input"; | ||
import { useForm } from "react-hook-form"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
|
||
|
||
|
||
// * 8~16字元 | ||
// * 至少包含1大寫英文字母 | ||
// * 至少包含1小寫英文字母 | ||
// * 至少包含1個數字 | ||
// * 密碼三代不重覆 | ||
const formSchema = z.object({ | ||
oldPassword: z.string().min(8), | ||
newPassword: z.string().min(8).max(16).refine((value) => { | ||
const hasLowerCase = /[a-z]/.test(value) | ||
const hasUpperCase = /[A-Z]/.test(value) | ||
const hasNumber = /[0-9]/.test(value) | ||
return hasLowerCase && hasUpperCase && hasNumber | ||
}, { | ||
message: "Password must contain at least 1 uppercase letter, 1 lowercase letter, and 1 number", | ||
}), | ||
confirmPassword: z.string().min(8).max(16) | ||
}).refine((data) => data.newPassword === data.confirmPassword, { | ||
message: "Passwords do not match", | ||
}); | ||
|
||
const UpdatePasswordDialog = () => { | ||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: { | ||
oldPassword: "", | ||
newPassword: "", | ||
confirmPassword: "", | ||
}, | ||
}) | ||
|
||
function onSubmit(values: z.infer<typeof formSchema>) { | ||
// Do something with the form values. | ||
// ✅ This will be type-safe and validated. | ||
console.log(values) | ||
} | ||
|
||
return ( | ||
<Dialog> | ||
<DialogTrigger>Update Password</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Password Update</DialogTitle> | ||
<DialogDescription> | ||
Due to the perfect security of changing your password, we will help you to update your password, while logged in. | ||
</DialogDescription> | ||
</DialogHeader> | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8"> | ||
<FormField | ||
control={form.control} | ||
name="oldPassword" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Old Password</FormLabel> | ||
<FormControl> | ||
<Input placeholder="password" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="newPassword" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>New Password</FormLabel> | ||
<FormControl> | ||
<Input placeholder="password" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="confirmPassword" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Confirm Password</FormLabel> | ||
<FormControl> | ||
<Input placeholder="password" {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Button type="submit">Submit</Button> | ||
</form> | ||
</Form> | ||
</DialogContent> | ||
</Dialog> | ||
|
||
) | ||
} | ||
|
||
export default UpdatePasswordDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.