-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathFederatedCRUD.mxml
299 lines (241 loc) · 10.7 KB
/
FederatedCRUD.mxml
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="800" minHeight="600">
<fx:Script>
<![CDATA[
import spark.events.GridSelectionEvent;
private static const FIREBASE_API_KEY:String = "YOUR-FIREBASE-APIKEY";
private static const FIREBASE_CREATE_AUTH_URL:String = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/createAuthUri?key="+FIREBASE_API_KEY;
private static const FIREBASE_VERIFY_ASSERTION_URL:String = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key="+FIREBASE_API_KEY;
private static const FIREBASE_REDIRECT_URL:String = "https://<YOUR-PROJECT-ID>.firebaseapp.com/__/auth/handler";
private static const JOURNAL_URL:String = "https://<YOUR-PROJECt-ID>.firebaseio.com/journal/";
private var webView:StageWebView;
private var sessionId:String;
private var requestUri:String;
private var profile:Object;
private var authToken:String;
private function startAuth(provider:String):void
{
var header:URLRequestHeader = new URLRequestHeader("Content-Type", "application/json");
var myObject:Object = new Object();
myObject.continueUri = FIREBASE_REDIRECT_URL;
myObject.providerId = provider;
var request:URLRequest = new URLRequest(FIREBASE_CREATE_AUTH_URL);
request.method = URLRequestMethod.POST;
request.data = JSON.stringify(myObject);
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, authURLCreated);
loader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, errorHandler);
loader.load(request);
}
private function authURLCreated(event:flash.events.Event):void
{
var rawData:Object = JSON.parse(event.currentTarget.data);
//We store the sessionId value from the response for later use
sessionId = rawData.sessionId;
webView = new StageWebView();
webView.addEventListener(LocationChangeEvent.LOCATION_CHANGE, changeLocation);
webView.stage = this.stage;
webView.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);
//We load the URL from the response, it will automatically contain the client id, scopes and the redirect URL
webView.loadURL(rawData.authUri);
}
private function changeLocation(event:LocationChangeEvent):void
{
var location:String = webView.location;
if(location.indexOf("/__/auth/handler?code=") != -1 || location.indexOf("/__/auth/handler?state=") != -1 || location.indexOf("/__/auth/handler#state=") != -1 && location.indexOf("error") == -1){
//We are looking for a code parameter in the URL, once we have it we dispose the webview and prepare the last URLRequest
webView.removeEventListener(LocationChangeEvent.LOCATION_CHANGE, changeLocation);
webView.dispose();
requestUri = location;
getAccountInfo();
}
}
private function getAccountInfo():void
{
var header:URLRequestHeader = new URLRequestHeader("Content-Type", "application/json");
var myObject:Object = new Object();
myObject.requestUri = requestUri;
myObject.sessionId = sessionId;
myObject.returnSecureToken = true;
var request:URLRequest = new URLRequest(FIREBASE_VERIFY_ASSERTION_URL);
request.method = URLRequestMethod.POST;
request.data = JSON.stringify(myObject);
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, registerComplete);
loader.addEventListener(flash.events.IOErrorEvent.IO_ERROR, errorHandler);
loader.load(request);
}
private function registerComplete(event:flash.events.Event):void
{
//After a successful login/register we save the response into am easily accessible Object
var rawData:Object = JSON.parse(event.currentTarget.data);
profile = rawData;
refreshToken(profile.refreshToken);
}
private function refreshToken(refreshToken:String):void
{
var header:URLRequestHeader = new URLRequestHeader("Content-Type", "application/json");
var myObject:Object = new Object();
myObject.grant_type = "refresh_token";
myObject.refresh_token = refreshToken;
var request:URLRequest = new URLRequest("https://securetoken.googleapis.com/v1/token?key="+FIREBASE_API_KEY);
request.method = URLRequestMethod.POST;
request.data = JSON.stringify(myObject);
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, refreshTokenLoaded);
loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
loader.load(request);
}
private function refreshTokenLoaded(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, refreshTokenLoaded);
var rawData:Object = JSON.parse(event.currentTarget.data);
authToken = rawData.access_token;
currentState = "GridState";
}
protected function selectItem(event:GridSelectionEvent):void
{
titleInput.text = journalGrid.selectedItem.title;
descriptionInput.text = journalGrid.selectedItem.description;
deleteBtn.enabled = true;
modifyBtn.enabled = true;
}
/*
Read block
*/
private function loadJournal():void
{
var request:URLRequest = new URLRequest(JOURNAL_URL+profile.localId+".json?auth="+authToken);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, journalLoaded);
loader.load(request);
}
private function journalLoaded(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, journalLoaded);
//The JSON generated by Firebase contains the id as the node key, we use this function to add it to our Objects
var rawData:Object = JSON.parse(event.currentTarget.data);
var entriesArray:Array = new Array();
for (var parent:String in rawData)
{
var tempObject:Object = new Object();
tempObject.id = parent;
for (var child:* in rawData[parent])
{
tempObject[child] = rawData[parent][child];
}
entriesArray.push(tempObject);
tempObject = null;
}
journalGrid.dataProvider = new ArrayList(entriesArray);
titleInput.text = "";
descriptionInput.text = "";
deleteBtn.enabled = false;
modifyBtn.enabled = false;
}
/*
Insert block
*/
private function saveEntry():void
{
var myObject:Object = new Object();
myObject.title = titleInput.text;
myObject.description = descriptionInput.text;
myObject.timestamp = new Date().getTime();
var request:URLRequest = new URLRequest(JOURNAL_URL+profile.localId+".json?auth="+authToken);
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entrySent);
loader.load(request);
}
private function entrySent(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, entrySent);
loadJournal();
}
/*
Delete block
*/
private function deleteEntry():void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "DELETE");
var request:URLRequest = new URLRequest(JOURNAL_URL+profile.localId+"/"+journalGrid.selectedItem.id+".json?auth="+authToken);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryDeleted);
loader.load(request);
}
private function entryDeleted(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, entryDeleted);
loadJournal();
}
/*
Update block
*/
private function updateEntry():void
{
var header:URLRequestHeader = new URLRequestHeader("X-HTTP-Method-Override", "PATCH");
var myObject:Object = new Object();
myObject.title = titleInput.text;
myObject.description = descriptionInput.text;
var request:URLRequest = new URLRequest(JOURNAL_URL+profile.localId+"/"+journalGrid.selectedItem.id+".json?auth="+authToken);
request.data = JSON.stringify(myObject);
request.method = URLRequestMethod.POST;
request.requestHeaders.push(header);
var loader:URLLoader = new URLLoader();
loader.addEventListener(flash.events.Event.COMPLETE, entryUpdated);
loader.load(request);
}
private function entryUpdated(event:flash.events.Event):void
{
event.currentTarget.removeEventListener(flash.events.Event.COMPLETE, entryUpdated);
loadJournal();
}
/*
Universal Error Handler
*/
private function errorHandler(event:flash.events.IOErrorEvent):void
{
trace(event.currentTarget.data);
}
]]>
</fx:Script>
<s:states>
<s:State name="LoginState"/>
<s:State name="GridState" enterState="loadJournal()"/>
</s:states>
<s:Panel includeIn="LoginState" title="Select a Provider" horizontalCenter="0" verticalCenter="0">
<s:VGroup id="loginGroup" padding="10">
<s:Button id="facebookButton" label="Sign in with Facebook" width="150" click="startAuth('facebook.com')"/>
<s:Button id="twitterButton" label="Sign in with Twitter" width="150" click="startAuth('twitter.com')"/>
<s:Button id="googleButton" label="Sign in with Google" width="150" click="startAuth('google.com')"/>
</s:VGroup>
</s:Panel>
<s:VGroup includeIn="GridState" left="10" right="10" top="10" bottom="10">
<s:DataGrid id="journalGrid" selectionChange="selectItem(event)" width="100%" height="100%">
<s:columns>
<s:ArrayList>
<s:GridColumn headerText="ID" dataField="id" />
<s:GridColumn headerText="Title" dataField="title" />
<s:GridColumn headerText="Description" dataField="description" />
<s:GridColumn headerText="Timestamp" dataField="timestamp" />
</s:ArrayList>
</s:columns>
</s:DataGrid>
<s:TextInput id="titleInput" prompt="Type a Title" width="100%"/>
<s:TextArea id="descriptionInput" prompt="Type a Description" width="100%" height="100"/>
<s:HGroup width="100%" horizontalAlign="center">
<s:Button id="deleteBtn" label="Delete" click="deleteEntry()"/>
<s:Button id="modifyBtn" label="Modify" click="updateEntry()"/>
<s:Button id="createBtn" label="Create" click="saveEntry()"/>
</s:HGroup>
</s:VGroup>
</s:WindowedApplication>