Skip to content

Commit

Permalink
Pls finally cookie set fixed.
Browse files Browse the repository at this point in the history
resolves #114
  • Loading branch information
MichiBaum committed Dec 24, 2024
1 parent 7a6ed13 commit 8668f61
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
4 changes: 2 additions & 2 deletions website/src/app/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {routes} from './app.routes';
import {HTTP_INTERCEPTORS, HttpClient, provideHttpClient, withInterceptorsFromDi} from "@angular/common/http";
import {InterpolatableTranslationObject, TranslateLoader, TranslateModule, TranslateService} from "@ngx-translate/core";
import {TranslateHttpLoader} from "@ngx-translate/http-loader";
import {AuthInterceptor} from "./core/interceptors/auth.interceptor";
import {AuthJwtInterceptor} from "./core/interceptors/auth-jwt.interceptor";
import {provideServiceWorker} from '@angular/service-worker';
import {IMAGE_LOADER, ImageLoaderConfig} from "@angular/common";
import {environment} from "../environments/environment";
Expand Down Expand Up @@ -73,7 +73,7 @@ export const appConfig: ApplicationConfig = {
}),
{
provide : HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
useClass: AuthJwtInterceptor,
multi : true,
},
TranslateModule.forRoot({
Expand Down
15 changes: 15 additions & 0 deletions website/src/app/core/interceptors/auth-cookie.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {Injectable} from "@angular/core";
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest} from "@angular/common/http";
import {Observable} from "rxjs";

@Injectable()
export class AuthCookieInterceptor implements HttpInterceptor{

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const modifiedRequest = req.clone({
withCredentials: true,
});
return next.handle(modifiedRequest);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {TestBed} from '@angular/core/testing';
import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing';
import {HttpTestingController, provideHttpClientTesting} from '@angular/common/http/testing';
import {HTTP_INTERCEPTORS, HttpClient} from '@angular/common/http';
import {AuthInterceptor} from './auth.interceptor';
import {AuthJwtInterceptor} from './auth-jwt.interceptor';
import {AuthService} from "../services/auth.service";

describe('AuthInterceptor', () => {
Expand All @@ -13,9 +13,9 @@ describe('AuthInterceptor', () => {
const authServiceSpy = jasmine.createSpyObj('AuthService', ['jwtIsPresent', 'getJwtTokenFromLocalStorage']);

TestBed.configureTestingModule({
imports: [HttpClientTestingModule],
providers: [
{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true},
provideHttpClientTesting(),
{provide: HTTP_INTERCEPTORS, useClass: AuthJwtInterceptor, multi: true},
{provide: AuthService, useValue: authServiceSpy}
]
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {AuthService} from "../services/auth.service";
* if a JWT token is present.
*/
@Injectable()
export class AuthInterceptor implements HttpInterceptor {
export class AuthJwtInterceptor implements HttpInterceptor {
private authService = inject(AuthService);

/**
Expand Down
28 changes: 18 additions & 10 deletions website/src/app/login/login.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {FormsModule, ReactiveFormsModule} from '@angular/forms';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import {provideHttpClientTesting} from '@angular/common/http/testing';
import {TranslateModule} from '@ngx-translate/core';
import {LoginComponent} from './login.component';
import {of} from 'rxjs';
Expand All @@ -22,10 +22,10 @@ describe('LoginComponent', () => {
imports: [
ReactiveFormsModule,
FormsModule,
HttpClientTestingModule,
TranslateModule.forRoot()
],
providers: [
provideHttpClientTesting(),
AuthService,
HeaderService,
RouterNavigationService
Expand All @@ -50,26 +50,34 @@ describe('LoginComponent', () => {
it('should not call authService.login if username or password is empty', () => {
const loginSpy = spyOn(authService, 'login');

component.username = '';
component.password = '';
component.loginForm.patchValue({
username: '',
password: ''
})
component.login();
expect(loginSpy).not.toHaveBeenCalled();

component.username = 'user';
component.password = '';
component.loginForm.patchValue({
username: 'user',
password: ''
})
component.login();
expect(loginSpy).not.toHaveBeenCalled();

component.username = '';
component.password = 'pass';
component.loginForm.patchValue({
username: '',
password: 'pass'
})
component.login();
expect(loginSpy).not.toHaveBeenCalled();
});

it('should call authService.login if username and password are not empty', () => {
const loginSpy = spyOn(authService, 'login');
component.username = 'user';
component.password = 'pass';
component.loginForm.patchValue({
username: 'user',
password: 'pass'
})
component.login();
expect(loginSpy).toHaveBeenCalledWith('user', 'pass');
});
Expand Down

0 comments on commit 8668f61

Please sign in to comment.