This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
/
tutorial.js
117 lines (100 loc) · 4.44 KB
/
tutorial.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Accepting remoteStorage accounts in your web app
// ------------------------------------------------
var tutorial = (function() {
// `getStorageInfo` takes a user address ("user@host") and a callback as its
// arguments. The callback will get an error code, and a `storageInfo`
// object. If the error code is `null`, then the `storageInfo` object will
// contain data required to access the remoteStorage.
function connect(userAddress, callback) {
remoteStorage.getStorageInfo(userAddress, function(error, storageInfo) {
if(error) {
alert('Could not load storage info');
console.log(error);
} else {
console.log('Storage info received:');
console.log(storageInfo);
}
callback(error, storageInfo);
});
}
// Getting data from the "public" category doesn't require any credentials.
// For writing to a user's public data, or reading/writing any of the other
// categories, we need to do an OAuth request first to obtain a token.
// This method opens a popup that sends the user to the OAuth dialog of the
// remoteStorage provider.
function authorize(categories) {
var storageInfo = JSON.parse(localStorage.getItem('userStorageInfo'));
var redirectUri = location.protocol + '//' + location.host + '/receive_token.html';
// `createOAuthAddress` takes the `storageInfo`, the categories that we
// intend to access and a redirect URI that the storage provider sends the
// user back to after authorization.
// That page extracts the token and sends it back to us, which is
// [described here](token.html).
var oauthPage = remoteStorage.createOAuthAddress(storageInfo, categories, redirectUri);
var popup = window.open(oauthPage);
}
// To get the OAuth token we listen for the `message` event from the
// receive_token.html that sends it back to us.
window.addEventListener('message', function(event) {
if(event.origin == location.protocol +'//'+ location.host) {
console.log('Received an OAuth token: ' + event.data);
localStorage.setItem('bearerToken', event.data);
helper.setAuthorizedState(helper.isAuthorized());
}
}, false);
// To get data from the remoteStorage, we need to create a client with
// the `createClient` method. It takes the object that we got via the
// `getStorageInfo` call and the category we want to access. If the
// category is any other than "public", we also have to provide the OAuth
// token.
function getData(category, key, callback) {
var storageInfo = JSON.parse(localStorage.getItem('userStorageInfo'));
var client;
if (category == 'public') {
client = remoteStorage.createClient(storageInfo, 'public');
} else {
var token = localStorage.getItem('bearerToken');
client = remoteStorage.createClient(storageInfo, category, token);
}
// The client's `get` method takes a key and a callback. The callback will
// be invoked with an error code and the data.
client.get(key, function(error, data) {
if(error) {
alert('Could not find "' + key + '" in category "' + category + '" on the remoteStorage');
console.log(error);
} else {
if (data == undefined) {
console.log('There wasn\'t anything for "' + key + '" in category "' + category + '"');
} else {
console.log('We received this for key "' + key + '" in category "' + category + '": ' + data);
}
}
callback(error, data);
});
}
// For saving data we use the client's `put` method. It takes a key, the
// value and a callback. The callback will be called with an error code,
// which is `null` on success.
function putData(category, key, value, callback) {
var storageInfo = JSON.parse(localStorage.getItem('userStorageInfo'));
var token = localStorage.getItem('bearerToken');
var client = remoteStorage.createClient(storageInfo, category, token);
client.put(key, value, function(error) {
if (error) {
alert('Could not store "' + key + '" in "' + category + '" category');
console.log(error);
} else {
console.log('Stored "' + value + '" for key "' + key + '" in "' + category + '" category');
}
callback(error);
});
}
// Now all that's left is to bind the events from the UI elements to
// these actions, as can be seen [here](app.html).
return {
connect: connect,
authorize: authorize,
getData: getData,
putData: putData,
};
})();