OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015, the Dartino project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 import 'dart:convert'; | |
6 | |
7 import 'package:crypto/crypto.dart'; | |
8 import 'package:immi/dart/immi.dart'; | |
9 | |
10 // Export generated code for nodes in login_presenter.immi | |
11 import 'package:immi/dart/login_presenter.dart'; | |
12 export 'package:immi/dart/login_presenter.dart'; | |
13 | |
14 class LoginPresenter { | |
15 static const String _emptyNameOrPassword = | |
16 'Login failed. Empty name or password.'; | |
17 | |
18 Node state; | |
19 String url; | |
20 String user; | |
21 | |
22 LoginPresenter(this.url) { | |
23 state = new LoggedOutStateNode(login: login, message: ''); | |
24 } | |
25 | |
26 LoginNode present(Node prev) { | |
27 return new LoginNode(state: state); | |
28 } | |
29 | |
30 void login(String name, String password) { | |
31 user = name; | |
32 if (name == '' || password == '') { | |
33 state = new LoggedOutStateNode(login: login, | |
34 message: _emptyNameOrPassword); | |
35 } else { | |
36 HttpsRequestNode request = | |
37 new HttpsRequestNode(url: url, | |
38 authorization: _createAuthToken(name, password), | |
39 handleResponse: _handleResponse); | |
40 state = new LoginRequestStateNode(request: request); | |
41 } | |
42 } | |
43 | |
44 String _createAuthToken(String name, String password) { | |
45 List<int> bytes = UTF8.encode('$name:$password'); | |
46 String base64 = CryptoUtils.bytesToBase64(bytes); | |
47 return 'Basic $base64'; | |
48 } | |
49 | |
50 void _handleResponse(String data) { | |
51 // TODO(zarah): implement this and return depending on data. | |
52 state = new LoggedInStateNode(logout: logout, user: user); | |
53 } | |
54 | |
55 void logout() { | |
56 state = new LoggedOutStateNode(login: login, message: ''); | |
57 } | |
58 } | |
OLD | NEW |