Skip to content

Commit 77a6f52

Browse files
committed
integrate oneauth cookie login
1 parent ee77e32 commit 77a6f52

File tree

10 files changed

+86
-35
lines changed

10 files changed

+86
-35
lines changed

app/adapters/application.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
1818
return this._super(url, method, hash);
1919
},
2020
authorize(xhr) {
21-
const { jwt } = this.get('session.data.authenticated');
21+
const jwt = this.get('session.data.token');
2222
if (isPresent(jwt)) {
2323
xhr.setRequestHeader('Authorization', `JWT ${jwt}`);
2424
}

app/mixins/authenticated-route-mixin.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import Mixin from '@ember/object/mixin';
44

55
export default Mixin.create(AuthenticatedRouteMixin, {
66
router: service(),
7+
session: service(),
78

89
beforeModel () {
910
localStorage.setItem('redirectionPath', window.location.pathname)
10-
return this._super(...arguments)
11+
12+
if(!this.session.isAuthenticated) {
13+
router.transitionTo('application')
14+
}
1115
}
1216
})

app/pods/application/route.js

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import Route from '@ember/routing/route';
2-
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
32
import UTMCookieRouteMixin from 'hackerblocks/mixins/utm-cookie-route-mixin';
43
import { inject as service } from '@ember/service';
54
import { isNone } from '@ember/utils';
65
import config from 'hackerblocks/config/environment'
76

8-
export default Route.extend(ApplicationRouteMixin, UTMCookieRouteMixin, {
7+
export default Route.extend(UTMCookieRouteMixin, {
98
session: service(),
109
currentUser: service(),
1110
queryParams: {
@@ -28,31 +27,11 @@ export default Route.extend(ApplicationRouteMixin, UTMCookieRouteMixin, {
2827

2928
async beforeModel (transition) {
3029
this._super(...arguments)
31-
if (!isNone(transition.to.queryParams.code)) {
32-
if (this.get('session.isAuthenticated')) {
33-
return this.transitionTo('index', { queryParams: { code: undefined } })
34-
}
35-
// we have ?code qp
36-
const { code } = transition.to.queryParams
37-
38-
try {
39-
await (
40-
this.session.authenticate('authenticator:jwt', { identification: code, password: code, code })
41-
.then(() => this.currentUser.load())
42-
)
43-
return this.transitionTo('index', { queryParams: { code: undefined } })
44-
} catch (error) {
45-
console.log(error)
46-
if (error.json.err === 'USER_EMAIL_NOT_VERIFIED') {
47-
this.transitionTo('error', {
48-
queryParams: {
49-
errorCode: 'USER_EMAIL_NOT_VERIFIED',
50-
code: undefined
51-
}
52-
})
53-
}
54-
}
30+
31+
if(!this.session.isAuthenticated) {
32+
window.location.href = config.nuxtPublicUrl
5533
}
34+
5635
},
5736

5837
async model () {

app/pods/components/nav-bar/component.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import config from 'hackerblocks/config/environment'
77
export default class NavBarComponent extends Component {
88
@service session
99
@service currentUser
10+
@service router
1011
@alias('currentUser.user') user
1112

1213
hideHamburgerNav = true
@@ -17,7 +18,8 @@ export default class NavBarComponent extends Component {
1718
this.toggleProperty('hideHamburgerNav')
1819
}
1920

20-
get logoutLink () {
21-
return config.oneauthURL + '/logout?redirect=' + config.publicUrl + '/logout'
21+
@action
22+
logout () {
23+
return this.router.transitionTo('logout')
2224
}
2325
}

app/pods/components/nav-bar/template.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
</li>
7373
<div class="divider-h my-3"></div>
7474
<li>
75-
<a class="dark-grey" href={{logoutLink}} >
75+
<a class="dark-grey" {{action 'logout'}} >
7676
Logout
7777
</a>
7878
</li>

app/pods/logout/route.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export default class Logout extends Route {
77

88
async beforeModel(transition) {
99
try {
10-
await this.api.request("/jwt/logout")
1110
window.localStorage.clear()
1211
await this.session.invalidate();
1312
} catch (err) {

app/services/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export default AjaxService.extend({
88
host: env.apiHost,
99
contentType: 'application/json; charset=utf-8',
1010
namespace: '/api/v2',
11-
headers: computed ('session.data.authenticated.jwt', function () {
11+
headers: computed ('session.data.token', function () {
1212
let headers = {};
13-
const jwt = this.get('session.data.authenticated.jwt');
13+
const jwt = this.get('session.data.token');
1414
if (jwt) {
1515
headers['Authorization'] = `JWT ${jwt}`;
1616
}

app/services/session.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import Service from '@ember/service';
2+
import { inject as service } from '@ember/service';
3+
import Ember from 'ember';
4+
import ENV from 'hackerblocks/config/environment';
5+
6+
export default Service.extend({
7+
api: service(),
8+
router: service(),
9+
cookieName: 'cb_auth',
10+
isAuthenticated: false,
11+
data: null,
12+
13+
init() {
14+
this._super(...arguments)
15+
this.validate()
16+
},
17+
18+
async authenticate(data) {
19+
await this.api.request(`/jwt/login?grant_code=${data.code}`)
20+
.catch(err => console.log(err))
21+
this.validate()
22+
this.router.transitionTo('application')
23+
},
24+
25+
getCookieToken() {
26+
if (!Ember.isEmpty(document.cookie)) {
27+
const cookies = document.cookie.split(';')
28+
const cookie = cookies.filter(c => c.includes(this.cookieName))[0]
29+
if (!Ember.isEmpty(cookie)) {
30+
return cookie.split('=')[1]
31+
}
32+
}
33+
},
34+
35+
async invalidate() {
36+
await this.api.request('/jwt/logout').catch(err => console.log(err))
37+
window.location.href = `${ENV.oneauthURL}/logout?returnTo=${ENV.publicUrl}`
38+
},
39+
40+
validate() {
41+
const token = this.getCookieToken()
42+
43+
if (Ember.isEmpty(token)) {
44+
this.set('data', null)
45+
this.set('isAuthenticated', false)
46+
} else {
47+
this.set('data', { token })
48+
this.set('isAuthenticated', true)
49+
}
50+
}
51+
52+
})

config/environment.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ module.exports = function (environment) {
5050
// ENV.APP.LOG_TRANSITIONS = true;
5151
// ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
5252
// ENV.APP.LOG_VIEW_LOOKUPS = true;
53+
ENV.nuxtPublicUrl = "http://localhost:8081"
5354
ENV.publicUrl = 'http://test.hackerblocks/app/';
5455
// ENV.publicUrl = "http://localhost:4200/app/";
5556
ENV.apiHost = 'http://test.hackbackend';
5657
// ENV.apiHost = "http://localhost:3000";
57-
ENV.oneauthURL = "https://account.codingblocks.com";
58+
ENV.oneauthURL = "http://localhost:3838";
5859
ENV.clientId = 3680446660;
5960
}
6061

@@ -73,6 +74,7 @@ module.exports = function (environment) {
7374
}
7475

7576
if (environment === "staging") {
77+
ENV.nuxtPublicUrl = "https://hack.codingblocks.xyz"
7678
ENV.publicUrl = "http://hack.codingblocks.xyz/app/";
7779
ENV.apiHost = "https://hack-api.codingblocks.xyz";
7880
ENV.oneauthURL = "https://account.codingblocks.com";
@@ -81,6 +83,7 @@ module.exports = function (environment) {
8183
}
8284

8385
if (environment === "production") {
86+
ENV.nuxtPublicUrl = "https://hack.codingblocks.com"
8487
ENV.publicUrl = "https://hack.codingblocks.com/app";
8588
ENV.apiHost = "https://hack-api.codingblocks.com";
8689
ENV.oneauthURL = "https://account.codingblocks.com";
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
4+
module('Unit | Service | session', function(hooks) {
5+
setupTest(hooks);
6+
7+
// Replace this with your real tests.
8+
test('it exists', function(assert) {
9+
let service = this.owner.lookup('service:session');
10+
assert.ok(service);
11+
});
12+
});

0 commit comments

Comments
 (0)