diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-11-24 20:57:41 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-11-24 20:57:41 -0500 |
| commit | 612561818f907f0f9988247c82ec158ba4494986 (patch) | |
| tree | 8bbc773dad7a6364cbdb8aed2ef4cab04854f49a /StoneIsland/platforms/ios/www/js/sdk/auth.js | |
| parent | d548a7d02ce6349ca9a820b6824e4374b759812b (diff) | |
build
Diffstat (limited to 'StoneIsland/platforms/ios/www/js/sdk/auth.js')
| -rw-r--r-- | StoneIsland/platforms/ios/www/js/sdk/auth.js | 104 |
1 files changed, 98 insertions, 6 deletions
diff --git a/StoneIsland/platforms/ios/www/js/sdk/auth.js b/StoneIsland/platforms/ios/www/js/sdk/auth.js index cea0054c..8a72dd46 100644 --- a/StoneIsland/platforms/ios/www/js/sdk/auth.js +++ b/StoneIsland/platforms/ios/www/js/sdk/auth.js @@ -7,7 +7,7 @@ }, */ -var auth = (function(){ +var auth = sdk.auth = (function(){ var auth = {} auth.appname = is_iphone ? "native-iphone-stoneisland/1.0.0" : "native-android-stoneisland/1.0.0" @@ -16,18 +16,110 @@ var auth = (function(){ auth.access_token = "" auth.user_id = -1 + + auth.next_view = null + auth.deferred_product = null // ios: integrate keychain api // android: cordova.file.externalRootDirectory api - auth.set_user = function(user_id, access_token){ - // persist user data - auth.user_id = user_id + auth.init = function(fn){ + console.log("AUTH INIT") + auth.get_user(function(){ + if (auth.logged_in()) { + sdk.account.checkin({ + success: function(data){ + fn && fn( auth.logged_in() ) + } + }) + auth.get_cart() + } + else { + fn && fn( auth.logged_in() ) + } + }) + } + + auth.set_user = function(user_id, access_token, cb){ auth.access_token = access_token + auth.user_id = user_id + + localStorage.setItem("yoox.access_token", access_token) + localStorage.setItem("yoox.user_id", user_id) + cb && cb() } auth.get_user = function(cb){ - // fetch user data + auth.access_token = localStorage.getItem("yoox.access_token") || "" + auth.user_id = localStorage.getItem("yoox.user_id") || -1 + cb && cb() + } + auth.clear_user = function(cb){ + auth.access_token = "" + auth.user_id = -1 + localStorage.removeItem("yoox.access_token") + localStorage.removeItem("yoox.user_id") + cb && cb() + } + + auth.set_cart = function(cart_id, cart_token, cb){ + localStorage.setItem("yoox.cart_token", cart_token) + localStorage.setItem("yoox.cart_id", cart_id) + cb && cb() + } + auth.get_cart = function(cb){ + sdk.cart.token = localStorage.getItem("yoox.cart_token") || "" + sdk.cart.id = localStorage.getItem("yoox.cart_id") || -1 + cb && cb() + } + auth.clear_cart = function(cb){ + sdk.cart.token = "" + sdk.cart.id = -1 + localStorage.removeItem("yoox.cart_token") + localStorage.removeItem("yoox.cart_id") + cb && cb() + } + auth.create_cart = function(cb){ + if (auth.has_cart()) { return cb() } + sdk.cart.initialize({ + success: function(data){ + sdk.cart.set_user({ + success: function(){ + auth.set_cart(sdk.cart.id, sdk.cart.token, function(){ + cb && cb() + }) + } + }) + } + }) + } + auth.add_deferred_product_to_cart = function(cb){ + // auth.deferred_product + if (! auth.deferred_product) { + cb && cb() + return + } + sdk.cart.add_item({ + data: auth.deferred_product, + success: function(){ + console.log("ADDED") + cb && cb() + } + }) + auth.deferred_product = null + app.header.increment_cart_count() + } + + auth.log_out = function(){ + auth.clear_user() + auth.clear_cart() + auth.view_logged_out() + } + auth.logged_in = function(){ + return (auth.user_id !== -1) && (auth.user_id !== "undefined") + } + auth.has_cart = function(){ + return (sdk.cart.id !== -1) && (sdk.cart.id !== "undefined") } return auth -})() +})()
\ No newline at end of file |
