I have a login token stored in the storage of the platform and every time I want to use it I need to use async in the function, Can someone plz help me to store it in a globally variable and access it everywhere without ASYNC in function.
Now if somewhere I need to use the token in a function I have to use the ASYNC with my function otherwise it will not work.
This imho will be impossible to achieve as the underlying APIs (http, local storage, etc) are async by nature. So you will have to develop software with that in mind - that the results of a call will come in later.
This means discarding imperative thinking, moving onto functional thinking etc.
Use service in ionic to store the value and import the service wherever you want and get the token there. Follow the below steps. ionic generate service myappService
Import inside your component where your token is fetched. import {MyAppServiceService} from "/myappService.service"; ... ... constructor(private app_service:MyAppServiceService){} this.app_service.token=your_token;
In your service add a variable named token.
Thats it.
While this pattern is great, it does expose the variable to each consumer in a way it can be muted directly without the service knowing it ( and likely any other consumer)
This breaches the single responsibility structure you actually want to achieve
A way to go around this, is to use Subject and Observable patterns
A. I never interact with HTTP from pages or components, only services. Pages and components are easier to write if they live in a world containing only streams of business objects, and this facilitates frequently-seen evolution patterns. You can add and remove caches, mock backends out for testing, &c without touching any view layer code.
B. Do not fall into the trap of blasting as many custom headers as you can, thinking “what can it hurt?”. The #1 offender here is manually setting Content-Type. Angular’s HttpClient is smart, and automatically deals with Content-Type based on the type of thing you give it for request bodies. An object will automatically become application/json. When you set things manually that you don’t have to, three bad things happen: the code becomes harder to read because you’re obscuring the important stuff, you have another vector for stupid typos, and if somebody changes the backend transfer format, your manual setting will become wrong but still in charge.
C. async is syntactic sugar. It is never needed for anything. I personally consider it a solution in search of a problem. There is absolutely nothing wrong with never using it and explicitly declaring future return types like Observable<Foo> and Promise<Foo> instead.
D. Despite all the great general-purpose in-app communication stuff in this thread, for the specific question in the OP, interceptors are pretty much the “mic drop” answer. See the Angular docs for sample code. Your interceptor here can take a reference to the service that exposes an Observable of your authentication token, and then every HTTP request will be decorated with the Authorization header.
E. If you want to get fancy, you can use a custom X-No-Authorization header to signal to the interceptor that it should leave particular requests alone. In that case, the interceptor should eat that header before it gets out to the network, but not add the Authorization header. Implementation is left as an exercise for the reader.
Maybe not a direct answer to your question, but dit you consider using a HttpInterceptor? With an interceptor you only have one function that adds the token to every request to your API.
There are tons of examples out there, but here’s one to get the idea: