Skip to content

Commit 6aa6d9f

Browse files
committed
refactor(frontend:auth): user and server initialization logic through initUser method
1 parent 95a455b commit 6aa6d9f

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

frontend/src/app/auth/auth.service.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import { LoginResponseDto, TwoFaResponseDto } from '@sync-in-server/backend/src/
2323
import type { TokenResponseDto } from '@sync-in-server/backend/src/authentication/dto/token-response.dto'
2424
import { TwoFaVerifyDto } from '@sync-in-server/backend/src/authentication/dto/two-fa-verify.dto'
2525
import { currentTimeStamp } from '@sync-in-server/backend/src/common/shared'
26-
import { ServerConfig } from '@sync-in-server/backend/src/configuration/config.interfaces'
2726
import { catchError, finalize, map, Observable, of, throwError } from 'rxjs'
2827
import { switchMap, tap } from 'rxjs/operators'
2928
import { USER_PATH } from '../applications/users/user.constants'
@@ -98,8 +97,7 @@ export class AuthService {
9897
map((r: ClientAuthCookieDto) => {
9998
this.accessExpiration = r.token.access_expiration
10099
this.refreshExpiration = r.token.refresh_expiration
101-
this.userService.initUser(r.user)
102-
this.setServerConfig(r.server)
100+
this.initUser(r)
103101
if (r?.client_token_update) {
104102
// update client token
105103
this.electron.send(EVENT.SERVER.AUTHENTICATION_TOKEN_UPDATE, r.client_token_update)
@@ -162,8 +160,7 @@ export class AuthService {
162160
if (r !== null) {
163161
this.accessExpiration = r.token.access_expiration
164162
this.refreshExpiration = r.token.refresh_expiration
165-
this.userService.initUser(r.user, impersonate)
166-
this.setServerConfig(r.server)
163+
this.initUser(r, impersonate)
167164
}
168165
}
169166

@@ -200,10 +197,7 @@ export class AuthService {
200197
return of(false)
201198
} else if (!this.store.user.getValue()) {
202199
return this.http.get<Omit<LoginResponseDto, 'token'>>(API_USERS_ME).pipe(
203-
tap((r: Omit<LoginResponseDto, 'token'>) => {
204-
this.userService.initUser(r.user)
205-
this.setServerConfig(r.server)
206-
}),
200+
tap((r: Omit<LoginResponseDto, 'token'>) => this.initUser(r)),
207201
map(() => true),
208202
catchError((e: HttpErrorResponse) => {
209203
if (e.status === 401) {
@@ -230,9 +224,11 @@ export class AuthService {
230224
return this.http.post<TwoFaResponseDto>(API_TWO_FA_LOGIN_VERIFY, verify)
231225
}
232226

233-
private setServerConfig(serverConfig: ServerConfig) {
234-
if (!serverConfig) return
235-
this.store.server.set(serverConfig)
227+
initUser(r: Partial<LoginResponseDto>, impersonate = false) {
228+
this.userService.initUser(r.user, impersonate)
229+
if (r.server) {
230+
this.store.server.set(r.server)
231+
}
236232
}
237233

238234
private refreshTokenHasExpired(): boolean {

0 commit comments

Comments
 (0)