2021-06-21 09:22:13 +02:00
|
|
|
import { AsyncSubject } from 'rxjs'
|
|
|
|
|
import { HttpClient } from '@angular/common/http'
|
|
|
|
|
import { Injectable } from '@angular/core'
|
2021-09-16 23:36:51 +02:00
|
|
|
import { User } from '../../api'
|
2021-06-21 09:22:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
|
|
|
export class LoginService {
|
2021-10-22 22:35:15 +02:00
|
|
|
user: User | null
|
2021-06-21 09:22:13 +02:00
|
|
|
ready$ = new AsyncSubject<void>()
|
|
|
|
|
|
|
|
|
|
constructor (private http: HttpClient) {
|
|
|
|
|
this.init()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-25 21:55:40 +02:00
|
|
|
async updateUser () {
|
2021-10-22 22:35:15 +02:00
|
|
|
if (!this.user) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2021-06-25 21:55:40 +02:00
|
|
|
await this.http.put('/api/1/user', this.user).toPromise()
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 09:22:13 +02:00
|
|
|
private async init () {
|
2021-06-25 21:55:40 +02:00
|
|
|
try {
|
|
|
|
|
this.user = await this.http.get('/api/1/user').toPromise()
|
|
|
|
|
} catch {
|
|
|
|
|
this.user = null
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-21 09:22:13 +02:00
|
|
|
this.ready$.next()
|
|
|
|
|
this.ready$.complete()
|
|
|
|
|
}
|
|
|
|
|
}
|