Skip to content

Instantly share code, notes, and snippets.

@changhuixu
Last active July 26, 2020 00:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save changhuixu/9696a16ba0f00ec93ddc8da9e92b20c0 to your computer and use it in GitHub Desktop.
Save changhuixu/9696a16ba0f00ec93ddc8da9e92b20c0 to your computer and use it in GitHub Desktop.
@Injectable({
providedIn: 'root',
})
export class AuthService implements OnDestroy {
private readonly apiUrl = `${environment.apiUrl}api/account`;
private timer: Subscription;
private _user = new BehaviorSubject<ApplicationUser>(null);
user$: Observable<ApplicationUser> = this._user.asObservable();
private storageEventListener(event: StorageEvent) { // ... }
constructor(private router: Router, private http: HttpClient) {
window.addEventListener('storage', this.storageEventListener.bind(this));
}
ngOnDestroy(): void {
window.removeEventListener('storage', this.storageEventListener.bind(this));
}
login(username: string, password: string) { // ... }
logout() { // ... }
refreshToken() { // ... }
setLocalStorage(x: LoginResult) { // ... }
clearLocalStorage() { // ... }
private getTokenRemainingTime() { // ... }
private startTokenTimer() { // ... }
private stopTokenTimer() {
this.timer?.unsubscribe();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment