Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature#300 #398

Closed
wants to merge 12 commits into from
8 changes: 7 additions & 1 deletion src/app/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { User } from '../models/user.model'
import { type TokenService } from './token.service'
import { type Router } from '@angular/router'
import { type CookieService } from 'ngx-cookie-service'
import { type UserService } from './user.service'

describe('AuthService', () => {
let authService: AuthService
Expand All @@ -28,6 +29,7 @@ describe('AuthService', () => {
let httpClient: HttpClient
let httpClientMock: HttpTestingController
let tokenServiceMock: TokenService
let userServiceMock: UserService

beforeEach(() => {
TestBed.configureTestingModule({
Expand Down Expand Up @@ -62,7 +64,11 @@ describe('AuthService', () => {
value: cookieServiceMock
})

authService = new AuthService(httpClient, routerMock as unknown as Router, cookieServiceMock as unknown as CookieService, tokenServiceMock)
userServiceMock = {
userLoggedIn: false
} as unknown as UserService

authService = new AuthService(httpClient, routerMock as unknown as Router, cookieServiceMock as unknown as CookieService, tokenServiceMock, userServiceMock)
})

it('should return the current user when user is NOT FOUND in cookies', (done) => {
Expand Down
8 changes: 7 additions & 1 deletion src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Router } from '@angular/router'
import { CookieService } from 'ngx-cookie-service'
import { TokenService } from './token.service'
import { Inject, Injectable } from '@angular/core'
import { UserService } from './user.service'

interface loginResponse {
id: string
Expand Down Expand Up @@ -43,7 +44,8 @@ export class AuthService {
@Inject(HttpClient) private readonly http: HttpClient,
@Inject(Router) private readonly router: Router,
@Inject(CookieService) private readonly cookieService: CookieService,
@Inject(TokenService) private readonly tokenService: TokenService
@Inject(TokenService) private readonly tokenService: TokenService,
@Inject(UserService) private readonly userService: UserService
) {
// private helper: CookieEncryptionHelper) {

Expand Down Expand Up @@ -183,13 +185,17 @@ export class AuthService {
this.cookieService.set('authToken', resp.authToken)
this.cookieService.set('refreshToken', resp.refreshToken)
this.cookieService.set('user', JSON.stringify(this.currentUser))
this.userService.userLoggedIn = true
console.log(`userLoggedIn: ${this.userService.userLoggedIn}`)
return resp
}

public logout (): void {
this.cookieService.delete('authToken')
this.cookieService.delete('refreshToken')
this.cookieService.delete('user')
this.userService.userLoggedIn = false
console.log(`userLoggedIn: ${this.userService.userLoggedIn}`)
this.currentUser = new User(this.anonym) // Asignar un nuevo usuario anónimo
void this.router.navigate(['/login']) // Usar void para marcar la promesa como explícitamente ignorada
}
Expand Down
16 changes: 16 additions & 0 deletions src/app/services/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing'

import { UserService } from './user.service'

describe('UserService', () => {
let service: UserService

beforeEach(() => {
TestBed.configureTestingModule({})
service = TestBed.inject(UserService)
})

it('should be created', () => {
expect(service).toBeTruthy()
})
})
10 changes: 10 additions & 0 deletions src/app/services/user.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Injectable } from '@angular/core'

@Injectable({
providedIn: 'root'
})
export class UserService {
userRegistered: boolean = false
userLoggedIn: boolean = false
userSentASolution: boolean = false
}
Loading