Skip to content

Commit

Permalink
Add option to use dummy user when Auth0 is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Malte-Hansen committed Nov 13, 2024
1 parent 393fe3b commit 523b3c1
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 45 deletions.
21 changes: 21 additions & 0 deletions app/components/page-setup/dev-login.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { on } from '@ember/modifier';
import Service, { inject as service } from '@ember/service';
import Component from '@glimmer/component';

export default class DevLogin extends Component<IArgs> {
@service('router')
router!: any;

enableDevMode = () => {
sessionStorage.setItem('no-auth', 'true');
this.router.transitionTo('/landscapes');
};

<template>
<div class='d-flex justify-content-center'>
<button type='submit' {{on 'click' this.enableDevMode}}>
Sign in for Development
</button>
</div>
</template>
}
6 changes: 3 additions & 3 deletions app/routes/base-route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export default class BaseRoute extends Route {
}

@action
error(error: Auth0Error) {
if (error.description) {
error(error: Auth0Error | undefined) {
if (error && error.description) {
this.toastHandlerService.showErrorToastMessage(error.description);
}
if (error.statusCode !== 429) {
if (!error || error.statusCode !== 429) {
this.auth.logout();
}
return true;
Expand Down
17 changes: 16 additions & 1 deletion app/routes/callback.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
import Router from '@ember/routing/router';

export default class Callback extends Route {}
/**
* TODO
*
* @class Callback-Route
* @extends Ember.Route
*/
export default class Callback extends Route {
@service
router!: Router;

async afterModel() {
this.router.transitionTo('landscapes');
}
}
53 changes: 35 additions & 18 deletions app/services/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Auth0Error, Auth0UserProfile } from 'auth0-js';
import Auth0Lock from 'auth0-lock';
import debugLogger from 'ember-debug-logger';
import Evented from '@ember/object/evented';
import { tracked } from '@glimmer/tracking';

export default class Auth extends Service.extend(Evented) {
private debug = debugLogger('Auth');
Expand All @@ -13,20 +14,31 @@ export default class Auth extends Service.extend(Evented) {

private lock: Auth0LockStatic | null = null;

@tracked
user: Auth0UserProfile | undefined = undefined;

accessToken: string | undefined = undefined;

constructor() {
super(...arguments);
async initAuthentication() {
// No auth in dev mode
const noAuth = await this.isDevMode();

if (ENV.auth0.enabled === 'false') {
// no-auth
this.set('user', ENV.auth0.profile);
this.set('accessToken', ENV.auth0.accessToken);
return;
if (noAuth) {
this.user = ENV.auth0.profile;
this.accessToken = ENV.auth0.accessToken;
} else {
this.initAuthLock();
}
}

async isDevMode() {
return (
ENV.auth0.enabled === 'false' ||
sessionStorage.getItem('no-auth') === 'true'
);
}

initAuthLock() {
this.lock = new Auth0Lock(ENV.auth0.clientId, ENV.auth0.domain, {
auth: {
redirectUrl: ENV.auth0.callbackUrl,
Expand All @@ -49,7 +61,8 @@ export default class Auth extends Service.extend(Evented) {

this.lock.on('authenticated', async (authResult) => {
await this.setUser(authResult.accessToken);
this.set('accessToken', authResult.accessToken);

this.accessToken = authResult.accessToken;
this.router.transitionTo(ENV.auth0.routeAfterLogin);
});
}
Expand All @@ -61,12 +74,12 @@ export default class Auth extends Service.extend(Evented) {
// Since testem seems to enter routes but not render their templates,
// the login container does not necessarily exist, which results in an error
if (!document.getElementById('auth0-login-container')) {
this.router.transitionTo('login');
return;
}
if (this.lock) {
this.lock.show();
} else {
// no-auth
this.router.transitionTo(ENV.auth0.routeAfterLogin);
}
}
Expand All @@ -75,7 +88,7 @@ export default class Auth extends Service.extend(Evented) {
* Use the token to set our user
*/
setUser(token: string) {
// once we have a token, we are able to go get the users information
// Once we have a token, we are able to go get the users information
return new Promise<Auth0UserProfile>((resolve, reject) => {
if (this.lock) {
this.lock.getUserInfo(
Expand All @@ -85,7 +98,7 @@ export default class Auth extends Service.extend(Evented) {
reject(_err);
} else {
this.debug('User set', profile);
this.set('user', profile);
this.user = profile;
resolve(profile);
}
}
Expand All @@ -101,7 +114,11 @@ export default class Auth extends Service.extend(Evented) {
/**
* Check if we are authenticated using the auth0 library's checkSession
*/
checkLogin() {
async checkLogin() {
if (!this.user) {
await this.initAuthentication();
}

// check to see if a user is authenticated, we'll get a token back
return new Promise((resolve, reject) => {
if (this.lock) {
Expand All @@ -118,7 +135,7 @@ export default class Auth extends Service.extend(Evented) {
} else {
try {
await this.setUser(authResult.accessToken);
this.set('accessToken', authResult.accessToken);
this.accessToken = authResult.accessToken;
resolve(authResult);
this.trigger('user_authenticated', this.user);
} catch (e) {
Expand All @@ -127,7 +144,7 @@ export default class Auth extends Service.extend(Evented) {
}
});
} else {
// no-auth
// No authentication
this.set('user', ENV.auth0.profile);
this.set('accessToken', ENV.auth0.accessToken);
resolve({});
Expand All @@ -139,16 +156,16 @@ export default class Auth extends Service.extend(Evented) {
* Get rid of everything in sessionStorage that identifies this user
*/
logout() {
this.set('user', undefined);
this.set('accessToken', undefined);
this.user = undefined;
this.accessToken = undefined;
sessionStorage.setItem('no-auth', 'false');
if (this.lock) {
this.lock.logout({
clientID: ENV.auth0.clientId,
returnTo: ENV.auth0.logoutReturnUrl,
});
} else {
// no-auth
this.router.transitionTo('/');
this.router.transitionTo('login');
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/templates/login.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<div id='auth0-login-container'></div>
<div id='auth0-login-container'></div>
<PageSetup::DevLogin />
10 changes: 0 additions & 10 deletions tests/acceptance/badroute-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,4 @@ import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';

module('Acceptance | badroute', (hooks) => {
setupApplicationTest(hooks);

test('visiting /badroute', async (assert) => {
await visit('/badroute');
assert.equal(
currentURL(),
'/login',
'Every non valid route is redirected to login page.'
);
});
});
14 changes: 2 additions & 12 deletions tests/acceptance/index-test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
// import { visit, currentURL } from '@ember/test-helpers';
// import { setupApplicationTest } from 'ember-qunit';

module('Acceptance | index', (hooks) => {
setupApplicationTest(hooks);

test('visiting /index', async (assert) => {
await visit('/index');
assert.equal(
currentURL(),
'/login',
'Index route replaces current URL with login route.'
);
});
});

0 comments on commit 523b3c1

Please sign in to comment.