diff --git a/src/app/admin-pages/home-panel/home-panel.component.ts b/src/app/admin-pages/home-panel/home-panel.component.ts index 97185e7..69699a9 100644 --- a/src/app/admin-pages/home-panel/home-panel.component.ts +++ b/src/app/admin-pages/home-panel/home-panel.component.ts @@ -1,5 +1,6 @@ import { Component } from "@angular/core"; -import { LocalCookiesService } from "src/app/services/authentication/local-cookies.service"; +import { AuthUser } from "src/app/models/AuthUser"; +import { AuthUserService } from "src/app/services/authentication/auth-user.service"; @Component({ selector: "home-panel", @@ -7,11 +8,13 @@ import { LocalCookiesService } from "src/app/services/authentication/local-cooki styleUrls: ["./home-panel.component.css"] }) export class HomePanelComponent { - user: any; + user: AuthUser; - constructor(private cookieService: LocalCookiesService) { - this.cookieService.userEvent.subscribe((value) => { - this.user = value; + constructor(private AuthUser: AuthUserService) { + this.AuthUser.userEvent.subscribe((value) => { + if (value !== null) { + this.user = value; + } }); } } diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index bc34eb1..4e0b402 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -8,7 +8,7 @@ import { MissingPageComponent } from "./pages/missing-page/missing-page.componen import { SeriesPageComponent } from "./pages/series-page/series-page.component"; import { SearchPageComponent } from "./search-page/search-page.component"; import { LoginPageComponent } from "./pages/login/login.component"; -import { AuthenticationGuard } from "./services/authentication/authentication.guard"; +import { AuthenticationGuard } from "./guards/authentication.guard"; import { UserSettingsPageComponent } from "./user-settings-page/user-settings-page.component"; import { RegistrationPageComponent } from "./pages/registration/registration.component"; diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 11e9630..486b03e 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,6 +1,6 @@ import { Component, OnInit } from "@angular/core"; -import { LocalCookiesService } from "./services/authentication/local-cookies.service"; import { AuthenticationService } from "./services/authentication/authentication.service"; +import { AuthUserService } from "./services/authentication/auth-user.service"; @Component({ selector: "app-root", @@ -14,16 +14,16 @@ export class AppComponent implements OnInit { constructor( private authService: AuthenticationService, - private cookieService: LocalCookiesService + private AuthUser: AuthUserService ) { - this.cookieService.userEvent.subscribe((value) => { - this.isAuthenticated = Object.keys(value).length > 0; + this.AuthUser.userEvent.subscribe(() => { + this.isAuthenticated = this.AuthUser.isLoggedIn(); }); } ngOnInit(): void { if (this.isAuthenticated) { - this.authService.isLoggedIn().subscribe((value) => { + this.authService.validateCookies().subscribe((value) => { this.isAuthenticated = value ? true : false; }); } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index de1ef09..ceb8be5 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -15,7 +15,6 @@ import { MatDialogModule } from "@angular/material/dialog"; import { SearchPageModule } from "./search-page/search-page.module"; import { LoginPageComponent } from "./pages/login/login.component"; import { UserNavMenuComponent } from "./user-nav-menu/user-nav-menu.component"; -import { LocalCookiesService } from "./services/authentication/local-cookies.service"; import { UserSettingsPageComponent } from "./user-settings-page/user-settings-page.component"; import { RegistrationPageComponent } from "./pages/registration/registration.component"; import { SharedModule } from "./shared/shared.module"; @@ -23,6 +22,7 @@ import { DeleteConfirmationComponent } from "./user-settings-page/delete-confirm import { BookPageComponent } from "./pages/book-page/book-page.component"; import { SeriesPageComponent } from "./pages/series-page/series-page.component"; import { MarkdownModule } from "ngx-markdown"; +import { AuthUserService } from "./services/authentication/auth-user.service"; @NgModule({ declarations: [ @@ -51,7 +51,7 @@ import { MarkdownModule } from "ngx-markdown"; BookPageComponent, SeriesPageComponent ], - providers: [Title, LocalCookiesService], + providers: [Title, AuthUserService], bootstrap: [AppComponent] }) export class AppModule {} diff --git a/src/app/constants/UserPermissions.ts b/src/app/constants/UserPermissions.ts new file mode 100644 index 0000000..a609269 --- /dev/null +++ b/src/app/constants/UserPermissions.ts @@ -0,0 +1,9 @@ +export enum UserPermissions { + APPROVE_CORRECTIONS = "approve_corrections", + VIEW_CORRECTIONS = "view_corrections", + UPDATE_USERS = "update_users", + ADD_ENTRY = "add_entry", + DELETE_ENTRY = "delete_entry", + MODIFY_ENTRY = "modify_entry", + STAFF = "staff" +} diff --git a/src/app/services/authentication/authentication.guard.spec.ts b/src/app/guards/authentication.guard.spec.ts similarity index 100% rename from src/app/services/authentication/authentication.guard.spec.ts rename to src/app/guards/authentication.guard.spec.ts diff --git a/src/app/services/authentication/authentication.guard.ts b/src/app/guards/authentication.guard.ts similarity index 72% rename from src/app/services/authentication/authentication.guard.ts rename to src/app/guards/authentication.guard.ts index 1bbc192..3963e05 100644 --- a/src/app/services/authentication/authentication.guard.ts +++ b/src/app/guards/authentication.guard.ts @@ -1,13 +1,13 @@ import { inject } from "@angular/core"; import { Router } from "@angular/router"; import { tap } from "rxjs"; -import { AuthenticationService } from "./authentication.service"; +import { AuthenticationService } from "../services/authentication/authentication.service"; export const AuthenticationGuard = () => { const router = inject(Router); const authService = inject(AuthenticationService); - return authService.isLoggedIn().pipe( + return authService.validateCookies().pipe( tap((value) => { return value ? true : router.navigate(["/login"], { queryParams: { state: "expired" } }); }) diff --git a/src/app/models/AuthUser.ts b/src/app/models/AuthUser.ts new file mode 100644 index 0000000..4390555 --- /dev/null +++ b/src/app/models/AuthUser.ts @@ -0,0 +1,7 @@ +export interface AuthUser { + user_id: number; + username: string; + create_date: string; + last_login: string; + role_name: string; +} diff --git a/src/app/pages/book-page/book-page.component.ts b/src/app/pages/book-page/book-page.component.ts index 179000f..3eac0b7 100644 --- a/src/app/pages/book-page/book-page.component.ts +++ b/src/app/pages/book-page/book-page.component.ts @@ -7,7 +7,6 @@ import { MetadataService } from "../../services/metadata.service"; import { MynewormAPIService } from "../../services/myneworm-api.service"; import { MatDialog, MatDialogConfig } from "@angular/material/dialog"; import { ListEntryModalComponent } from "../../shared/list-entry-modal/list-entry-modal.component"; -import { LocalCookiesService } from "../../services/authentication/local-cookies.service"; import { ListEntry } from "../../models/ListEntry"; import { ToastService } from "../../services/toast.service"; import { CommonModule } from "@angular/common"; @@ -16,6 +15,7 @@ import { BookFormatPipe } from "src/app/pipes/BookFormat.pipe"; import { SharedModule } from "src/app/shared/shared.module"; import * as moment from "moment"; import { MarkdownModule } from "ngx-markdown"; +import { AuthUserService } from "src/app/services/authentication/auth-user.service"; @Component({ selector: "book-page", @@ -28,7 +28,7 @@ export class BookPageComponent implements OnInit { book: BookData; publisher: PublisherData; isLoggedIn = false; - private userID: string; + private userID: number; hasExistingEntry = false; remainingMsg: string; @@ -37,14 +37,14 @@ export class BookPageComponent implements OnInit { private service: MynewormAPIService, private metaService: MetadataService, private matDialog: MatDialog, - private cookieService: LocalCookiesService, + private AuthUser: AuthUserService, private toastService: ToastService, private router: Router ) { - this.cookieService.userEvent.subscribe((value) => { - this.isLoggedIn = Object.keys(value).length > 0; + this.AuthUser.userEvent.subscribe((value) => { + this.isLoggedIn = this.AuthUser.isLoggedIn(); - if (this.isLoggedIn) { + if (this.isLoggedIn && value !== null) { this.userID = value.user_id; } }); @@ -75,7 +75,7 @@ export class BookPageComponent implements OnInit { if (this.isLoggedIn) { this.service - .getListEntry(data.isbn, this.userID) + .getListEntry(data.isbn, this.userID.toString()) .pipe( catchError((err) => { if (err.status === 404) { diff --git a/src/app/services/authentication/local-cookies.service.spec.ts b/src/app/services/authentication/auth-user.service.spec.ts similarity index 50% rename from src/app/services/authentication/local-cookies.service.spec.ts rename to src/app/services/authentication/auth-user.service.spec.ts index 74e1473..7b9e375 100644 --- a/src/app/services/authentication/local-cookies.service.spec.ts +++ b/src/app/services/authentication/auth-user.service.spec.ts @@ -1,13 +1,13 @@ import { TestBed } from "@angular/core/testing"; -import { LocalCookiesService } from "./local-cookies.service"; +import { AuthUserService } from "./auth-user.service"; -describe("LocalCookiesService", () => { - let service: LocalCookiesService; +describe("AuthUserService", () => { + let service: AuthUserService; beforeEach(() => { TestBed.configureTestingModule({}); - service = TestBed.inject(LocalCookiesService); + service = TestBed.inject(AuthUserService); }); it("should be created", () => { diff --git a/src/app/services/authentication/auth-user.service.ts b/src/app/services/authentication/auth-user.service.ts new file mode 100644 index 0000000..404473c --- /dev/null +++ b/src/app/services/authentication/auth-user.service.ts @@ -0,0 +1,66 @@ +import { Injectable } from "@angular/core"; +import { BehaviorSubject } from "rxjs"; +import { AuthUser } from "../../models/AuthUser"; +import { UserPermissions } from "../../constants/UserPermissions"; + +@Injectable({ + providedIn: "root" +}) +export class AuthUserService { + userEvent = new BehaviorSubject(null); + user: AuthUser | null = null; + private userPermissions: string[]; + + constructor() { + this.userEvent.subscribe((value) => { + this.user = value; + }); + + const cachedAuth = localStorage.getItem("user"); + + if (cachedAuth) { + this.user = JSON.parse(cachedAuth); + } + this.userEvent.next(this.user); + } + + isLoggedIn() { + return this.user !== null; + } + + isModerator() { + return this.user !== null && this.userPermissions.includes(UserPermissions.STAFF); + } + + isSameUser(username: string) { + return this.user !== null && this.user.username === username; + } + + setPermissions(permissions: UserPermissions[]) { + this.userPermissions = permissions; + } + + hasPermission(...permissions: UserPermissions[]) { + for (let i = 0; i < permissions.length; i++) { + if (!this.userPermissions.includes(permissions[i])) { + return false; + } + } + return true; + } + + updateUser(userObj: any) { + this.userPermissions = userObj["permissions"]; + delete userObj["permissions"]; + this.user = userObj; + localStorage.setItem("user", JSON.stringify(userObj)); + this.userEvent.next(this.user); + } + + deleteUser() { + localStorage.removeItem("user"); + this.user = null; + this.userPermissions = []; + this.userEvent.next(this.user); + } +} diff --git a/src/app/services/authentication/authentication.service.ts b/src/app/services/authentication/authentication.service.ts index fc58f40..ba1c504 100644 --- a/src/app/services/authentication/authentication.service.ts +++ b/src/app/services/authentication/authentication.service.ts @@ -3,8 +3,8 @@ import { Injectable } from "@angular/core"; import { catchError, map, of } from "rxjs"; import { environment } from "src/environments/environment"; import { Buffer } from "buffer"; -import { LocalCookiesService } from "./local-cookies.service"; import { ToastService } from "../toast.service"; +import { AuthUserService } from "./auth-user.service"; @Injectable({ providedIn: "root" @@ -12,8 +12,8 @@ import { ToastService } from "../toast.service"; export class AuthenticationService { constructor( private http: HttpClient, - private cookieService: LocalCookiesService, - private toastService: ToastService + private toastService: ToastService, + private AuthUserService: AuthUserService ) {} login(username: string, password: string) { @@ -36,7 +36,7 @@ export class AuthenticationService { if (data.body !== null) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore - this.cookieService.updateUser(data.body.user); + this.AuthUserService.updateUser(data.body.user); return true; } @@ -54,7 +54,7 @@ export class AuthenticationService { validateCookies() { return this.http .post( - `${environment.API_ADDRESS}/auth/validateCookies`, + `${environment.API_ADDRESS}/auth/validate`, {}, { withCredentials: true, @@ -63,40 +63,16 @@ export class AuthenticationService { ) .pipe( map((data) => { - this.cookieService.updateUser(data.body); + this.AuthUserService.updateUser(data.body); return true; }), catchError(() => { - this.cookieService.deleteUser(); + this.AuthUserService.deleteUser(); return of(false); }) ); } - isLoggedIn() { - return this.http - .post( - `${environment.API_ADDRESS}/auth/isAuthenticated`, - {}, - { - withCredentials: true, - observe: "response" - } - ) - .pipe( - map((data) => { - return data.status === 200; - }), - catchError((error) => { - if (error.status === 401) { - this.cookieService.deleteUser(); - return of(false); - } - return of(`ERROR (${error.status}): ${error.statusText}`); - }) - ); - } - reauthenticate() { return this.http .post( @@ -136,7 +112,7 @@ export class AuthenticationService { } clearSessions() { - this.cookieService.deleteUser(); + this.AuthUserService.deleteUser(); return this.http.post(`${environment.API_ADDRESS}/auth/clear`, {}).pipe( catchError((err: any) => { this.toastService.sendError(err.error.errors); @@ -157,12 +133,12 @@ export class AuthenticationService { ) .pipe( map((data) => { - this.cookieService.deleteUser(); + this.AuthUserService.deleteUser(); return data.status === 200; }), catchError((error) => { if (error.status === 401) { - this.cookieService.deleteUser(); + this.AuthUserService.deleteUser(); return of(false); } return of(`ERROR (${error.status}): ${error.statusText}`); diff --git a/src/app/services/authentication/is-moderator.guard.ts b/src/app/services/authentication/is-moderator.guard.ts index 6a45c95..5013c12 100644 --- a/src/app/services/authentication/is-moderator.guard.ts +++ b/src/app/services/authentication/is-moderator.guard.ts @@ -1,27 +1,20 @@ import { inject } from "@angular/core"; import { Router } from "@angular/router"; -import { tap } from "rxjs"; -import { AuthenticationService } from "./authentication.service"; -import { LocalCookiesService } from "./local-cookies.service"; +import { AuthUserService } from "./auth-user.service"; export const IsModeratorGuard = () => { const router = inject(Router); - const authService = inject(AuthenticationService); - const cookieService = inject(LocalCookiesService); + const AuthUser = inject(AuthUserService); - if (!cookieService.user) { + if (!AuthUser.user) { router.navigate(["/"]); return; } - return authService.validateCookies().pipe( - tap((value) => { - if (!value || cookieService.user.role_id.includes("user")) { - router.navigate(["/login"]); - return; - } + if (!AuthUser.isModerator()) { + router.navigate(["/login"]); + return; + } - return true; - }) - ); + return true; }; diff --git a/src/app/services/authentication/local-cookies.service.ts b/src/app/services/authentication/local-cookies.service.ts deleted file mode 100644 index 4a8ac61..0000000 --- a/src/app/services/authentication/local-cookies.service.ts +++ /dev/null @@ -1,31 +0,0 @@ -import { Injectable } from "@angular/core"; -import { BehaviorSubject } from "rxjs"; - -@Injectable({ - providedIn: "root" -}) -export class LocalCookiesService { - userEvent: BehaviorSubject = new BehaviorSubject({}); - user: any; - - constructor() { - this.userEvent.subscribe((value) => { - this.user = value; - }); - - this.user = JSON.parse(localStorage.getItem("user") || "{}"); - this.userEvent.next(this.user); - } - - updateUser(userObj: any) { - localStorage.setItem("user", JSON.stringify(userObj)); - this.user = userObj; - this.userEvent.next(this.user); - } - - deleteUser() { - localStorage.removeItem("user"); - this.user = {}; - this.userEvent.next(this.user); - } -} diff --git a/src/app/shared/list-entry-modal/list-entry-modal.component.ts b/src/app/shared/list-entry-modal/list-entry-modal.component.ts index fd530cb..1470272 100644 --- a/src/app/shared/list-entry-modal/list-entry-modal.component.ts +++ b/src/app/shared/list-entry-modal/list-entry-modal.component.ts @@ -5,7 +5,8 @@ import { MynewormAPIService } from "src/app/services/myneworm-api.service"; import { ToastService } from "src/app/services/toast.service"; import { UtilitiesService } from "src/app/services/utilities.service"; import { ActivityStatus, OwnershipStatus } from "src/app/models/ListEntryStatus"; -import { LocalCookiesService } from "src/app/services/authentication/local-cookies.service"; +import { AuthUserService } from "src/app/services/authentication/auth-user.service"; +import { AuthUser } from "src/app/models/AuthUser"; @Component({ selector: "list-entry-modal", @@ -18,37 +19,42 @@ export class ListEntryModalComponent { showDatePicker = [false, false]; activityStatus = ActivityStatus; ownershipStatus = OwnershipStatus; - private user: any; + private user: AuthUser; constructor( @Inject(MAT_DIALOG_DATA) public bookData: { isbn: string; cover: string; title: string }, private service: MynewormAPIService, private toastService: ToastService, private utilities: UtilitiesService, - private cookieService: LocalCookiesService, + private AuthUser: AuthUserService, private dialogRef: MatDialogRef ) { - this.cookieService.userEvent.subscribe((value) => { + this.AuthUser.userEvent.subscribe((value) => { + if (value === null) { + return; + } this.user = value; - this.service.getListEntry(bookData.isbn, this.user.user_id).subscribe((data: ListEntry | null) => { - if (data === null) { - return; - } - - this.isUpdate = true; - this.listEntryForm.active_status = data.active_status; - this.listEntryForm.owner_status = data.owner_status; - this.listEntryForm.score = data.score; - this.listEntryForm.reread = data.reread; - this.listEntryForm.notes = data.notes; - if (data.start_date) { - this.listEntryForm.start_date = this.utilities.APIDateFormatter(new Date(data.start_date)); - } - if (data.end_date) { - this.listEntryForm.end_date = this.utilities.APIDateFormatter(new Date(data.end_date)); - } - }); + this.service + .getListEntry(bookData.isbn, this.user.user_id.toString()) + .subscribe((data: ListEntry | null) => { + if (data === null) { + return; + } + + this.isUpdate = true; + this.listEntryForm.active_status = data.active_status; + this.listEntryForm.owner_status = data.owner_status; + this.listEntryForm.score = data.score; + this.listEntryForm.reread = data.reread; + this.listEntryForm.notes = data.notes; + if (data.start_date) { + this.listEntryForm.start_date = this.utilities.APIDateFormatter(new Date(data.start_date)); + } + if (data.end_date) { + this.listEntryForm.end_date = this.utilities.APIDateFormatter(new Date(data.end_date)); + } + }); }); } diff --git a/src/app/user-nav-menu/user-nav-menu.component.html b/src/app/user-nav-menu/user-nav-menu.component.html index d15da6a..9e23a65 100644 --- a/src/app/user-nav-menu/user-nav-menu.component.html +++ b/src/app/user-nav-menu/user-nav-menu.component.html @@ -1,4 +1,4 @@ -
+
{{user.username}}
Profile diff --git a/src/app/user-nav-menu/user-nav-menu.component.ts b/src/app/user-nav-menu/user-nav-menu.component.ts index 549317c..6f27422 100644 --- a/src/app/user-nav-menu/user-nav-menu.component.ts +++ b/src/app/user-nav-menu/user-nav-menu.component.ts @@ -1,7 +1,8 @@ import { Component } from "@angular/core"; import { Router } from "@angular/router"; import { AuthenticationService } from "../services/authentication/authentication.service"; -import { LocalCookiesService } from "../services/authentication/local-cookies.service"; +import { AuthUser } from "../models/AuthUser"; +import { AuthUserService } from "../services/authentication/auth-user.service"; @Component({ selector: "user-nav-menu", @@ -9,17 +10,17 @@ import { LocalCookiesService } from "../services/authentication/local-cookies.se styleUrls: ["./user-nav-menu.component.css", "../shared/navigation-bar.css"] }) export class UserNavMenuComponent { - user: any; + user: AuthUser | null; isModerator = false; constructor( private router: Router, private authService: AuthenticationService, - private cookieService: LocalCookiesService + private AuthUser: AuthUserService ) { - this.cookieService.userEvent.subscribe((value) => { + this.AuthUser.userEvent.subscribe((value) => { this.user = value; - this.isModerator = !this.user.role_id.includes("user"); + this.isModerator = this.AuthUser.isModerator(); }); } diff --git a/src/app/user-pages/profile/profile.component.html b/src/app/user-pages/profile/profile.component.html index 7e4d10f..2a45bab 100644 --- a/src/app/user-pages/profile/profile.component.html +++ b/src/app/user-pages/profile/profile.component.html @@ -1,5 +1,5 @@
-
-
+

{{profileInfo.display_name}} (@{{profileInfo.username}})

{{profileInfo.username}}

diff --git a/src/app/user-pages/user-list/user-list.component.ts b/src/app/user-pages/user-list/user-list.component.ts index f7993ce..dd89c0f 100644 --- a/src/app/user-pages/user-list/user-list.component.ts +++ b/src/app/user-pages/user-list/user-list.component.ts @@ -6,7 +6,7 @@ import { ActivatedRoute } from "@angular/router"; import { ListEntry } from "../../models/ListEntry"; import { UserData } from "../../models/userData"; -import { LocalCookiesService } from "../../services/authentication/local-cookies.service"; +import { AuthUserService } from "src/app/services/authentication/auth-user.service"; @Component({ selector: "user-list", @@ -39,14 +39,14 @@ export class UserListComponent { private route: ActivatedRoute, private service: MynewormAPIService, private metaService: MetadataService, - private cookieService: LocalCookiesService + private AuthUser: AuthUserService ) { this.route.params.subscribe((data) => { this.metaService.updateMetaTags(`${data.username}'s List`, `/user/${data.username}/lists`); this.listUser = data.username; - this.cookieService.userEvent.subscribe((value) => { - this.isAuthUser = value.username === this.listUser; + this.AuthUser.userEvent.subscribe((value) => { + this.isAuthUser = this.AuthUser.isSameUser(this.listUser); }); }); } diff --git a/src/app/user-pages/wishlist/wishlist.component.ts b/src/app/user-pages/wishlist/wishlist.component.ts index f2e943d..84ea9d4 100644 --- a/src/app/user-pages/wishlist/wishlist.component.ts +++ b/src/app/user-pages/wishlist/wishlist.component.ts @@ -2,7 +2,7 @@ import { Component } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; import { WishlistEntry } from "src/app/models/WishlistEntry"; import { UserData } from "src/app/models/userData"; -import { LocalCookiesService } from "src/app/services/authentication/local-cookies.service"; +import { AuthUserService } from "src/app/services/authentication/auth-user.service"; import { MetadataService } from "src/app/services/metadata.service"; import { MynewormAPIService } from "src/app/services/myneworm-api.service"; import { ToastService } from "src/app/services/toast.service"; @@ -27,8 +27,8 @@ export class WishlistComponent { private service: MynewormAPIService, public utilities: UtilitiesService, private metaService: MetadataService, - private cookieService: LocalCookiesService, - private toastService: ToastService + private toastService: ToastService, + private AuthUser: AuthUserService ) {} ngOnInit() { @@ -39,7 +39,7 @@ export class WishlistComponent { } this.user = data; - this.isAuthUser = this.cookieService.user.username === this.user.username; + this.isAuthUser = this.AuthUser.isSameUser(this.user.username); if (!this.user.wishlist_msg) { this.user.wishlist_msg = null; diff --git a/src/app/user-settings-page/user-settings-page.component.ts b/src/app/user-settings-page/user-settings-page.component.ts index 74f5b0f..1bbf814 100644 --- a/src/app/user-settings-page/user-settings-page.component.ts +++ b/src/app/user-settings-page/user-settings-page.component.ts @@ -1,7 +1,6 @@ import { Component, OnInit, ViewChild } from "@angular/core"; import { ActivatedRoute, Router } from "@angular/router"; import { MynewormAPIService } from "../services/myneworm-api.service"; -import { LocalCookiesService } from "../services/authentication/local-cookies.service"; import { AccountData, UserData } from "../models/userData"; import { AccountUpdateData } from "../models/accountUpdateData"; import { ToastService } from "../services/toast.service"; @@ -9,6 +8,8 @@ import { MatDialog, MatDialogConfig } from "@angular/material/dialog"; import { DeleteConfirmationComponent } from "./delete-confirmation/delete-confirmation.component"; import { AuthenticationService } from "../services/authentication/authentication.service"; import { email, password, username } from "../models/validationPatterns"; +import { AuthUserService } from "../services/authentication/auth-user.service"; +import { AuthUser } from "../models/AuthUser"; @Component({ selector: "user-settings-page", @@ -18,7 +19,7 @@ import { email, password, username } from "../models/validationPatterns"; export class UserSettingsPageComponent implements OnInit { currPage: string; url: string | ArrayBuffer | null | undefined; - private user: any; + private user: AuthUser; private oldEmail: string; profileData: UserData; accountData: AccountData; @@ -33,19 +34,22 @@ export class UserSettingsPageComponent implements OnInit { constructor( private route: ActivatedRoute, private service: MynewormAPIService, - private cookieService: LocalCookiesService, private authService: AuthenticationService, private toastService: ToastService, private matDialog: MatDialog, - private router: Router + private router: Router, + private AuthUser: AuthUserService ) { - this.cookieService.userEvent.subscribe((value) => { + this.AuthUser.userEvent.subscribe((value) => { + if (value === null) { + return; + } this.user = value; }); } ngOnInit() { - this.service.getAuthUser(this.user.user_id).subscribe((data: UserData | null) => { + this.service.getAuthUser(this.user.user_id.toString()).subscribe((data: UserData | null) => { if (data === null) { return; }