Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 48983

Make constant value accessible public

$
0
0

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.

A function I build.

async ngOnInit() {
    const token = await Storage.get({ key: TTOKEN_KEY });
    const takenValue = token.value;

    let headers = new HttpHeaders({
      'Content-Type': 'application/json;',
      Authorization: 'Bearer ' + takenValue,
    });
    let options = { headers: headers };
    this.http
      .get('https://website.com/api/Details/'+this.cid, options)
      .subscribe((res: any) => {
        this.profile = res;
      });
}

My login.service.ts file is where I store generated TOKEN at the platform.

 login(credentials: { email; password }): Observable<any> {
    return this.http.post(`https://website.com/api/login`, credentials).pipe(
      map((data: any) => data.success.token),
      switchMap((token) => {
        return from(Storage.set({ key: TTOKEN_KEY, value: token }));
      }),
      tap((_) => {
        this.isAuthenticated.next(true);
      })
    );
  }

and

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 48983

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>