Skip to content

Instantly share code, notes, and snippets.

View marcosdeaguiar's full-sized avatar

Marcos de Aguiar marcosdeaguiar

  • Salvador - Bahia - Brazil
View GitHub Profile
@marcosdeaguiar
marcosdeaguiar / UserServiceTypeORM.ts
Created November 26, 2020 18:19
TypeORM user service.
import { getConnection, Repository } from "typeorm";
import { User } from "../entity/User";
export default class UserService {
private userRepository: Repository<User>;
constructor() {}
private getUserRepository(): Repository<User> {
@marcosdeaguiar
marcosdeaguiar / TypeORMApp.ts
Created November 26, 2020 18:13
App ts for TypeORM.
import express from "express";
import setupTypeORM from "./setuptypeorm";
var app = express();
// Creates the try connect loop.
setupTypeORM();
app.use(...);
// Start other middleware and routes...
@marcosdeaguiar
marcosdeaguiar / setuptypeorm.ts
Created November 26, 2020 18:06
Setup TypeORM connection loop.
import { createConnection, Connection, getConnection } from "typeorm";
import { CONNECTION_STRING } from "./config";
const setupTypeORM = async () => {
let connection: any = null;
while (connection == undefined || !connection.isConnected)
{
try {
@marcosdeaguiar
marcosdeaguiar / WrongTypeORMIndex.ts
Created November 26, 2020 18:01
TypeORM wrong app start.
import * as express from "express";
import ...;
import { createConnection } from "typeorm";
const app = express();
createConnection().then((connection) => {
app.use(...);
// Start other middleware and routes...
app.use(...);
});
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// ... Other middlewares ...
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseCookiePolicy();
app.UseJwtCookieMiddleware(app.ApplicationServices.GetService<IAntiforgery>(), Encoding.ASCII.GetBytes("signing key"));
public static class AntiforgeryCookieMiddleware
{
/// <summary>
/// Adds an antiforgery cookie to the response.
/// </summary>
/// <param name="app">Application builder instance.</param>
/// <param name="antiforgery">Antiforgery service.</param>
/// <param name="cookieName">Name of the cookie where the token will be added.</param>
public static void UseAntiforgeryCookieMiddleware(this IApplicationBuilder app,
IAntiforgery antiforgery,
[HttpGet]
[Route("logout")]
public IActionResult Logout()
{
HttpContext.User = new ClaimsPrincipal();
RefreshCSRFToken();
HttpContext.Response.Cookies.Delete("jwt");
return Ok();
public static class JwtCookieMiddleware
{
/// <summary>
/// Checks the jwt cookie and sets the user for the application.
/// </summary>
/// <param name="app">App builder reference.</param>
/// <param name="antiforgery">Reference to the antiforgery service.</param>
/// <param name="key">The key to decrypt the token.</param>
/// <param name="autoRefresh">Creates a new token if the token is half past expiration time (and still valid).</param>
/// <param name="cookieName">Name of the cookie where the jwt is (defaults to jwt).</param>
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Add other services...
private readonly UserService _userService;
private readonly IAntiforgery _antiforgery;
public UserApiController(UserService userService,
IAntiforgery antiforgery)
{
_userService = userService;
_antiforgery = antiforgery;
}