Skip to content

Commit

Permalink
rename auth services, remove registration button from landing page wh…
Browse files Browse the repository at this point in the history
…en user is logged in, reset login state and redirect user to login when requests return 401 or 403.
  • Loading branch information
halbekanne committed Jan 12, 2024
1 parent 73551d2 commit 00da6c4
Show file tree
Hide file tree
Showing 20 changed files with 81 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/app/chat/data-access/chat-state.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { chatStateAdapter } from './chat-state.adapter';
import { TimerService } from '@shared/util/time/timer.service';
import { startWith } from 'rxjs/operators';
import { ActivatedRoute } from '@angular/router';
import { AuthStorageService } from '@shared/data-access/auth/auth-storage.service';
import { AuthService } from '@shared/data-access/auth/auth.service';
import { OwnProfileService } from '@shared/data-access/profile/own-profile.service';

export type SingleChatState = {
Expand All @@ -45,7 +45,7 @@ export type ChatAdaptState = {
export class ChatStateService {
private readonly storePath = 'chat';
private activatedRoute = inject(ActivatedRoute);
private authStorageService = inject(AuthStorageService);
private authStorageService = inject(AuthService);
private profileService = inject(OwnProfileService);

private userLoggedOut$ = this.authStorageService.hasLoggedOut$.pipe(
Expand Down
8 changes: 4 additions & 4 deletions src/app/home/feature/contact/contact.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import { AuthStorageService } from '@shared/data-access/auth/auth-storage.service';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthService } from '@shared/data-access/auth/auth.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { EventLogService } from '@shared/data-access/log/event-log.service';
import { ContactService } from '../../data-access/contact.service';
import { tap } from 'rxjs';
Expand Down Expand Up @@ -49,8 +49,8 @@ export class ContactComponent implements OnInit {
constructor(
private fb: NonNullableFormBuilder,
private contactService: ContactService,
public authStorageService: AuthStorageService,
private authenticationService: AuthenticationService,
public authStorageService: AuthService,
private authenticationService: AuthHttpService,
private eventLogService: EventLogService
) {}

Expand Down
10 changes: 8 additions & 2 deletions src/app/home/feature/landing-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Component } from '@angular/core';
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { EventLogService } from '@shared/data-access/log/event-log.service';
import { AuthService } from '@shared/data-access/auth/auth.service';
import { NgIf } from '@angular/common';

@Component({
selector: 'app-landing-page',
Expand Down Expand Up @@ -36,6 +38,7 @@ import { EventLogService } from '@shared/data-access/log/event-log.service';
Wir verbinden Tänzer
</div>
<button
*ngIf="!isLoggedIn()"
class="mt-12 cursor-pointer rounded bg-red-900 py-3 px-8 text-xl font-bold text-white transition-all duration-200 ease-in-out hover:bg-white hover:text-red-900 lg:text-2xl"
(click)="navigateToRegistration()"
>
Expand Down Expand Up @@ -94,9 +97,12 @@ import { EventLogService } from '@shared/data-access/log/event-log.service';
</main>
`,
standalone: true,
imports: [NgIf],
})
export class LandingPageComponent {
constructor(private router: Router, private eventService: EventLogService) {}
private router = inject(Router);
private eventService = inject(EventLogService);
public isLoggedIn = inject(AuthService).isLoggedIn;

navigateToRegistration(): void {
this.eventService.createAndPublishEvent('navigated_to_page', {
Expand Down
8 changes: 4 additions & 4 deletions src/app/home/feature/logout-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { Subscription } from 'rxjs';
import { AuthStorageService } from '@shared/data-access/auth/auth-storage.service';
import { AuthService } from '@shared/data-access/auth/auth.service';

@Component({
selector: 'app-logout-page',
Expand All @@ -13,8 +13,8 @@ export class LogoutPageComponent implements OnInit, OnDestroy {
logoutSubscription: Subscription | undefined;

constructor(
private authService: AuthenticationService,
private authStorageService: AuthStorageService,
private authService: AuthHttpService,
private authStorageService: AuthService,
private router: Router
) {}

Expand Down
4 changes: 2 additions & 2 deletions src/app/home/ui/login-form/login-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UntypedFormGroup,
Validators,
} from '@angular/forms';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { LoginRequest } from '@shared/data-access/auth/authentication.types';
import { Router, RouterLink } from '@angular/router';
import { APIError } from '@shared/util/http/response.types';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class LoginFormComponent implements OnInit {

constructor(
private fb: UntypedFormBuilder,
private authService: AuthenticationService,
private authService: AuthHttpService,
private router: Router
) {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { ReactiveFormsModule } from '@angular/forms';
import { MatInputModule } from '@angular/material/input';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatButtonModule } from '@angular/material/button';
import { AuthStorageService } from '@shared/data-access/auth/auth-storage.service';
import { AuthService } from '@shared/data-access/auth/auth.service';
import { of } from 'rxjs';
import { Location } from '@angular/common';
import { InitPersonalDataComponent } from '../init-personal-data/init-personal-data.component';
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Setting up username', () => {
getApiUrl: () => 'http://test.de',
}),
},
MockProvider(AuthStorageService, {
MockProvider(AuthService, {
authData$: of({
isLoggedIn: true,
isHuman: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
UntypedFormGroup,
Validators,
} from '@angular/forms';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { Subscription } from 'rxjs';
import { mustMatch } from '@shared/util/mustMatch.validator';
Expand Down Expand Up @@ -45,7 +45,7 @@ export class RegisterUserFormComponent implements OnInit, OnDestroy {

constructor(
private fb: UntypedFormBuilder,
private authenticationService: AuthenticationService,
private authenticationService: AuthHttpService,
private route: ActivatedRoute,
private router: Router
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
Validators,
} from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { APIError } from '@shared/util/http/response.types';
import { mustMatch } from '@shared/util/mustMatch.validator';
import { switchMap } from 'rxjs';
Expand Down Expand Up @@ -48,7 +48,7 @@ export class NewPasswordComponent {

constructor(
private fb: NonNullableFormBuilder,
private authenticationService: AuthenticationService,
private authenticationService: AuthHttpService,
private route: ActivatedRoute,
private router: Router
) {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnDestroy, OnInit } from '@angular/core';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { ActivatedRoute, Router } from '@angular/router';
import { Subscription, switchMap } from 'rxjs';

Expand All @@ -15,7 +15,7 @@ export class VerifyAccountComponent implements OnInit, OnDestroy {
constructor(
private router: Router,
private route: ActivatedRoute,
private authService: AuthenticationService
private authService: AuthHttpService
) {}

ngOnInit(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { MatInputModule } from '@angular/material/input';
import { MatCardModule } from '@angular/material/card';
import { ReactiveFormsModule } from '@angular/forms';
import { MockProvider } from 'ng-mocks';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { ActivatedRoute } from '@angular/router';

describe('SendVerificationLinkFormComponent', () => {
Expand All @@ -20,8 +20,8 @@ describe('SendVerificationLinkFormComponent', () => {
ReactiveFormsModule,
],
providers: [
MockProvider(AuthenticationService),
MockProvider(AuthenticationService),
MockProvider(AuthHttpService),
MockProvider(AuthHttpService),
MockProvider(ActivatedRoute),
],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
ReactiveFormsModule,
Validators,
} from '@angular/forms';
import { AuthenticationService } from '@shared/data-access/auth/authentication.service';
import { AuthHttpService } from '@shared/data-access/auth/auth-http.service';
import { ActivatedRoute, Router } from '@angular/router';
import { APIError, OldAPIResponse } from '@shared/util/http/response.types';
import { LinkType } from '../../util/registration.types';
import { Observable } from 'rxjs';
import { AuthStorageService } from '@shared/data-access/auth/auth-storage.service';
import { AuthService } from '@shared/data-access/auth/auth.service';
import { MatButtonModule } from '@angular/material/button';
import { RecaptchaModule } from 'ng-recaptcha';
import { DataTestDirective } from '@shared/util/data-test.directive';
Expand Down Expand Up @@ -51,10 +51,10 @@ export class SendVerificationLinkFormComponent implements OnInit {

constructor(
private fb: NonNullableFormBuilder,
private authenticationService: AuthenticationService,
private authenticationService: AuthHttpService,
private router: Router,
private route: ActivatedRoute,
public authStorageService: AuthStorageService
public authStorageService: AuthService
) {}

ngOnInit(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { AuthenticationService } from './authentication.service';
import { AuthHttpService } from './auth-http.service';
import {
createHttpFactory,
HttpMethod,
SpectatorHttp,
} from '@ngneat/spectator/jest';
import { AuthStorageService } from './auth-storage.service';
import { AuthService } from './auth.service';
import { EnvironmentService } from '../environment.service';
import { LoginRequest, UserRegistration } from './authentication.types';
import { MockService } from 'ng-mocks';
import { TestRequest } from '@angular/common/http/testing';
import { APIError } from '../../util/http/response.types';

describe('AuthenticationService', () => {
let spectator: SpectatorHttp<AuthenticationService>;
describe('AuthHttpService', () => {
let spectator: SpectatorHttp<AuthHttpService>;
const createHttp = createHttpFactory({
service: AuthenticationService,
mocks: [AuthStorageService],
service: AuthHttpService,
mocks: [AuthService],
});

beforeEach(() => {
Expand Down Expand Up @@ -110,7 +110,7 @@ describe('AuthenticationService', () => {
);

it('sets the login state in the auth storage service', () => {
const authStorageService = spectator.inject(AuthStorageService);
const authStorageService = spectator.inject(AuthService);
spectator.service.login(loginRequest).subscribe();

spectator
Expand All @@ -126,7 +126,7 @@ describe('AuthenticationService', () => {
});

it("doesn't set the login state in the auth storage service when login fails", () => {
const authStorageService = spectator.inject(AuthStorageService);
const authStorageService = spectator.inject(AuthService);
spectator.service.login(loginRequest).subscribe();

spectator
Expand Down Expand Up @@ -189,7 +189,7 @@ describe('AuthenticationService', () => {
});

it('sets the human state in the auth storage service', () => {
const authStorageService = spectator.inject(AuthStorageService);
const authStorageService = spectator.inject(AuthService);
spectator.service.loginAsHuman(captchaToken).subscribe();

spectator
Expand All @@ -205,7 +205,7 @@ describe('AuthenticationService', () => {
});

it("doesn't set the human state in the auth storage service when human login fails", () => {
const authStorageService = spectator.inject(AuthStorageService);
const authStorageService = spectator.inject(AuthService);
spectator.service.loginAsHuman(captchaToken).subscribe();

spectator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
UserRegistration,
} from './authentication.types';
import { catchError, map, Observable, of, shareReplay, tap } from 'rxjs';
import { AuthStorageService } from './auth-storage.service';
import { AuthService } from './auth.service';
import { EnvironmentService } from '../environment.service';
import {
asError,
Expand All @@ -23,7 +23,7 @@ import {
@Injectable({
providedIn: 'root',
})
export class AuthenticationService {
export class AuthHttpService {
private defaultOptions = {
withCredentials: true,
};
Expand All @@ -32,7 +32,7 @@ export class AuthenticationService {

constructor(
private http: HttpClient,
private authStorageService: AuthStorageService,
private authStorageService: AuthService,
private environment: EnvironmentService
) {
this.baseUrl = `${this.environment.getApiUrl()}/authentication`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TestBed } from '@angular/core/testing';

import { AuthData, AuthStorageService } from './auth-storage.service';
import { AuthData, AuthService } from './auth.service';
import { HttpClientTestingModule } from '@angular/common/http/testing';

describe('AuthStorageService', () => {
let service: AuthStorageService | undefined;
describe('AuthService', () => {
let service: AuthService | undefined;
let setItemSpy: jest.SpyInstance;
let getItemSpy: jest.SpyInstance;

Expand All @@ -31,7 +31,7 @@ describe('AuthStorageService', () => {
});

it('is created', () => {
service = TestBed.inject(AuthStorageService);
service = TestBed.inject(AuthService);
expect(service).toBeTruthy();
});

Expand All @@ -48,7 +48,7 @@ describe('AuthStorageService', () => {
return null;
});

service = TestBed.inject(AuthStorageService);
service = TestBed.inject(AuthService);

const actualAuthData = service.getSnapshot();
expect(actualAuthData).toEqual(expectedAuthData);
Expand All @@ -59,7 +59,7 @@ describe('AuthStorageService', () => {
return null;
});

service = TestBed.inject(AuthStorageService);
service = TestBed.inject(AuthService);

const expectedAuthData: AuthData = {
isLoggedIn: false,
Expand All @@ -75,7 +75,7 @@ describe('AuthStorageService', () => {

beforeEach(() => {
listener.mockClear();
service = TestBed.inject(AuthStorageService);
service = TestBed.inject(AuthService);
service.authData$.subscribe(listener);

service.setLoginState(true);
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('AuthStorageService', () => {

beforeEach(() => {
listener.mockClear();
service = TestBed.inject(AuthStorageService);
service = TestBed.inject(AuthService);
service.authData$.subscribe(listener);
service.setLoginState(false);
service.setHumanState(true);
Expand Down
Loading

0 comments on commit 00da6c4

Please sign in to comment.