Fix user creation

This commit is contained in:
aditya.chandel
2025-03-08 11:38:12 -07:00
parent 4d9b0423ba
commit d705c7c04e
6 changed files with 13 additions and 21 deletions
@@ -29,7 +29,7 @@ export class BookService {
loadBooks(): void {
const currentState = this.bookStateSubject.value;
if (currentState.loaded) {
if (currentState.loaded && this.bookStateSubject.value.books != null) {
return;
}
this.http.get<Book[]>(this.url).pipe(
@@ -67,19 +67,21 @@ export class CreateUserDialogComponent implements OnInit {
};
this.userService.createUser(userData).subscribe({
next: response => {
next: () => {
this.messageService.add({
severity: 'success',
summary: 'User Created',
detail: response.message
detail: 'The user has been successfully created.'
});
this.ref.close(true);
},
error: err => {
error: (err) => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to create user: ' + err.error.message
summary: 'User Creation Failed',
detail: err?.error?.message
? `Unable to create user: ${err.error.message}`
: 'An unexpected error occurred while creating the user. Please try again later.'
});
}
});
@@ -19,7 +19,7 @@
</div>
<div *ngIf="!books || books.length === 0">
<div *ngIf="!books">
<p-progressSpinner></p-progressSpinner>
</div>
@@ -7,6 +7,7 @@
.dashboard-scroller-no-books {
padding-top: 3rem;
padding-bottom: 3rem;
display: flex;
justify-content: center;
align-items: center;
+1 -1
View File
@@ -36,7 +36,7 @@ export class LoginComponent {
this.authService.login({ username: this.username, password: this.password }).subscribe({
next: (response) => {
this.authService.saveToken(response.token);
this.router.navigate(['/all-books']);
this.router.navigate(['/dashboard']);
},
error: () => {
this.errorMessage = 'Invalid username or password';
+2 -13
View File
@@ -82,23 +82,12 @@ export class UserService {
return this.http.get<User>(`${this.userUrl}/me`);
}
getUserFromAPI(): Observable<User> {
const token = localStorage.getItem('token');
if (token) {
const decoded: JwtPayload & { userId?: string } = jwtDecode(token);
if (decoded.userId) {
return this.http.get<User>(`${this.userUrl}/${decoded.userId}`);
}
}
return of(null as unknown as User);
}
getLocalUser(): User | null {
return this.userDataSubject.getValue();
}
createUser(userData: Omit<User, 'id'>): Observable<{ message: string }> {
return this.http.post<{ message: string }>(this.apiUrl, userData);
createUser(userData: Omit<User, 'id'>): Observable<void> {
return this.http.post<void>(this.apiUrl, userData);
}
getUsers(): Observable<User[]> {