/
session-account.js
77 lines (75 loc) · 2.93 KB
/
session-account.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import Ember from 'ember';
const { inject: { service }, RSVP } = Ember;
export default Ember.Service.extend ({
session: service('session'),
store: service(),
serverValidationComplete: false,
server422: false,
// Create a Promise to handle a server request that validates the current LocalStorage
// If valid, then set SessionAccount User.
loadCurrentUser() {
if (!Ember.isEmpty(this.get('session.data.authenticated.userId'))) {
this.serverValidation().then(() => {
return new RSVP.Promise((resolve, reject) => {
const userId = this.get('session.data.authenticated.userId');
// Get User to Session-Account Block
if(this.get('serverValidationComplete') === true) {
return this.get('store').find('user', userId).then((user) => {
this.set('user', user);
resolve();
}).catch((reason) => {
console.log(reason.errors);
var possible404 = reason.errors.filterBy('status','404');
var possible500 = reason.errors.filterBy('status','500');
// User has tampered the Local Storage.
if(possible404.length !== 0) {
alert('404 | Invalid Account Credentials.');
this.get('session').invalidate();
}
else if(possible500.length !== 0) {
alert('500 | Server Error');
this.get('session').invalidate();
}
reject();
});
}
else if(this.get('server422') === true) {
console.log('422 | Waiting for login reload...');
resolve();
}
else{
alert('Session for Server Validation failed! Logging out!');
this.get('session').invalidate();
resolve();
}
});
});
} else {
// Session is empty...
}
},
serverValidation() {
return new RSVP.Promise((resolve) => {
var tokenAuthentication = this.get('store').createRecord('token', {
id: this.get('session.data.authenticated.userId'),
email: this.get('session.data.authenticated.email'),
authenticity_token: this.get('session.data.authenticated.token')
});
tokenAuthentication.save().then(() => {
this.set('serverValidationComplete',true);
console.log('Server Validation complete with 200');
resolve();
}).catch((reason) => {
this.set('serverValidationComplete',false);
var possible422 = reason.errors.filterBy('detail','The adapter rejected the commit because it was invalid');
// Force Page reload to sync sessions if Server replies with 422
if(possible422.length !== 0) {
console.log('422 | Sessions not fully synced due to login.');
this.set('server422',true);
}
console.log(reason);
resolve();
});
});
}
});