-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-auth.html
More file actions
158 lines (143 loc) · 3.62 KB
/
app-auth.html
File metadata and controls
158 lines (143 loc) · 3.62 KB
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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-ajax/iron-ajax.html">
<!--
`app-auth`
Simple polymer custom element for authentication module in your application
@demo demo/index.html
### Event Throw
`app-auth` will throw these custom events so host element can listen to them:
Event Name | Description | Thrown Attribute(s)
`init-error` | Error event because login and logout URL not set | -
`login` | `app-auth` currently logging in given username | username:String
`logout` | `app-auth` currently logging out given username | username:String
-->
<dom-module id="app-auth">
<template>
<style>
</style>
<iron-ajax
id="ajax"
method="[[method]]"
url="[[_ajaxUrl]]"
params="[[params]]"
body="[[_body]]"
loading="[[working]]"
handle-as="json"
debounce-duration="300"
lastResponse={{_response}}
lastError={{_error}}
>
</iron-ajax>
</template>
<script>
Polymer({
is: 'app-auth',
hostAttributes: {
hidden: true,
},
properties: {
username: {
type: String,
},
password: {
type: String,
},
method: {
type: String,
value: "POST",
},
url: {
type: String,
value: "",
},
loginUrl: {
type: String,
value: "",
},
logoutUrl: {
type: String,
value: "",
},
payload: {
type: String,
value: null,
},
user: {
type: Object,
readOnly: true,
notify: true,
},
authorized: {
type: Boolean,
value: false,
notify: true,
},
working: {
type: Boolean,
value: false,
notify: true,
},
params: {
type: Object,
},
session: {
type: Boolean,
value: false,
},
_ajaxUrl: {
type: String,
value: null,
},
_body: {
type: Object,
},
_response: {
type: Object,
observer: '_onAjaxResponse'
},
_error: {
type: Object,
},
},
// Element Lifecycle
attached: function() {
this.listen(this.$.ajax, 'response', '_onAjaxResponse');
this.listen(this.$.ajax, 'error', '_onAjaxError');
},
// Private Methods
_onAjaxResponse: function() {
if(data.detail.__data__.status == 200) {
this._response = data.detail.__data__.response;
if(this.payload != null) {
this.user = this._response[this.payload];
} else {
this.user = this._response;
}
this.authorized = true;
this.fire('success', {user: this.user});
} else {
this._error = data.detail.__data__.response;
this.fire('failed', {message: this._error});
}
},
_onAjaxError: function(data) {
this._error = data.detail.error;
this.fire('failed', {message: this._error});
},
login: function() {
this.fire('login', {username: this.username});
this._ajaxUrl = this.url + this.loginUrl;
this._body = {
username: this.username,
password: this.password
};
this.$.ajax.generateRequest();
},
logout: function() {
this.fire('logout', {username: this.username});
this._ajaxUrl = this.url + this.logoutUrl;
this.$.ajax.generateRequest();
},
});
</script>
</dom-module>