-
Notifications
You must be signed in to change notification settings - Fork 602
/
application-route-mixin.js
150 lines (129 loc) · 4.66 KB
/
application-route-mixin.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
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
import Mixin from '@ember/object/mixin';
import { A } from '@ember/array';
import { bind } from '@ember/runloop';
import { computed } from '@ember/object';
import { getOwner } from '@ember/application';
import { inject } from '@ember/service';
import Ember from 'ember';
import Configuration from './../configuration';
const { testing } = Ember;
/**
The mixin for the application route, __defining methods that are called when
the session is successfully authenticated (see
{{#crossLink "SessionService/authenticationSucceeded:event"}}{{/crossLink}})
or invalidated__ (see
{{#crossLink "SessionService/invalidationSucceeded:event"}}{{/crossLink}}).
__Using this mixin is optional.__ The session events can also be handled
manually, e.g. in an instance initializer:
```js
// app/instance-initializers/session-events.js
export function initialize(instance) {
const applicationRoute = instance.container.lookup('route:application');
const session = instance.container.lookup('service:session');
session.on('authenticationSucceeded', function() {
applicationRoute.transitionTo('index');
});
session.on('invalidationSucceeded', function() {
applicationRoute.transitionTo('bye');
});
};
export default {
initialize,
name: 'session-events',
after: 'ember-simple-auth'
};
```
__When using the `ApplicationRouteMixin` you need to specify
`needs: ['service:session']` in the application route's unit test.__
@class ApplicationRouteMixin
@module ember-simple-auth/mixins/application-route-mixin
@extends Ember.Mixin
@public
*/
export default Mixin.create({
/**
The session service.
@property session
@readOnly
@type SessionService
@public
*/
session: inject('session'),
_isFastBoot: computed(function() {
const fastboot = getOwner(this).lookup('service:fastboot');
return fastboot ? fastboot.get('isFastBoot') : false;
}),
/**
The route to transition to after successful authentication.
@property routeAfterAuthentication
@type String
@default 'index'
@public
*/
routeAfterAuthentication: computed(function() {
return Configuration.routeAfterAuthentication;
}),
init() {
this._super(...arguments);
this._subscribeToSessionEvents();
},
_subscribeToSessionEvents() {
A([
['authenticationSucceeded', 'sessionAuthenticated'],
['invalidationSucceeded', 'sessionInvalidated']
]).forEach(([event, method]) => {
this.get('session').on(event, bind(this, () => {
this[method](...arguments);
}));
});
},
/**
This method handles the session's
{{#crossLink "SessionService/authenticationSucceeded:event"}}{{/crossLink}}
event. If there is a transition that was previously intercepted by the
{{#crossLink "AuthenticatedRouteMixin/beforeModel:method"}}
AuthenticatedRouteMixin's `beforeModel` method{{/crossLink}} it will retry
it. If there is no such transition, the `ember_simple_auth-redirectTarget`
cookie will be checked for a url that represents an attemptedTransition
that was aborted in Fastboot mode, otherwise this action transitions to the
{{#crossLink "Configuration/routeAfterAuthentication:property"}}{{/crossLink}}.
@method sessionAuthenticated
@public
*/
sessionAuthenticated() {
const attemptedTransition = this.get('session.attemptedTransition');
const cookies = getOwner(this).lookup('service:cookies');
const redirectTarget = cookies.read('ember_simple_auth-redirectTarget');
if (attemptedTransition) {
attemptedTransition.retry();
this.set('session.attemptedTransition', null);
} else if (redirectTarget) {
this.transitionTo(redirectTarget);
cookies.clear('ember_simple_auth-redirectTarget');
} else {
this.transitionTo(this.get('routeAfterAuthentication'));
}
},
/**
This method handles the session's
{{#crossLink "SessionService/invalidationSucceeded:event"}}{{/crossLink}}
event. __It reloads the Ember.js application__ by redirecting the browser
to the application's root URL so that all in-memory data (such as Ember
Data stores etc.) gets cleared.
If the Ember.js application will be used in an environment where the users
don't have direct access to any data stored on the client (e.g.
[cordova](http://cordova.apache.org)) this action can be overridden to e.g.
simply transition to the index route.
@method sessionInvalidated
@public
*/
sessionInvalidated() {
if (!testing) {
if (this.get('_isFastBoot')) {
this.transitionTo(Configuration.baseURL);
} else {
window.location.replace(Configuration.baseURL);
}
}
}
});