From 72f9054ecc900e8803c3bfdd71c6f93d37813168 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Sun, 27 Nov 2016 22:34:04 -0500 Subject: [PATCH] LocalStorage --- lib/browser.dart | 22 +++++++++++++++++++--- pubspec.yaml | 2 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/lib/browser.dart b/lib/browser.dart index 85b649df..1238c6f4 100644 --- a/lib/browser.dart +++ b/lib/browser.dart @@ -3,7 +3,7 @@ library angel_client.browser; import 'dart:async' show Completer, Future; import 'dart:convert' show JSON; -import 'dart:html' show HttpRequest; +import 'dart:html' show HttpRequest, window; import 'angel_client.dart'; import 'auth_types.dart' as auth_types; export 'angel_client.dart'; @@ -46,6 +46,19 @@ class Rest extends Angel { {String type: auth_types.LOCAL, credentials, 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'; if (type == auth_types.LOCAL) { @@ -58,8 +71,11 @@ class Rest extends Angel { request ..onLoadEnd.listen((_) { - completer - .complete(new _AngelAuthResultImpl.fromMap(request.response)); + final result = 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((_) { try { diff --git a/pubspec.yaml b/pubspec.yaml index 0dbdcc30..1a0c8983 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: angel_client -version: 1.0.0-dev+8 +version: 1.0.0-dev+9 description: Client library for the Angel framework. author: Tobe O homepage: https://github.com/angel-dart/angel_client