Code Explainers

Code explainers tagged #authentication

python
import base64
import hashlib
import hmac
import json

Building signed expiring tokens with HMAC

hmac authentication base64
Intermediate 7 steps
php
<?php
 
namespace App\View\Composers;
 

How a Laravel view composer feeds the navigation

view-composer caching dependency-injection
Intermediate 5 steps
typescript
import {
  WebSocketGateway,
  WebSocketServer,
  SubscribeMessage,

Building a real-time chat gateway in NestJS

websockets real-time socket.io
Intermediate 8 steps
python
from functools import wraps
 
import jwt
from flask import current_app, g, jsonify, request

How a JWT auth decorator works in Flask

decorators authentication jwt
Intermediate 6 steps
ruby
class PasswordResetsController < ApplicationController
  RESET_PURPOSE = :password_reset
  RESET_EXPIRY = 15.minutes
 

Password resets with signed global IDs in Rails

authentication signed-tokens global-id
Intermediate 9 steps
python
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView
 
from .models import Invoice

Building a scoped, filtered ListView in Django

class-based views queryset filtering authentication
Intermediate 7 steps
php
<?php
 
namespace App\Providers;
 

Defining rate limiters in Laravel

rate limiting service provider closures
Intermediate 7 steps
rust
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
use chrono::{Duration, Utc};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc};

Refresh token rotation in Axum

authentication token-rotation reuse-detection
Advanced 9 steps
rust
use axum::{
    extract::{FromRef, FromRequestParts},
    http::{request::Parts, StatusCode},
    response::{IntoResponse, Response},

A custom API-key extractor in Axum

authentication extractors trait-implementation
Advanced 8 steps
javascript
import { NextResponse } from 'next/server';
import { jwtVerify } from 'jose';
 
const PROTECTED_PREFIXES = ['/dashboard', '/settings', '/billing'];

Gating routes with Next.js middleware and JWTs

authentication middleware jwt
Intermediate 8 steps
python
from functools import wraps
from flask import Blueprint, session, request, redirect, url_for, flash, render_template
from werkzeug.security import check_password_hash
 

Session-based auth with a Flask Blueprint

authentication decorators sessions
Intermediate 9 steps
typescript
import { inject } from '@angular/core';
import {
  ActivatedRouteSnapshot,
  CanActivateFn,

How a functional route guard works in Angular

route-guards dependency-injection rxjs
Intermediate 5 steps
javascript
const express = require('express');
const session = require('express-session');
const RedisStore = require('connect-redis').default;
const { createClient } = require('redis');

Redis-backed session auth in Express

sessions authentication redis
Intermediate 8 steps
rust
use axum::{
    async_trait,
    extract::{FromRequestParts, State},
    http::{header, request::Parts, StatusCode},

Custom auth extractors in Axum

extractors authentication error-handling
Intermediate 9 steps
typescript
import { Injectable } from '@angular/core';
import {
  HttpInterceptor,
  HttpHandler,

Refreshing tokens with an Angular HttpInterceptor

http-interceptor token-refresh rxjs
Advanced 8 steps
python
import os
from datetime import datetime, timezone
 
import jwt

JWT bearer auth as a FastAPI dependency

authentication jwt dependency-injection
Intermediate 7 steps
go
package middleware
 
import (
	"crypto/subtle"

Building an API-key middleware in Gin

middleware authentication dependency-injection
Intermediate 5 steps
go
package api
 
func RegisterRoutes(r *gin.Engine, deps *Dependencies) {
	r.GET("/health", func(c *gin.Context) {

Layering route groups and middleware in Gin

routing middleware authentication
Intermediate 8 steps