Checkpoint: Authentication

This commit is contained in:
aditya.chandel
2025-02-15 21:43:06 -07:00
parent e1685b2bf8
commit d3627a2d3f
25 changed files with 669 additions and 60 deletions
+23 -26
View File
@@ -1,38 +1,35 @@
import {Routes} from '@angular/router';
import {PdfViewerComponent} from './book/components/pdf-viewer/pdf-viewer.component';
import {BookBrowserComponent} from './book/components/book-browser/book-browser.component';
import {MainDashboardComponent} from './dashboard/components/main-dashboard/main-dashboard.component';
import {AppLayoutComponent} from './layout/component/layout-main/app.layout.component';
import {EpubViewerComponent} from './epub-viewer/component/epub-viewer.component';
import {SettingsComponent} from './core/component/settings/settings.component';
import { Routes } from '@angular/router';
import { PdfViewerComponent } from './book/components/pdf-viewer/pdf-viewer.component';
import { BookBrowserComponent } from './book/components/book-browser/book-browser.component';
import { MainDashboardComponent } from './dashboard/components/main-dashboard/main-dashboard.component';
import { AppLayoutComponent } from './layout/component/layout-main/app.layout.component';
import { EpubViewerComponent } from './epub-viewer/component/epub-viewer.component';
import { SettingsComponent } from './core/component/settings/settings.component';
import { LoginComponent } from './login/login.component';
import { AuthGuard } from './auth.guard';
export const routes: Routes = [
{
path: '', component: AppLayoutComponent,
path: '',
component: AppLayoutComponent,
children: [
{
path: '', component: MainDashboardComponent,
},
{
path: 'all-books', component: BookBrowserComponent,
},
{
path: 'settings', component: SettingsComponent,
},
{
path: 'library/:libraryId/books', component: BookBrowserComponent,
},
{
path: 'shelf/:shelfId/books', component: BookBrowserComponent,
}
{ path: '', component: MainDashboardComponent, canActivate: [AuthGuard] },
{ path: 'all-books', component: BookBrowserComponent, canActivate: [AuthGuard] },
{ path: 'settings', component: SettingsComponent, canActivate: [AuthGuard] },
{ path: 'library/:libraryId/books', component: BookBrowserComponent, canActivate: [AuthGuard] },
{ path: 'shelf/:shelfId/books', component: BookBrowserComponent, canActivate: [AuthGuard] },
]
},
{
path: 'pdf-viewer/book/:bookId',
component: PdfViewerComponent
component: PdfViewerComponent,
canActivate: [AuthGuard]
},
{
path: 'epub-viewer/book/:bookId',
component: EpubViewerComponent
}
component: EpubViewerComponent,
canActivate: [AuthGuard]
},
{ path: 'login', component: LoginComponent },
{ path: '**', redirectTo: 'login', pathMatch: 'full' }
];
@@ -0,0 +1,16 @@
import {HttpInterceptorFn} from '@angular/common/http';
export const AuthInterceptorService: HttpInterceptorFn = (req, next) => {
const token = localStorage.getItem('token');
if (token) {
const clonedReq = req.clone({
setHeaders: {
Authorization: `Bearer ${token}`
}
});
return next(clonedReq);
}
return next(req);
};
+13
View File
@@ -0,0 +1,13 @@
import {inject} from '@angular/core';
import {CanActivateFn, Router} from '@angular/router';
export const AuthGuard: CanActivateFn = (route, state) => {
const router = inject(Router);
const token = localStorage.getItem('token');
if (!token) {
router.navigate(['/login']);
return false;
}
return true;
};
@@ -0,0 +1,28 @@
import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AuthService {
private apiUrl = 'http://localhost:7050/api/v1/auth/login';
constructor(private http: HttpClient) {}
login(credentials: { username: string; password: string }): Observable<any> {
return this.http.post(this.apiUrl, credentials);
}
saveToken(token: string): void {
localStorage.setItem('token', token);
}
getToken(): string | null {
return localStorage.getItem('token');
}
logout(): void {
localStorage.removeItem('token');
}
}
@@ -68,6 +68,13 @@
</button>
<app-theme-configurator></app-theme-configurator>
</li>
<p-divider layout="vertical"/>
<li>
<button class="topbar-item" (click)="logout()" pTooltip="Logout" tooltipPosition="top">
<i class="pi pi-sign-out text-surface-700 dark:text-surface-100"></i>
</button>
</li>
</ul>
</div>
@@ -113,4 +113,9 @@ export class AppTopBarComponent implements OnDestroy {
navigateToSettings() {
this.router.navigate(['/settings']);
}
logout() {
localStorage.removeItem('token');
window.location.href = '/login';
}
}
@@ -0,0 +1,13 @@
<div class="login-container">
<h2>Login</h2>
<form (submit)="login()">
<label>Username</label>
<input type="text" [(ngModel)]="username" name="username" required />
<label>Password</label>
<input type="password" [(ngModel)]="password" name="password" required />
<button type="submit">Login</button>
<p *ngIf="errorMessage" class="error">{{ errorMessage }}</p>
</form>
</div>
@@ -0,0 +1,27 @@
.login-container {
@apply flex flex-col items-center justify-center min-h-screen bg-gradient-to-br from-blue-200 to-blue-500;
h2 {
@apply text-2xl font-bold text-gray-700 mb-4;
}
form {
@apply bg-white p-6 rounded-2xl shadow-lg w-96 flex flex-col gap-4;
label {
@apply text-sm font-semibold text-gray-600;
}
input {
@apply w-full p-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-400;
}
button {
@apply w-full bg-blue-500 text-white py-2 rounded-lg font-semibold hover:bg-blue-600 transition duration-300;
}
.error {
@apply text-red-500 text-sm font-medium mt-2;
}
}
}
@@ -0,0 +1,34 @@
import { Component } from '@angular/core';
import {AuthService} from '../core/service/auth.service';
import {Router} from '@angular/router';
import {FormsModule} from '@angular/forms';
import {NgIf} from '@angular/common';
@Component({
selector: 'app-login',
imports: [
FormsModule,
NgIf
],
templateUrl: './login.component.html',
styleUrl: './login.component.scss'
})
export class LoginComponent {
username = '';
password = '';
errorMessage = '';
constructor(private authService: AuthService, private router: Router) {}
login(): void {
this.authService.login({ username: this.username, password: this.password }).subscribe({
next: (response) => {
this.authService.saveToken(response.token);
this.router.navigate(['/all-books']);
},
error: () => {
this.errorMessage = 'Invalid username or password';
}
});
}
}