summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/sdk/auth.js
blob: c154653256fcd90424ae731eac946f495586748f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
      headers: {
        "x-yoox-appname": auth.appname,
        "x-yoox-account-token": auth.access_token,
        "x-yoox-device": auth.device,
        "x-yoox-api-key": auth.apikey,
      },
*/

var auth = sdk.auth = (function(){
  var auth = {}

  auth.appname = is_iphone ? "native-iphone-stoneisland/1.0.0" : "native-android-stoneisland/1.0.0"
  auth.apikey = "U2FsdGVkX18fThqg9bF0/ZgE9Jg948hn8O9EXli4B2729nAESCQaexv//M5+7+za"
  auth.device = "smartphone"

  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.init = function(fn){
    auth.get_user(function(){
      if (auth.logged_in()) {
        auth.get_cart()
      }
      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){
    access_token = localStorage.getItem("yoox.access_token") || ""
    user_id = localStorage.getItem("yoox.user_id") || -1
    cb && cb()
  }
  auth.clear_user = function(cb){
    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){
    cart.token = localStorage.getItem("yoox.cart_token") || ""
    cart.id = localStorage.getItem("yoox.cart_id") || -1
    cb && cb()
  }
  auth.clear_cart = function(cb){
    localStorage.removeItem("yoox.cart_token")
    localStorage.removeItem("yoox.cart_id")
    cb && cb()
  }
  auth.create_cart = function(cb){
    cart.initialize().done(function(){
      cart.set_user().done(function(){
        auth.set_cart(sdk.cart.id, sdk.cart.token, function(){
          cb && cb()
        })
      })
    })
  }
  auth.defer_add_to_cart = function(){
    // auth.deferred_product
  }
  auth.log_out = function(){
    auth.access_token = ""
    auth.user_id = -1
    cart.token = ""
    cart.id = -1
    auth.view_logged_out()
  }
  auth.logged_in = function(){
    return (auth.user_id !== -1)
  }
  auth.has_cart = function(){
    return (cart.id !== -1)
  }
  
  return auth
})()