Implement user-specific tutorial access and enhance authentication handling

This commit is contained in:
liosha84
2025-07-06 11:00:17 +03:00
parent 140de83d0a
commit 3fdb1b4640
18 changed files with 153 additions and 25 deletions
+7 -1
View File
@@ -65,7 +65,13 @@
"development": {
"optimization": false,
"extractLicenses": false,
"sourceMap": true
"sourceMap": true,
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.development.ts"
}
]
}
},
"defaultConfiguration": "production"
+2
View File
@@ -18,9 +18,11 @@ import 'prismjs';
import 'prismjs/components/prism-typescript.min.js';
import 'prismjs/plugins/line-numbers/prism-line-numbers.js';
import 'prismjs/plugins/line-highlight/prism-line-highlight.js';
import {authInterceptorProviders} from './helpers/auth.interceptor';
export const appConfig: ApplicationConfig = {
providers: [
authInterceptorProviders,
provideBrowserGlobalErrorListeners(),
provideAnimations(),
provideZoneChangeDetection({ eventCoalescing: true }),
@@ -3,6 +3,9 @@ import {Tutorial} from '../../models/tutorial.model';
import {TutorialService} from '../../services/tutorial.service';
import {MatCard, MatCardContent, MatCardHeader} from '@angular/material/card';
import {MarkdownComponent} from 'ngx-markdown';
import {authInterceptorProviders} from '../../helpers/auth.interceptor';
import {HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
import {CustomHttpInterceptor} from '../../helpers/custom-http-interceptor';
@Component({
selector: 'app-tutorials.component',
@@ -14,7 +17,12 @@ import {MarkdownComponent} from 'ngx-markdown';
],
templateUrl: './tutorials.component.html',
styleUrl: './tutorials.component.scss',
schemas: [CUSTOM_ELEMENTS_SCHEMA]
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [authInterceptorProviders,{
provide: HTTP_INTERCEPTORS,
useClass: CustomHttpInterceptor,
multi: true
}],
})
export class TutorialsComponent {
tutorials?: Tutorial[];
@@ -12,15 +12,25 @@ export class AuthInterceptor implements HttpInterceptor {
constructor(private token: TokenStorageService) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
let authReq = req;
req = req.clone({
withCredentials: true,
});
return next.handle(req);
/*let authReq = req;
const token = this.token.getToken();
if (token != null) {
authReq = req.clone({ headers: req.headers.set(TOKEN_HEADER_KEY, 'Bearer ' + token) });
}
return next.handle(authReq);
return next.handle(authReq);*/
}
}
export const authInterceptorProviders = [
{ provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true }
];
];
@@ -18,6 +18,9 @@ import {MatMenu, MatMenuItem, MatMenuTrigger} from '@angular/material/menu';
import {MatTooltip} from '@angular/material/tooltip';
import {DialogSignupComponent} from '../../components/dialog-signup.component/dialog-signup.component';
import {MatLabel} from '@angular/material/input';
import {HttpClient} from '@angular/common/http';
import { environment } from '../../../environments/environment';
@Component({
selector: 'app-main.component',
@@ -62,12 +65,13 @@ export class MainComponent implements OnInit{
eventBusSub?: Subscription;
private environment = environment;
private storageService: TokenStorageService = inject(TokenStorageService);
private authService: AuthService = inject(AuthService);
private eventBusService: EventBusService = inject(EventBusService);
constructor(private router: Router) {
constructor(private router: Router,private http: HttpClient) {
}
goToHome() {
@@ -136,7 +140,8 @@ export class MainComponent implements OnInit{
console.log(res);
this.storageService.clean();
window.location.reload();
//window.location.reload();
this.router.navigate([environment.default_page]);
},
error: err => {
console.log(err);
@@ -167,8 +172,9 @@ export class MainComponent implements OnInit{
this.dialogLoginData = result;
this.authService.login(this.dialogLoginData.username, this.dialogLoginData.password).subscribe(
data => {
this.storageService.saveToken(data.accessToken);
data => {
//this.storageService.saveToken(data.token);
this.storageService.saveUser(data);
this.isLoggedIn = true;
@@ -4,11 +4,12 @@ import {mainRouting} from './main.routing';
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {HTTP_INTERCEPTORS} from '@angular/common/http';
import {HTTP_INTERCEPTORS, provideHttpClient, withInterceptorsFromDi} from '@angular/common/http';
import {CustomHttpInterceptor} from '../helpers/custom-http-interceptor';
import {AngularImageViewerModule} from '@hreimer/angular-image-viewer';
import {BrowserModule} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {authInterceptorProviders} from '../helpers/auth.interceptor';
@NgModule({
declarations: [
@@ -22,7 +23,7 @@ import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
],
exports: [RouterModule],
providers: [{
providers: [authInterceptorProviders,provideHttpClient(withInterceptorsFromDi()),{
provide: HTTP_INTERCEPTORS,
useClass: CustomHttpInterceptor,
multi: true
@@ -19,6 +19,8 @@ export class AuthService {
username,
password
}, httpOptions);
}
register(username: string, email: string, password: string): Observable<any> {
@@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Tutorial } from '../models/tutorial.model';
import {text} from 'node:stream/consumers';
const baseUrl = 'http://localhost:8080/api/tutorials';
@@ -39,4 +40,4 @@ export class TutorialService {
findByTitle(title: any): Observable<Tutorial[]> {
return this.http.get<Tutorial[]>(`${baseUrl}?title=${title}`);
}
}
}
+3
View File
@@ -62,6 +62,9 @@ if (isMainModule(import.meta.url)) {
});
}
/**
* Request handler used by the Angular CLI (for dev-server and during build) or Firebase Cloud Functions.
*/