Add: Auth protection & error notifications
This commit is contained in:
parent
5d508c4731
commit
51668bae9d
6 changed files with 53 additions and 37 deletions
|
|
@ -77,6 +77,11 @@ export const useAuth = () => {
|
|||
}
|
||||
|
||||
if (data.value) {
|
||||
// Validation: Only allow STUDENT role to login
|
||||
if (data.value.user.role.code !== 'STUDENT') {
|
||||
return { success: false, error: 'Email ไม่ถูกต้อง' }
|
||||
}
|
||||
|
||||
token.value = data.value.token
|
||||
refreshToken.value = data.value.refreshToken // Save refresh token
|
||||
|
||||
|
|
|
|||
|
|
@ -20,29 +20,29 @@ export function useFormValidation() {
|
|||
return re.test(email)
|
||||
}
|
||||
|
||||
const validateField = (value: string, rules: ValidationRule, fieldName: string, formData?: Record<string, string>): string | null => {
|
||||
const validateField = (value: string, rules: ValidationRule, fieldName: string, formData?: Record<string, string>, messages?: Record<string, string>): string | null => {
|
||||
if (rules.required && !value.trim()) {
|
||||
return `กรุณากรอก${fieldName}`
|
||||
return messages?.required || `กรุณากรอก${fieldName}`
|
||||
}
|
||||
|
||||
if (rules.minLength && value.length < rules.minLength) {
|
||||
return `${fieldName}ต้องมีอย่างน้อย ${rules.minLength} ตัวอักษร`
|
||||
return messages?.minLength || `${fieldName}ต้องมีอย่างน้อย ${rules.minLength} ตัวอักษร`
|
||||
}
|
||||
|
||||
if (rules.maxLength && value.length > rules.maxLength) {
|
||||
return `${fieldName}ต้องไม่เกิน ${rules.maxLength} ตัวอักษร`
|
||||
return messages?.maxLength || `${fieldName}ต้องไม่เกิน ${rules.maxLength} ตัวอักษร`
|
||||
}
|
||||
|
||||
if (rules.email && value && !validateEmail(value)) {
|
||||
return 'รูปแบบอีเมลไม่ถูกต้อง'
|
||||
return messages?.email || 'รูปแบบอีเมลไม่ถูกต้อง'
|
||||
}
|
||||
|
||||
if (rules.pattern && value && !rules.pattern.test(value)) {
|
||||
return `${fieldName}รูปแบบไม่ถูกต้อง`
|
||||
return messages?.pattern || `${fieldName}รูปแบบไม่ถูกต้อง`
|
||||
}
|
||||
|
||||
if (rules.match && formData && value !== formData[rules.match]) {
|
||||
return 'รหัสผ่านไม่ตรงกัน'
|
||||
return messages?.match || 'รหัสผ่านไม่ตรงกัน'
|
||||
}
|
||||
|
||||
if (rules.custom) {
|
||||
|
|
@ -54,13 +54,13 @@ export function useFormValidation() {
|
|||
|
||||
const validate = (
|
||||
formData: Record<string, string>,
|
||||
validationRules: Record<string, { rules: ValidationRule; label: string }>
|
||||
validationRules: Record<string, { rules: ValidationRule; label: string; messages?: Record<string, string> }>
|
||||
): boolean => {
|
||||
const newErrors: FieldErrors = {}
|
||||
let isValid = true
|
||||
|
||||
for (const [fieldKey, config] of Object.entries(validationRules)) {
|
||||
const error = validateField(formData[fieldKey] || '', config.rules, config.label, formData)
|
||||
const error = validateField(formData[fieldKey] || '', config.rules, config.label, formData, config.messages)
|
||||
if (error) {
|
||||
newErrors[fieldKey] = error
|
||||
isValid = false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue