Skip to content

Commit

Permalink
Added login states and route redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
dpigera committed Jan 7, 2025
1 parent 3362ed4 commit c1ab0fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ export default class Router extends EmberRouter {
}

Router.map(function() {
this.route('login', { path: '/' });
this.route('login');
this.route('dashboard');
});
22 changes: 22 additions & 0 deletions app/routes/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default class ApplicationRoute extends Route {
@service session;
@service router;

async beforeModel() {
await this.session.setup();

// If user is authenticated and trying to access the root or login route,
// redirect them to dashboard
if (this.session.isAuthenticated) {
const currentRouteName = this.router.currentRouteName;
if (currentRouteName === 'login' || currentRouteName === 'index' || currentRouteName === null) {
this.router.transitionTo('dashboard');
}
}else{
this.router.transitionTo('login');
}
}
}

0 comments on commit c1ab0fb

Please sign in to comment.