Skip to content

Show "This page requires authentication" page for protected routes if unauthenticated #4315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/controllers/catch-all.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Controller from '@ember/controller';
import { action } from '@ember/object';
import { inject as service } from '@ember/service';

export default class CatchAllController extends Controller {
@service session;

@action reload() {
this.model.transition.retry();
}
Expand Down
8 changes: 5 additions & 3 deletions app/routes/-authenticated-route.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class AuthenticatedRoute extends Route {
@service notifications;
@service router;
@service session;

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

if (!result.currentUser) {
this.notifications.error('Please log in to proceed');
this.session.savedTransition = transition;
this.router.transitionTo('index');
this.router.replaceWith('catch-all', {
transition,
loginNeeded: true,
title: 'This page requires authentication',
});
}
}
}
5 changes: 5 additions & 0 deletions app/styles/catch-all.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@
composes: button-reset from '../styles/shared/buttons.module.css';
composes: link from '../styles/application.module.css';
font-weight: 500;

&[disabled] {
color: var(--grey600);
cursor: wait;
}
}
12 changes: 11 additions & 1 deletion app/templates/catch-all.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@

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

{{#if @model.tryAgain}}
{{#if @model.loginNeeded}}
<button
type="button"
disabled={{this.session.loginTask.isRunning}}
local-class="link"
data-test-login
{{on "click" (perform this.session.loginTask)}}
>
Log in with GitHub
</button>
{{else if @model.tryAgain}}
<button type="button" local-class="link" data-test-try-again {{on "click" this.reload}}>Try Again</button>
{{else}}
<button type="button" local-class="link" data-test-go-back {{on "click" this.back}}>Go Back</button>
Expand Down
7 changes: 4 additions & 3 deletions tests/acceptance/dashboard-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { visit } from '../helpers/visit-ignoring-abort';
module('Acceptance | Dashboard', function (hooks) {
setupApplicationTest(hooks);

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

test('shows the dashboard when logged in', async function (assert) {
Expand Down
7 changes: 4 additions & 3 deletions tests/acceptance/invites-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ module('Acceptance | /me/pending-invites', function (hooks) {
return { nanomsg, user };
}

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

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