Skip to content

Instantly share code, notes, and snippets.

View PradeepLoganathan's full-sized avatar

Pradeep Loganathan PradeepLoganathan

View GitHub Profile
@PradeepLoganathan
PradeepLoganathan / TokenService.ts
Created May 19, 2018 11:52
Angular service to get a JWT token from the token endpoint
import { OnInit, Injectable } from "@angular/core";
import { Observable } from "rxjs/Observable";
import {
HttpClient,
HttpHeaders,
HttpErrorResponse
} from "@angular/common/http";
import { environment } from "../../../../environments/environment";
import { tap, catchError } from "rxjs/operators";
import { ErrorObservable } from "rxjs/observable/ErrorObservable";
providers: [
CandidateService,
NotificationService,
UserLoginService,
UserRegistrationService,
LoggedInUserService,
TemplateCategoryService,
ResumeGeneratorService,
{provide: HTTP_INTERCEPTORS, useClass: AddTokenInterceptor, multi:true},
@PradeepLoganathan
PradeepLoganathan / TokenInterceptor.ts
Last active May 19, 2018 11:48
Angular 5 HTTP Interceptor. This interceptor adds a JWT token to all outgoing API requests.
import { HttpInterceptor, HttpSentEvent, HttpHeaderResponse, HttpHandler, HttpEvent, HttpRequest, HttpHeaders, HttpClient, HttpErrorResponse } from "@angular/common/http";
import { Observable } from "rxjs/Observable";
import { Injectable } from "@angular/core";
import { environment } from "../../../environments/environment";
import { tap, catchError } from "rxjs/operators";
import { ErrorObservable } from "rxjs/observable/ErrorObservable";
@Injectable()
export class AddTokenInterceptor implements HttpInterceptor {
@PradeepLoganathan
PradeepLoganathan / startup.cs
Created May 13, 2018 16:44
Adding a middleware component with MapWhen
app.MapWhen( context => context.Request.Query.ContainsKey("querypath1"), (appbuilder) =>
{
appbuilder.Use(async (context, next) =>
{
await context.Response.WriteAsync("-- Map when -- querypath1 - Middleware One</br>");
});
});
@PradeepLoganathan
PradeepLoganathan / startup.cs
Created May 13, 2018 15:30
Creating a middleware pipeline with map
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Middleware One</br>");
await next.Invoke();
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Middleware Two</br>");
await next.Invoke();
@PradeepLoganathan
PradeepLoganathan / startup.cs
Last active May 13, 2018 15:12
Multiple inline middleware components
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Middleware One</br>");
await next.Invoke();
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Middleware Two</br>");
await next.Invoke();
@PradeepLoganathan
PradeepLoganathan / startup.cs
Last active May 13, 2018 15:03
Simple Middleware with Use
app.Use(async (context, next) =>
{
await context.Response.WriteAsync("Middleware One");
await next.Invoke();
});
@PradeepLoganathan
PradeepLoganathan / startup.cs
Last active May 10, 2018 10:03
Using the exception handling middleware in statup. Remember to add it before UseMVC
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("AllowAll");
app.UseExceptionHandlingMiddleware();
app.UseMvc();
//app.UseExceptionHandler(appbuilder =>
//{
// appbuilder.Run(async context =>
@PradeepLoganathan
PradeepLoganathan / ExceptionHandlerMiddleware.cs
Created May 10, 2018 08:14
Static method to make it easier to use the exception handler middleware in startup class
using Microsoft.AspNetCore.Builder;
namespace thetalentbot.JobSeeker.Exceptions
{
public static class ExceptionHandlerMiddleware
{
public static IApplicationBuilder UseExceptionHandlingMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<ExceptionHandler>();
}
@PradeepLoganathan
PradeepLoganathan / ExceptionHandler.cs
Last active April 20, 2020 10:35
Custom ErrorHandler middleware for ASP.Net core API
using System;
using System.Net;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Hosting;
namespace thetalentbot.JobSeeker.Exceptions
{
public class ExceptionHandler