Code Explainers

Code explainers tagged #authorization

javascript
const ROLE_PERMISSIONS = {
  admin: ['users:read', 'users:write', 'billing:read', 'billing:write'],
  manager: ['users:read', 'billing:read'],
  member: ['users:read'],

Role-based permissions middleware in Express

authorization middleware rbac
Intermediate 9 steps
php
<?php
 
namespace App\Broadcasting;
 

Authorizing presence channels in Laravel

broadcasting authorization presence-channels
Intermediate 3 steps
php
<?php
 
namespace App\Http\Controllers;
 

Broadcasting typing indicators in Laravel

websockets authorization presence-channels
Intermediate 9 steps
typescript
import {
  CanActivate,
  ExecutionContext,
  Injectable,

Role-based access with a NestJS guard

authorization guards decorators
Intermediate 6 steps
python
import asyncio
import json
 
from fastapi import APIRouter, Depends, HTTPException

Streaming export progress with SSE in FastAPI

server-sent-events async-generators streaming
Advanced 8 steps
python
from pathlib import Path
 
from django.contrib.auth.decorators import login_required
from django.http import FileResponse, Http404, HttpResponseForbidden

Serving files with access checks in Django

authorization http-caching file-serving
Intermediate 7 steps
ruby
class TasksController < ApplicationController
  before_action :set_project
 
  def reorder

Bulk task reordering with upsert_all in Rails

bulk-update upsert authorization
Intermediate 8 steps
java
@Configuration
@EnableWebSecurity
public class ResourceServerConfig {
 

Configuring a JWT resource server in Spring

oauth2 jwt authorization
Intermediate 8 steps
python
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.shortcuts import get_object_or_404
from django.views.generic import DetailView
 

Team membership access control in Django

access-control mixins class-based-views
Intermediate 7 steps
typescript
import {
  CanActivate,
  ExecutionContext,
  Injectable,

How guards chain auth in NestJS

guards authentication authorization
Intermediate 8 steps
python
from fastapi import APIRouter, Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from sqlalchemy.orm import Session

Chaining auth dependencies in FastAPI

dependency-injection jwt-authentication authorization
Intermediate 8 steps
java
@Configuration
@EnableWebSecurity
public class OAuth2SecurityConfig {
 

How OIDC login works in Spring Security

oauth2 openid-connect authorization
Intermediate 8 steps
typescript
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import { ConfigService } from '@nestjs/config';

How a JWT strategy authenticates in NestJS

authentication jwt passport
Intermediate 7 steps
typescript
import { CanActivate, ExecutionContext, Injectable, UnauthorizedException } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { Reflector } from '@nestjs/core';
import { JwtService } from '@nestjs/jwt';

How a GraphQL auth guard works in NestJS

authentication authorization jwt
Intermediate 7 steps
ruby
class DocumentsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_document
 

Serving secure file downloads in Rails

authorization signed-urls active-storage
Intermediate 8 steps
rust
use axum::{
    body::Body,
    extract::MatchedPath,
    http::{Request, StatusCode},

An admin route guard in Axum middleware

middleware authorization request-extensions
Intermediate 7 steps
java
@RestController
@RequestMapping("/api/documents")
@RequiredArgsConstructor
public class DocumentController {

Method-level authorization with @PreAuthorize in Spring

authorization spel rest-api
Intermediate 7 steps
php
<?php
 
namespace App\Http\Requests;
 

How a Laravel FormRequest validates invoices

validation authorization form-request
Intermediate 6 steps