-
Notifications
You must be signed in to change notification settings - Fork 0
/
discourse-userinfo.js
56 lines (47 loc) · 1.48 KB
/
discourse-userinfo.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
var data = null;
var currentUser = null;
// enter the url of your own discourse installation
var baseUrl = "https://forum.example.com"
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
currentUser = JSON.parse(this.responseText).current_user;
}
});
xhr.open("GET", baseUrl + "/session/current.json", false);
xhr.send(data);
if (currentUser) {
console.log("logged in: ", currentUser.username);
var loggedIn = true
var discourseUsername = currentUser.username;
var discourseName = currentUser.name;
var firstName = discourseName.replace(/ .*/,'');
var discourseUserId = currentUser.id;
var discourseNotifications = currentUser.unread_notifications;
var discourseMessages = currentUser.unread_private_messages;
var profileUrl = baseUrl + "/users/" + discourseUsername + ".json";
var data = null;
var currentUserInfo = null;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
currentUserInfo = JSON.parse(this.responseText).user;
}
});
xhr.open("GET", profileUrl, false);
xhr.send(data);
if (currentUserInfo) {
discourse_email = currentUserInfo.email;
}
}
else {
console.log("user is logged out");
var loggedIn = false
var discourseUsername = null;
var discourseName = null;
var discourseUserId = null;
var discourseNotifications = 0;
var discourseMessages = 0;
}