forked from imtumbleweed/primary
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
261 lines (241 loc) · 7.86 KB
/
Copy pathindex.html
File metadata and controls
261 lines (241 loc) · 7.86 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
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
<html>
<head>
<title>Primary</title>
<link rel="stylesheet" type="text/css" href="style/lavacode.css" />
<script type="module">
/* Front-end API */
class User {
constructor() {}
}
class Password {
constructor() {}
}
// Create payload -- redundant everywhere, now in neat make() function
let make = function(payload) {
return {
method: 'post',
headers: { Accept: 'application/json' },
body: JSON.stringify(payload)
};
};
/* Create user in database
payload = {
username : "felix",
email : "felix@thecat.net",
first_name : "Felix",
last_name : "Felicis",
password : "Password123",
type : "normal" } */
User.create = function(payload) {
fetch('/api/user/register', make(payload))
.then(promise => promise.json())
.then(json => {
if (json.success)
console.log(
`User <${payload.username}> <${payload.email_address}> was successfully registered.`
);
else
console.log(
`User with email address <${payload.email_address}> already exists!`
);
});
};
/* Read (get) user from database:
payload = {
id: 1,
token: "ABC123xyz" } */
User.get = function(payload) {
fetch('/api/user/get', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Update user table in database
payload = {
id: 1,
token: "ABC123xyz" } */
User.update = function(payload) {
fetch('/api/user/update', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Delete user from database
payload = {
id: 1,
token: "ABC123xyz" } */
User.delete = function(payload) {
fetch('/api/user/delete', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Login and create user session (if credentials match)
payload = {
username: "username",
password: "password" } */
User.login = function(payload) {
fetch('/api/user/login', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Confirm verification code (optional)
payload = {
email: "felix@gmail.com",
verification_code: "12345AbcXyz" } */
User.verify = function(payload) {
fetch('/api/user/verify', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
// Authenticate user (check if session exists and receive back auth id)
// token.id = "ABC123xyz"
// token.timestamp = 12334545670
// (token in session on server must match token.id from localStorage)
User.authenticate = function(payload) {
fetch('/api/user/authenticate', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Post a tweet
payload = {
id: 1,
payload.message: "hello" } */
User.tweet = function(payload) {
fetch('/api/tweet/post', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Like a tweet
payload = {id: 1,
tweet_id: 100 } */
User.like = function(payload) {
fetch('/api/tweet/like', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Comment on a tweet
payload = {id: 1,
tweet_id: 100,
message: "I'm right, #yourewrong"}; */
User.comment = function(payload) {
fetch('/api/tweet/comment', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
};
/* Send password reset link
payload = {email_address: "john@example.com"} */
Password.email = function(payload) {
fetch('/api/password/email', make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
}
/* Reset password using a given token
payload: {password: 'secret', password_confirmation: 'secret'} */
Password.reset = function(payload) {
fetch(`/api/password/reset/${window.reset_token.value}`, make(payload))
.then(promise => promise.json())
.then(json => {
console.log(json);
});
}
// Because this is <script type = "module", to make User object
// globally available so it can be used in HTML attribute events,
// we have to manually add it to window object
window.onload = () => {
window.User = User;
window.Password = Password;
};
</script>
<style type="text/css">
#path1 {
position: absolute;
display: block;
}
</style>
</head>
<body>
<div id="body">
<div>
<b>API Tests</b><br />
<input type="text" id="un" placeholder="username" style="width: 90px" />
<input type="text" id="email_address" placeholder="email address" />
<input
id="user_button"
type="button"
onclick="let payload = { 'email_address': window.email_address.value, 'username': window.un.value, 'password': 'password'}; User.create( payload )"
value="register user"
/><br />
<input
type="button"
onclick="User.update({id: 1, password_sha3: 'newpass123A'})"
value="update user properties"
/><br />
<input
type="button"
onclick="User.get({id:1})"
value="get user where user.id = 1"
/><br />
<input
type="button"
onclick="User.get({id:100000000})"
value="get user where user.id = 100000000"
/><br />
<input
type="button"
onclick="User.verify({user:'felix', verify_id:'12345'})"
value="verify user (same as click on verify link in email)"
/><br />
<input
type="button"
onclick="User.authenticate({token:'token_test_12345'})"
value="authenticate (check if token exists in session table)"
/><br />
<input
type="button"
onclick="User.login({username:'felix', password:'password123'})"
value="log in as user felix / password (check session)"
/><br />
<input
type="button"
onclick="User.tweet({user:'felix',message:'hello there, this is my tweet.'})"
value="tweet as authenticated user"
/><br />
<input
type="button"
onclick="User.like({id:{user:1, tweet:1}})"
value="like tweet id=1 by user id=1"
/><br />
<input
type="button"
onclick="Password.email({email_address: window.email_address.value})"
value="send password reset link"
/><br />
<input type="text" id="reset_token" placeholder="reset token">
<input
type="button"
onclick="Password.reset({password: 'secret', password_confirmation: 'secret'})"
value="reset password"
/><br />
</div>
</div>
<div id="footer">2020 Primary. Copyright Something Something.</div>
</body>
</html>