-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.qml
217 lines (193 loc) · 5.98 KB
/
main.qml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.1
import QtQuick.LocalStorage 2.0
import "backend.js" as Backend
ApplicationWindow {
id: rootWindow
visible: true
width: 420
height: 680
title: qsTr("Login Demo")
property color backGroundColor : "#394454"
property color mainAppColor: "#6fda9c"
property color mainTextCOlor: "#f0f0f0"
property color popupBackGroundColor: "#b44"
property color popupTextCOlor: "#ffffff"
property var dataBase
FontLoader {
id: fontAwesome
name: "fontawesome"
source: "qrc:/fontawesome-webfont.ttf"
}
// Main stackview
StackView{
id: stackView
focus: true
anchors.fill: parent
}
// After loading show initial Login Page
Component.onCompleted: {
stackView.push("qrc:/LogInPage.qml") //initial page
dataBase = userDataBase()
console.log(dataBase.version)
}
//Popup to show messages or warnings on the bottom postion of the screen
Popup {
id: popup
property alias popMessage: message.text
background: Rectangle {
implicitWidth: rootWindow.width
implicitHeight: 60
color: popupBackGroundColor
}
y: (rootWindow.height - 60)
modal: true
focus: true
closePolicy: Popup.CloseOnPressOutside
Text {
id: message
anchors.centerIn: parent
font.pointSize: 12
color: popupTextCOlor
}
onOpened: popupClose.start()
}
// Popup will be closed automatically in 2 seconds after its opened
Timer {
id: popupClose
interval: 2000
onTriggered: popup.close()
}
// Create and initialize the database
function userDataBase()
{
var db = LocalStorage.openDatabaseSync("UserLoginApp", "1.0", "Login example!", 1000000);
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS UserDetails(username TEXT, password TEXT, hint TEXT)');
})
return db;
}
// Register New user
function registerNewUser(uname, pword, pword2, hint)
{
var ret = Backend.validateRegisterCredentials(uname, pword, pword2, hint)
var message = ""
switch(ret)
{
case 0: message = "Valid details!"
break;
case 1: message = "Missing credentials!"
break;
case 2: message = "Password does not match!"
break;
}
if(0 !== ret)
{
popup.popMessage = message
popup.open()
return
}
dataBase.transaction(function(tx) {
var results = tx.executeSql('SELECT password FROM UserDetails WHERE username=?;', uname);
console.log(results.rows.length)
if(results.rows.length !== 0)
{
popup.popMessage = "User already exist!"
popup.open()
return
}
tx.executeSql('INSERT INTO UserDetails VALUES(?, ?, ?)', [ uname, pword, hint ]);
showUserInfo(uname) // goto user info page
})
}
// Login users
function loginUser(uname, pword)
{
var ret = Backend.validateUserCredentials(uname, pword)
var message = ""
if(ret)
{
message = "Missing credentials!"
popup.popMessage = message
popup.open()
return
}
dataBase.transaction(function(tx) {
var results = tx.executeSql('SELECT password FROM UserDetails WHERE username=?;', uname);
if(results.rows.length === 0)
{
message = "User not registered!"
popup.popMessage = message
popup.open()
}
else if(results.rows.item(0).password !== pword)
{
message = "Invalid credentials!"
popup.popMessage = message
popup.open()
}
else
{
console.log("Login Success!")
showUserInfo(uname)
}
})
}
// Retrieve password using password hint
function retrievePassword(uname, phint)
{
var ret = Backend.validateUserCredentials(uname, phint)
var message = ""
var pword = ""
if(ret)
{
message = "Missing credentials!"
popup.popMessage = message
popup.open()
return ""
}
console.log(uname, phint)
dataBase.transaction(function(tx) {
var results = tx.executeSql('SELECT password FROM UserDetails WHERE username=? AND hint=?;', [uname, phint]);
if(results.rows.length === 0)
{
message = "User not found!"
popup.popMessage = message
popup.open()
}
else
{
pword = results.rows.item(0).password
}
})
return pword
}
// Show UserInfo page
function showUserInfo(uname)
{
stackView.replace("qrc:/UserInfoPage.qml", {"userName": uname})
}
// Logout and show login page
function logoutSession()
{
stackView.replace("qrc:/LogInPage.qml")
}
// Show Password reset page
function forgotPassword()
{
stackView.replace("qrc:/PasswordResetPage.qml")
}
// Show all users
function showAllUsers()
{
dataBase.transaction(function(tx) {
var rs = tx.executeSql('SELECT * FROM UserDetails');
var data = ""
for(var i = 0; i < rs.rows.length; i++) {
data += rs.rows.item(i).username + "\n"
}
console.log(data)
})
}
}