Skip to content

Instantly share code, notes, and snippets.

@GerritRiesch94
GerritRiesch94 / main.ts
Created July 10, 2023 13:33
Interceptor new way
import { bootstrapApplication } from "@angular/platform-browser";
import { AppComponent } from "./app/app.component";
import { HttpHandlerFn, HttpRequest, provideHttpClient, withInterceptors } from "@angular/common/http";
import { provideRouter } from "@angular/router";
import { inject } from "@angular/core";
import { HeaderService } from "./app/header.service";
import { MY_ROUTES } from "./app/app.routes";
bootstrapApplication(AppComponent, {
providers: [
@GerritRiesch94
GerritRiesch94 / interceptor-common-way.module.ts
Last active July 10, 2023 13:33
Interceptor module common way
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HTTP_INTERCEPTORS, HttpClientModule } from "@angular/common/http";
import { MyInterceptor } from "./my-interceptor";
@NgModule({
declarations: [
AppComponent
@GerritRiesch94
GerritRiesch94 / intercepter-common-way.ts
Last active July 10, 2023 13:33
Interceptor common way
import { Injectable } from "@angular/core";
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from "@angular/common/http";
import { Observable } from "rxjs";
import { HeaderService } from "./header.service";
@Injectable()
export class MyInterceptor implements HttpInterceptor {
constructor(private headerService: HeaderService) {
}
intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
@GerritRiesch94
GerritRiesch94 / home.component.html
Created April 13, 2023 13:11
Home Component HTML
<p>home works and can be reached using functional guards!</p>
<p *ngIf="open">Hello there</p>
@GerritRiesch94
GerritRiesch94 / home.component.ts
Created April 13, 2023 13:09
Standalone Home Component
import { Component } from '@angular/core';
import { NgIf } from '@angular/common';
@Component({
selector: 'app-home',
standalone: true,
imports: [NgIf],
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
@GerritRiesch94
GerritRiesch94 / app-routing-new.module.ts
Last active April 13, 2023 13:19
New App Routing Module with functional guard
const routes: Routes = [
{
path: 'home',
loadComponent: () => import('./home/home.component').then(m => m.HomeComponent),
canActivate: [(): boolean => inject(AuthService).isAuthenticated()]
}
];
@GerritRiesch94
GerritRiesch94 / auth.guard.ts
Created April 13, 2023 13:00
AuthGuard old
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
@GerritRiesch94
GerritRiesch94 / app-routing.module.ts
Last active April 13, 2023 13:18
Old App Routing Module with AuthGuard
const routes: Routes = [
{
path: 'home',
loadComponent: () => import('./home/home.component').then(m => m.HomeComponent),
canActivate: [AuthGuard]
}
];
@GerritRiesch94
GerritRiesch94 / AppController.java
Last active March 28, 2023 13:28
App Controller Spring Boot Example
package com.example.example.api;
import com.example.example.service.AppService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/data")
public class AppController {
@GerritRiesch94
GerritRiesch94 / app.controller.ts
Last active March 27, 2023 13:23
Nest.js Controller Example
import { Controller, Get, Param, Query, Res } from '@nestjs/common';
import { AppService } from './app.service';
import { Response } from 'express';
@Controller('api/data')
export class AppController {
constructor(private readonly appService: AppService) {}
@Get(':id')
async getData(