@saravananthemesoft wrote:
Hi i would like access ionic storage module inside be BehaviorSubject .
My Code below currently am using local storage i want to replace ionic storage
import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { BehaviorSubject, Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { UserService } from "./user.service"; import { Token } from '../_models'; import { environment } from "../../environments/environment"; import { Storage } from "@ionic/storage"; @Injectable({ providedIn: "root" }) export class AuthenticationService { private currentTokenSubject: BehaviorSubject<Token>; public currentToken: Observable<Token>; constructor( private http: HttpClient, public storage: Storage, public user_ser: UserService ) { this.currentTokenSubject = new BehaviorSubject<Token>( JSON.parse( localStorage.getItem("currentUser") //Need to replace with ionic storage ) ); this.currentToken = this.currentTokenSubject.asObservable(); } public get currentTokenValue(): Token { return this.currentTokenSubject.value; } login(username: string, password: string) { return this.http .post<any>(`${environment.url}/oauth/token`, { username, password, grant_type: "password", client_id: environment.passport.client_id, client_secret: environment.passport.client_secret }) .pipe( map(token => { if (token && token.access_token) { this.storage.ready().then(() => { this.storage.set("currentToken", token); }); this.currentTokenSubject.next(token); } return token; }) ); } logout() { this.storage.remove("currentToken"); this.currentTokenSubject.next(null); } }
Posts: 1
Participants: 1