Skip to content

Commit fdcb505

Browse files
committed
Show "This page requires authentication" page for protected routes if unauthenticated
1 parent a21b0a2 commit fdcb505

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

app/controllers/catch-all.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Controller from '@ember/controller';
22
import { action } from '@ember/object';
3+
import { inject as service } from '@ember/service';
34

45
export default class CatchAllController extends Controller {
6+
@service session;
7+
58
@action reload() {
69
this.model.transition.retry();
710
}

app/routes/-authenticated-route.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import Route from '@ember/routing/route';
22
import { inject as service } from '@ember/service';
33

44
export default class AuthenticatedRoute extends Route {
5-
@service notifications;
65
@service router;
76
@service session;
87

@@ -12,9 +11,12 @@ export default class AuthenticatedRoute extends Route {
1211
let result = await this.session.loadUserTask.last;
1312

1413
if (!result.currentUser) {
15-
this.notifications.error('Please log in to proceed');
1614
this.session.savedTransition = transition;
17-
this.router.transitionTo('index');
15+
this.router.replaceWith('catch-all', {
16+
transition,
17+
loginNeeded: true,
18+
title: 'This page requires authentication',
19+
});
1820
}
1921
}
2022
}

app/styles/catch-all.module.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,9 @@
1818
composes: button-reset from '../styles/shared/buttons.module.css';
1919
composes: link from '../styles/application.module.css';
2020
font-weight: 500;
21+
22+
&[disabled] {
23+
color: var(--grey600);
24+
cursor: wait;
25+
}
2126
}

app/templates/catch-all.hbs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,17 @@
44

55
<h1 local-class="title" data-test-title>{{or @model.title "Page not found"}}</h1>
66

7-
{{#if @model.tryAgain}}
7+
{{#if @model.loginNeeded}}
8+
<button
9+
type="button"
10+
disabled={{this.session.loginTask.isRunning}}
11+
local-class="link"
12+
data-test-login
13+
{{on "click" (perform this.session.loginTask)}}
14+
>
15+
Log in with GitHub
16+
</button>
17+
{{else if @model.tryAgain}}
818
<button type="button" local-class="link" data-test-try-again {{on "click" this.reload}}>Try Again</button>
919
{{else}}
1020
<button type="button" local-class="link" data-test-go-back {{on "click" this.back}}>Go Back</button>

tests/acceptance/dashboard-test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import { visit } from '../helpers/visit-ignoring-abort';
1010
module('Acceptance | Dashboard', function (hooks) {
1111
setupApplicationTest(hooks);
1212

13-
test('redirects to / when not logged in', async function (assert) {
13+
test('shows "page requires authentication" error when not logged in', async function (assert) {
1414
await visit('/dashboard');
15-
assert.equal(currentURL(), '/');
16-
assert.dom('[data-test-notification-message]').hasText('Please log in to proceed');
15+
assert.equal(currentURL(), '/dashboard');
16+
assert.dom('[data-test-title]').hasText('This page requires authentication');
17+
assert.dom('[data-test-login]').exists();
1718
});
1819

1920
test('shows the dashboard when logged in', async function (assert) {

tests/acceptance/invites-test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,11 @@ module('Acceptance | /me/pending-invites', function (hooks) {
4040
return { nanomsg, user };
4141
}
4242

43-
test('redirects to / when not logged in', async function (assert) {
43+
test('shows "page requires authentication" error when not logged in', async function (assert) {
4444
await visit('/me/pending-invites');
45-
assert.equal(currentURL(), '/');
46-
assert.dom('[data-test-notification-message]').hasText('Please log in to proceed');
45+
assert.equal(currentURL(), '/me/pending-invites');
46+
assert.dom('[data-test-title]').hasText('This page requires authentication');
47+
assert.dom('[data-test-login]').exists();
4748
});
4849

4950
test('list all pending crate owner invites', async function (assert) {

0 commit comments

Comments
 (0)