11<template >
22 <div class =" content" >
3- <div class =" box settings" >
4- Theme settings
5- <div class =" theme-buttons" >
3+ <div class =" settings" >
4+ <div class =" box" >
5+ Theme settings
6+ <div class =" theme-buttons" >
67 <button
78 @click =" setTheme('flash')"
89 class =" btn"
1516 :disabled =" theme == 'batman'" >
1617 Batman
1718 </button >
19+ </div >
1820 </div >
21+ <form class =" form" @submit.prevent =" savePassword" >
22+ <div v-if =" isCurrentPassword" class =" box" >
23+ Current Password
24+ <div >
25+ <input v-model =" oldPassword" type =" password" />
26+ </div >
27+ </div >
28+ <div class =" box" >
29+ New Password
30+ <div >
31+ <input v-model =" newPassword" type =" password" />
32+ </div >
33+ </div >
34+ <div v-if =" errorMessage" class =" text-center text-error" >{{ errorMessage }}</div >
35+ <div >
36+ <button type =" submit" class =" btn save" >{{ buttonCaption }}</button >
37+ </div >
38+ </form >
1939 </div >
2040 </div >
2141</template >
2242
2343<script >
24- import utils , { SETTINGSKEY } from ' ../assets/js/utils' ;
44+ import bcrypt from ' bcryptjs' ;
45+ import utils , { SETTINGSKEY , PASSWORDKEY } from ' ../assets/js/utils' ;
2546
2647export default {
2748 name: ' Settings' ,
2849 data () {
2950 return {
30- theme: ' '
51+ theme: ' ' ,
52+ newPassword: ' ' ,
53+ oldPassword: ' ' ,
54+ isCurrentPassword: ' ' ,
55+ errorMessage: ' ' ,
56+ buttonCaption: ' Save Password'
3157 };
3258 },
3359 methods: {
@@ -40,11 +66,44 @@ export default {
4066 }
4167 utils .saveConfiguration (SETTINGSKEY , { theme });
4268 this .theme = theme;
69+ },
70+ async getTheme () {
71+ const settings = await utils .getData (SETTINGSKEY );
72+ this .theme = settings .theme || ' flash' ;
73+ },
74+ async getPassword () {
75+ const password = await utils .getData (SETTINGSKEY );
76+ this .isCurrentPassword = password .password ;
77+ },
78+ async savePassword () {
79+ if (this .newPassword ) {
80+ if (this .isCurrentPassword
81+ && ! bcrypt .compareSync (this .oldPassword , this .isCurrentPassword )) {
82+ this .errorMessage = ' Invalid old password' ;
83+ } else {
84+ const hashedPassword = bcrypt .hashSync (this .newPassword , 10 );
85+ await utils .saveConfiguration (SETTINGSKEY , {
86+ [PASSWORDKEY ]: hashedPassword
87+ });
88+ this .buttonCaption = ' Saving...' ;
89+ this .errorMessage = ' ' ;
90+ setTimeout (() => {
91+ this .resetFields (hashedPassword);
92+ }, 1500 );
93+ }
94+ }
95+ },
96+ resetFields (hashedPassword ) {
97+ this .newPassword = ' ' ;
98+ this .oldPassword = ' ' ;
99+ this .errorMessage = ' ' ;
100+ this .isCurrentPassword = hashedPassword;
101+ this .buttonCaption = ' Save Password' ;
43102 }
44103 },
45- async mounted () {
46- const settings = await utils . getData ( SETTINGSKEY );
47- this .theme = settings . theme || ' flash ' ;
104+ mounted () {
105+ this . getTheme ( );
106+ this .getPassword () ;
48107 }
49108};
50109 </script >
0 commit comments