LocalStorage

This commit is contained in:
thosakwe 2016-11-27 22:34:04 -05:00
parent 319e195faa
commit 72f9054ecc
2 changed files with 20 additions and 4 deletions

View file

@ -3,7 +3,7 @@ library angel_client.browser;
import 'dart:async' show Completer, Future; import 'dart:async' show Completer, Future;
import 'dart:convert' show JSON; import 'dart:convert' show JSON;
import 'dart:html' show HttpRequest; import 'dart:html' show HttpRequest, window;
import 'angel_client.dart'; import 'angel_client.dart';
import 'auth_types.dart' as auth_types; import 'auth_types.dart' as auth_types;
export 'angel_client.dart'; export 'angel_client.dart';
@ -46,6 +46,19 @@ class Rest extends Angel {
{String type: auth_types.LOCAL, {String type: auth_types.LOCAL,
credentials, credentials,
String authEndpoint: '/auth'}) async { String authEndpoint: '/auth'}) async {
if (type == null) {
if (window.localStorage.containsKey('user') &&
window.localStorage.containsKey('token')) {
final result = new _AngelAuthResultImpl(
token: JSON.decode(window.localStorage['token']),
data: JSON.decode(window.localStorage['user']));
_authToken = result.token;
return result;
} else {
throw new Exception('Failed to authenticate via localStorage.');
}
}
final url = '$authEndpoint/$type'; final url = '$authEndpoint/$type';
if (type == auth_types.LOCAL) { if (type == auth_types.LOCAL) {
@ -58,8 +71,11 @@ class Rest extends Angel {
request request
..onLoadEnd.listen((_) { ..onLoadEnd.listen((_) {
completer final result = new _AngelAuthResultImpl.fromMap(request.response);
.complete(new _AngelAuthResultImpl.fromMap(request.response)); _authToken = result.token;
window.localStorage['token'] = JSON.encode(result.token);
window.localStorage['user'] = JSON.encode(result.data);
completer.complete(result);
}) })
..onError.listen((_) { ..onError.listen((_) {
try { try {

View file

@ -1,5 +1,5 @@
name: angel_client name: angel_client
version: 1.0.0-dev+8 version: 1.0.0-dev+9
description: Client library for the Angel framework. description: Client library for the Angel framework.
author: Tobe O <thosakwe@gmail.com> author: Tobe O <thosakwe@gmail.com>
homepage: https://github.com/angel-dart/angel_client homepage: https://github.com/angel-dart/angel_client