-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathcurrent-user.js
More file actions
49 lines (47 loc) · 1.44 KB
/
current-user.js
File metadata and controls
49 lines (47 loc) · 1.44 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
import Service from "@ember/service";
import { inject as service } from "@ember/service";
import { alias } from "@ember/object/computed";
export default Service.extend({
api: service(),
store: service(),
session: service(),
onesignal: service(),
metrics: service(),
user: {},
organization: alias("user.organization"),
init() {
// restore org from store
this._super(...arguments)
this.set("organization", window.localStorage.getItem("org"));
},
async load(force = false) {
if (force) {
const { refresh_token } = this.session.data.authenticated
await this.authenticator.refreshAccessToken(refresh_token)
}
if (!force) {
const currentUser = this.user
if (currentUser && currentUser.id) {
return Promise.resolve(currentUser)
}
}
document.cookie = `auth-jwt=${this.get("session.data.authenticated.jwt")}; domain=${window.location.host}; path=/`;
return this.store
.queryRecord("user", { custom: { ext: "url", url: "me" } })
.then(user => {
this.set("user", user);
if(this.user.photo==""){
this.user.photo="https://online.codingblocks/assets/default.png"
}
this.setOrg(user.get("organization"));
this.onesignal.setExternalId(user.oneauthId)
this.metrics.identify({
distinctId: user.oneauthId
})
return user;
});
},
setOrg(org) {
this.set("organization", org);
}
});