basic password validation

This commit is contained in:
hay-kot 2021-02-24 19:25:18 -09:00
commit 2abc773022
5 changed files with 69 additions and 29 deletions

View file

@ -0,0 +1,15 @@
export const validators = {
data() {
return {
emailRule: v =>
!v ||
/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(v) ||
"E-mail must be valid",
existsRule: value => !!value || "Field Required",
minRule: v =>
v.length >= 8 || "Use 8 characters or more for your password",
};
},
};

View file

@ -19,7 +19,7 @@
</v-card-title> </v-card-title>
<v-divider></v-divider> <v-divider></v-divider>
<v-card-text> <v-card-text>
<v-row > <v-row>
<v-col cols="12" md="3" align="center" justify="center"> <v-col cols="12" md="3" align="center" justify="center">
<v-avatar color="accent" size="120" class="mr-2" v-if="!loading"> <v-avatar color="accent" size="120" class="mr-2" v-if="!loading">
<img <img
@ -30,9 +30,21 @@
</v-col> </v-col>
<v-col cols="12" md="9"> <v-col cols="12" md="9">
<v-form> <v-form>
<v-text-field label="Full Name" v-model="user.fullName"> <v-text-field
label="Full Name"
required
v-model="user.fullName"
:rules="[existsRule]"
validate-on-blur
>
</v-text-field> </v-text-field>
<v-text-field label="Email" v-model="user.email"> <v-text-field
label="Email"
:rules="[emailRule]"
validate-on-blur
required
v-model="user.email"
>
</v-text-field> </v-text-field>
<v-text-field <v-text-field
label="Family" label="Family"
@ -66,36 +78,49 @@
</v-card-title> </v-card-title>
<v-divider></v-divider> <v-divider></v-divider>
<v-card-text> <v-card-text>
<v-form> <v-form ref="passChange">
<v-text-field <v-text-field
v-model="password.current" v-model="password.current"
prepend-icon="mdi-lock" prepend-icon="mdi-lock"
label="Current Password" label="Current Password"
:type="showPassword.current ? 'text' : 'password'" :rules="[existsRule]"
:append-icon="showPassword.current ? 'mdi-eye' : 'mdi-eye-off'" validate-on-blur
:type="showPassword ? 'text' : 'password'"
@click:append="showPassword.current = !showPassword.current" @click:append="showPassword.current = !showPassword.current"
></v-text-field> ></v-text-field>
<v-text-field <v-text-field
v-model="password.newOne" v-model="password.newOne"
prepend-icon="mdi-lock" prepend-icon="mdi-lock"
label="New Password" label="New Password"
:type="showPassword.newOne ? 'text' : 'password'" :rules="[minRule]"
:append-icon="showPassword.newOne ? 'mdi-eye' : 'mdi-eye-off'" :type="showPassword ? 'text' : 'password'"
@click:append="showPassword.newOne = !showPassword.newOne" @click:append="showPassword.newOne = !showPassword.newOne"
></v-text-field> ></v-text-field>
<v-text-field <v-text-field
v-model="password.newTwo" v-model="password.newTwo"
prepend-icon="mdi-lock" prepend-icon="mdi-lock"
label="Confirm Password" label="Confirm Password"
:type="showPassword.newTwo ? 'text' : 'password'" :rules="[
:append-icon="showPassword.newTwo ? 'mdi-eye' : 'mdi-eye-off'" password.newOne === password.newTwo || 'Password must match',
]"
validate-on-blur
:type="showPassword ? 'text' : 'password'"
@click:append="showPassword.newTwo = !showPassword.newTwo" @click:append="showPassword.newTwo = !showPassword.newTwo"
></v-text-field> ></v-text-field>
</v-form> </v-form>
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
<v-btn icon @click="showPassword = !showPassword" :loading="passwordLoading">
<v-icon v-if="!showPassword">mdi-eye-off</v-icon>
<v-icon v-else> mdi-eye </v-icon>
</v-btn>
<v-spacer></v-spacer> <v-spacer></v-spacer>
<v-btn color="accent" class="mr-2" @click="changePassword"> <v-btn
color="accent"
class="mr-2"
@click="changePassword"
>
<v-icon left> mdi-lock </v-icon> <v-icon left> mdi-lock </v-icon>
{{ $t("settings.change-password") }} {{ $t("settings.change-password") }}
</v-btn> </v-btn>
@ -109,22 +134,21 @@
// import AvatarPicker from '@/components/AvatarPicker' // import AvatarPicker from '@/components/AvatarPicker'
import UploadBtn from "@/components/UI/UploadBtn"; import UploadBtn from "@/components/UI/UploadBtn";
import api from "@/api"; import api from "@/api";
import { validators } from "@/mixins/validators";
export default { export default {
components: { components: {
UploadBtn, UploadBtn,
}, },
mixins: [validators],
data() { data() {
return { return {
passwordLoading: false,
password: { password: {
current: "", current: "",
newOne: "", newOne: "",
newTwo: "", newTwo: "",
}, },
showPassword: { showPassword: false,
current: false,
newOne: false,
newTwo: false,
},
loading: false, loading: false,
user: { user: {
fullName: "Change Me", fullName: "Change Me",
@ -133,7 +157,6 @@ export default {
admin: true, admin: true,
id: 1, id: 1,
}, },
showAvatarPicker: false,
}; };
}, },
@ -159,13 +182,20 @@ export default {
this.loading = false; this.loading = false;
}, },
async changePassword() { async changePassword() {
this.paswordLoading = true;
let data = { let data = {
currentPassword: this.password.current, currentPassword: this.password.current,
newPassword: this.password.newOne, newPassword: this.password.newOne,
}; };
await api.users.changePassword(this.user.id, data); if (this.$refs.passChange.validate()) {
await api.users.changePassword(this.user.id, data);
}
this.paswordLoading = false;
}, },
}, },
}; };
</script> </script>
<style>
</style>

View file

@ -3,6 +3,7 @@
// import Vuetify from "./plugins/vuetify"; // import Vuetify from "./plugins/vuetify";
import { vueApp } from "../main"; import { vueApp } from "../main";
const notifyHelpers = { const notifyHelpers = {
baseCSS: "notify-base", baseCSS: "notify-base",
error: "notify-error-color", error: "notify-error-color",

View file

@ -1,8 +0,0 @@
const validators = {
emailRules: v =>
!v ||
/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(v) ||
"E-mail must be valid",
};
export { validators };

View file

@ -86,13 +86,15 @@ async def update_password(
match_passwords = verify_password( match_passwords = verify_password(
password_change.current_password, current_user.password password_change.current_password, current_user.password
) )
print(match_passwords)
match_id = current_user.id == id match_id = current_user.id == id
if match_passwords and match_id: if match_passwords and match_id:
new_password = get_password_hash(password_change.new_password) new_password = get_password_hash(password_change.new_password)
db.users.update_password(session, id, new_password) db.users.update_password(session, id, new_password)
return SnackResponse.success("Password Updated")
return SnackResponse.success("Password Updated") else:
return SnackResponse.error("Existing password does not match")
@router.delete("/{id}") @router.delete("/{id}")