Check authenticated or not from route

Instead of using {{session.isAuthenticated}} in template, I need to check whether it is authenticated or not in Route?

I’m assuming “session” is a service, maybe Ember Simple Auth? If that’s the case you can inject the service into your route like:

import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
  session: service(),
...

Then you can use it by calling this.get('session.isAuthenticated') or if you’re on Ember 3.3+ you can probably just do this.session.isAuthenticated

2 Likes

If your session service is already injected into your routes, it may be as simple as this.get('session.isAuthenticated'). If you need to inject your session service into your routes, look into the inject() function.

1 Like