service that runs at angular app instance creation regardless of inital router endpoint

37 Views Asked by At

I have an authentication service that I want run on app instance creation. Im not sure how to do this.

One of the many things that this service will do is check if localstorage contains a JWT (I am using a angular app so I am protected from xss) and if it does, grabs the sign in permissions and info from the server.

I want this service to run regardless of where my inital route point is.

so it works at

root

and

root/totally/awesome/route/point

make sense? I'm asking this question because I honestly have no clue. I am currently checking the life cycle hooks

So no lifecycle hooks to use:

does the app component run on app instance regardless of the view currently navigated to? If so I think I could just do a ngOnInit lifecycle hook in the app component and inject the service app wide with the providers array.

so it seems I need to take the app.component.ts file and add a constructor and ngOnInit lifecycle hook that will check the authorization service. and do the necessary work needed to log a user in and display the relevant permissions

1

There are 1 best solutions below

0
On

15 views and I have to answer my own question.

so I wrote the following in my app.component.ts

import { Component, OnInit } from '@angular/core';
@Component({

  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers:[VenueAdminVolleyService]
})
export class AppComponent implements OnInit{
  title = 'app';



  ngOnInit(){
    console.log('this shit is working yo!');
  }
}

and navitated to both

root

and then closed the browser and navigated to

root/awesome/router/position/with/really/cool/stuff

and both times I got this shit is working yo!

in the console

which means this code is working. Now I just have to change the console.log to the check log in function in my authentication service!