1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # User preference window and other user-related view
21 # Update the content of `lbl_welcome`
25 self.text
= if name
!= null then
26 "Logged in as %0".t
.format
(name
)
27 else "Not logged in".t
30 # Set `text` to welcome an authentified user or invite to authentify
34 self.text
= if name
!= null then
35 "Welcome %0".t
.format
(name
)
40 # User preference window
45 var layout
= new ListLayout(parent
=self)
47 private var layout_user_options
= new VerticalLayout(parent
=layout
)
49 private var lbl_user_options_title
= new Label(parent
=layout_user_options
,
50 text
="Account options".t
)
52 private var lbl_welcome
= new Label(parent
=layout_user_options
)
53 private var but_logout
= new Button(parent
=layout_user_options
, text
="Logout".t
)
55 # Refesh displayed text
58 lbl_user_options_title
.size
= 1.5
59 lbl_welcome
.set_user_name
60 but_logout
.enabled
= app
.user
!= null
65 redef fun on_event
(event
)
67 if event
isa ButtonPressEvent then
68 var sender
= event
.sender
69 if sender
== but_logout
then
81 # Window for signing up a new user or logging in
85 private var list
= new ListLayout(parent
=self)
86 private var lbl_feedback
= new Label(parent
=list
, text
="Welcome")
88 private var layout_login
= new VerticalLayout(parent
=list
)
91 # First the login options
94 private var name_line
= new HorizontalLayout(parent
=layout_login
)
95 private var lbl_name
= new Label(parent
=name_line
, text
="Username".t
)
96 private var txt_name
= new TextInput(parent
=name_line
, text
=app
.user
)
99 private var pass_line
= new HorizontalLayout(parent
=layout_login
)
100 private var lbl_pass
= new Label(parent
=pass_line
, text
="Password".t
)
101 private var txt_pass
= new TextInput(parent
=pass_line
, is_password
=true)
102 private var lbl_pass_desc
= new Label(parent
=layout_login
, size
= 0.5,
103 text
="Passwords must be composed of at least 6 characters.".t
)
105 private var but_login
= new Button(parent
=layout_login
, text
="Login".t
)
108 # Then, the signup options
110 private var layout_register
= new VerticalLayout(parent
=list
)
112 private var lbl_signup_desc
= new Label(parent
=layout_register
, size
= 0.5,
113 text
="Fill the following fields to sign up.".t
)
116 private var pass_line2
= new HorizontalLayout(parent
=layout_register
)
117 private var lbl_pass2
= new Label(parent
=pass_line2
, text
="Repeat password".t
)
118 private var txt_pass2
= new TextInput(parent
=pass_line2
, is_password
=true)
121 private var email_line
= new HorizontalLayout(parent
=layout_register
)
122 private var lbl_email
= new Label(parent
=email_line
, text
="Email".t
)
123 private var txt_email
= new TextInput(parent
=email_line
)
125 private var but_signup
= new Button(parent
=layout_register
, text
="Signup".t
)
127 redef fun on_event
(event
)
129 if debug
then print
"BenitluxWindow::on_event {event}"
131 if event
isa ButtonPressEvent then
132 var sender
= event
.sender
133 if sender
== but_login
or sender
== but_signup
then
135 var name
= txt_name
.text
136 if name
== null or not name
.name_is_ok
then
137 feedback
"Invalid username.".t
141 var pass
= txt_pass
.text
142 if pass
== null or not pass
.pass_is_ok
then
143 feedback
"Invalid password.".t
147 if sender
== but_login
then
148 feedback
"Logging in...".t
149 (new LoginOrSignupAction(self, "rest/login?name={name}&pass={pass.pass_hash}")).start
150 else if sender
== but_signup
then
151 if pass
!= txt_pass2
.text
then
152 feedback
"Passwords do not match.".t
156 var email
= txt_email
.text
157 if email
== null or email
.is_empty
then
158 feedback
"Invalid email".t
162 feedback
"Signing up...".t
163 (new LoginOrSignupAction(self, "rest/signup?name={name}&pass={pass.pass_hash}&email={email}")).start
171 # Show lasting feedback to the user in a label
172 fun feedback
(text
: String) do lbl_feedback
.text
= text
176 # Async RESTful actions
178 # Async request for login in or signing up
179 class LoginOrSignupAction
180 super WindowHttpRequest
182 redef type W
: SignupWindow
184 init do affected_views
.add_all
([window
.but_login
, window
.but_signup
])
186 redef fun on_load
(res
, status
)
188 if intercept_error
(res
) then return
190 if not res
isa LoginResult then
191 on_fail
new Error("Server sent unexpected data {res or else "null"}")
195 app
.token
= res
.token
196 app
.user
= res
.user
.name
201 redef fun feedback
(text
) do window
.feedback text