|
1 |
| -import Route from '@ember/routing/route'; |
2 |
| -import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin'; |
3 |
| -import { inject as service } from '@ember/service'; |
4 |
| -import { isNone } from '@ember/utils'; |
| 1 | +import Route from "@ember/routing/route"; |
| 2 | +import ApplicationRouteMixin from "ember-simple-auth/mixins/application-route-mixin"; |
| 3 | +import UtmCookieRouteMixin from "../../mixins/utm-cookie-route" |
| 4 | +import { inject as service } from "@ember/service"; |
| 5 | +import { isNone } from "@ember/utils"; |
5 | 6 |
|
6 |
| -export default Route.extend(ApplicationRouteMixin, { |
| 7 | +export default Route.extend(ApplicationRouteMixin, UtmCookieRouteMixin, { |
7 | 8 | session: service(),
|
8 | 9 | currentUser: service(),
|
9 |
| - store: service (), |
| 10 | + store: service(), |
10 | 11 | headData: service(),
|
| 12 | + onesignal: service(), |
11 | 13 | metrics: service(), // !important: keep this here to init trackers for all routes
|
12 | 14 | // routeAfterAuthentication: 'dashboard',
|
13 | 15 | queryParams: {
|
14 | 16 | code: {
|
15 |
| - refreshModel: true |
| 17 | + refreshModel: true, |
| 18 | + repalce: true |
16 | 19 | }
|
17 | 20 | },
|
18 | 21 | async beforeModel(transition) {
|
| 22 | + this._super(...arguments) |
19 | 23 | this.metrics; // !important: keep this here to init trackers for all routes
|
20 |
| - if (!isNone(transition.to.queryParams.code)) { |
21 |
| - if (this.get('session.isAuthenticated')) { |
22 |
| - return '' |
23 |
| - // return this.transitionTo({ queryParams: { code: undefined } }) |
24 |
| - } |
25 |
| - // we have ?code qp |
26 |
| - const { code } = transition.to.queryParams |
27 |
| - |
28 |
| - return this.session.authenticate('authenticator:jwt', { identification: code, password: code, code }) |
29 |
| - .then(() => this.currentUser.load()) |
30 |
| - .then(user => { |
31 |
| - // if user belongs to an org, redirect to the domain |
32 |
| - if(user.get('organization')) { |
33 |
| - this.transitionTo(user.get('organization')) |
34 |
| - } |
35 |
| - }) |
36 |
| - .catch(error => { |
37 |
| - if (error.err === 'USER_EMAIL_NOT_VERIFIED') { |
38 |
| - return this.transitionTo('error', { |
39 |
| - queryParams: { |
40 |
| - errorCode: 'USER_EMAIL_NOT_VERIFIED' |
41 |
| - } |
42 |
| - }) |
43 |
| - } |
44 |
| - if (error.name == 'USER_LOGGED_IN_ELSEWHERE') { |
45 |
| - return this.transitionTo('login-blocker', { |
46 |
| - queryParams: { |
47 |
| - code: null, |
48 |
| - token: error.logout_token |
49 |
| - } |
50 |
| - }) |
51 |
| - } |
52 |
| - }); |
| 24 | + this.onesignal; |
| 25 | + if (!isNone(transition.to.queryParams.code)) { |
| 26 | + if (this.get("session.isAuthenticated")) { |
| 27 | + return ""; |
| 28 | + // return this.transitionTo({ queryParams: { code: undefined } }) |
53 | 29 | }
|
54 |
| - }, |
55 |
| - model () { |
56 |
| - if (this.get('session.isAuthenticated')) { |
57 |
| - return this.currentUser.load().then (user => { |
58 |
| - try { |
59 |
| - OneSignal.getUserId ().then (userId => { |
60 |
| - if (! userId) { |
61 |
| - throw new Error ('player ID not found') |
62 |
| - } |
63 |
| - |
64 |
| - const player = this.store.createRecord ('player') |
65 |
| - |
66 |
| - player.set ('playerId', userId) |
| 30 | + // we have ?code qp |
| 31 | + const { code } = transition.to.queryParams; |
67 | 32 |
|
68 |
| - return player.save () |
69 |
| - }) |
70 |
| - .then (result => console.log ('playerId set!')) |
71 |
| - .catch (error => console.error (error)) |
| 33 | + return this.session |
| 34 | + .authenticate("authenticator:jwt", { |
| 35 | + identification: code, |
| 36 | + password: code, |
| 37 | + code |
| 38 | + }) |
| 39 | + .then(() => this.currentUser.load()) |
| 40 | + .then(user => { |
| 41 | + // if user belongs to an org, redirect to the domain |
| 42 | + if (user.get("organization")) { |
| 43 | + this.transitionTo(user.get("organization")); |
| 44 | + } |
| 45 | + }) |
| 46 | + .catch(error => { |
| 47 | + if (error.err === "USER_EMAIL_NOT_VERIFIED") { |
| 48 | + return this.transitionTo("error", { |
| 49 | + queryParams: { |
| 50 | + errorCode: "USER_EMAIL_NOT_VERIFIED" |
| 51 | + } |
| 52 | + }); |
72 | 53 | }
|
73 |
| - catch (error) { |
74 |
| - console.error(error) |
| 54 | + if (error.name == "USER_LOGGED_IN_ELSEWHERE") { |
| 55 | + return this.transitionTo("login-blocker", { |
| 56 | + queryParams: { |
| 57 | + code: null, |
| 58 | + token: error.logout_token |
| 59 | + } |
| 60 | + }); |
75 | 61 | }
|
76 |
| - return user |
77 | 62 | });
|
78 |
| - } |
| 63 | + } |
79 | 64 | },
|
80 |
| - |
81 |
| - setupController(controller, model){ |
82 |
| - this._super(controller, model) |
83 |
| - controller.set('model', model) |
84 |
| - |
85 |
| - // later(function(){ |
86 |
| - // controller.set('code', undefined) |
87 |
| - // }) |
| 65 | + model() { |
| 66 | + return this.get("session.isAuthenticated") && this.currentUser.load() |
88 | 67 | },
|
89 | 68 | afterModel(model) {
|
90 |
| - this.set('headData.title', 'Coding Blocks Online') |
| 69 | + this.set("headData.title", "Coding Blocks Online") |
91 | 70 | },
|
92 | 71 |
|
93 |
| - |
94 |
| -}) |
| 72 | + setupController(controller, model) { |
| 73 | + this._super(controller, model); |
| 74 | + controller.set("model", model); |
| 75 | + } |
| 76 | +}); |
0 commit comments