authentication

typescript
import {
  WebSocketGateway,
  WebSocketServer,
  SubscribeMessage,
  OnGatewayConnection,
  OnGatewayDisconnect,

Room-Based Live Notifications over a NestJS WebSocket Gateway

nestjs websockets socket-io
by codesnips 4 tabs
go
package session

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"

Stateless Session Cookies Signed and Verified With HMAC in Go

go security cookies
by codesnips 3 tabs
javascript
const jwt = require('jsonwebtoken');

const SECRET = process.env.JWT_SECRET;
const ALGORITHM = 'HS256';
const ACCESS_TTL = '15m';

JWT Authentication Middleware in Express That Populates req.user

express jwt authentication
by codesnips 3 tabs
javascript
import { createContext, useContext, useEffect, useMemo, useState } from 'react';
import { fetchCurrentUser, postLogin, postLogout } from './api';

const AuthContext = createContext(null);

export function AuthProvider({ children }) {

Protecting React Routes with an Auth Context and a RequireAuth Wrapper

react react-router authentication
by codesnips 4 tabs
javascript
const jwt = require('jsonwebtoken');

function authenticate(req, res, next) {
  const header = req.headers.authorization || '';
  const [scheme, token] = header.split(' ');

Role-Based Access Control in Express with a requireRole Middleware Factory

express middleware rbac
by codesnips 3 tabs
ruby
secret = ROTP::Base32.random
current_user.update!(otp_secret: secret)

totp = ROTP::TOTP.new(secret, issuer: 'CodeSnips')
provisioning_uri = totp.provisioning_uri(current_user.email)

TOTP based multi factor authentication for sensitive actions

mfa totp authentication
by Kai Nakamura 1 tab
rust
use std::fmt;

#[derive(Debug)]
pub enum HashError {
    Backend(String),
    InvalidHash,

Pluggable Password Hasher Trait in Rust with Argon2 and Bcrypt Backends

rust argon2 bcrypt
by codesnips 3 tabs
python
from argon2 import PasswordHasher

password_hasher = PasswordHasher(
    time_cost=3,
    memory_cost=65536,
    parallelism=4,

Password hashing with Argon2 and bcrypt migration paths

passwords argon2 bcrypt
by Kai Nakamura 1 tab
ruby
class ApplicationController < ActionController::Base
  before_action :authenticate_user!

  rescue_from ActionController::InvalidAuthenticityToken do
    handle_unauthenticated(reason: :csrf)
  end

Turbo Streams: partial page auth failure handling

rails turbo hotwire
by codesnips 3 tabs
java
package com.example.security;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;

Stateless JWT Authentication with a Token Service and a Spring Security Filter

spring-security jwt authentication
by codesnips 3 tabs
php
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;

Laravel Sanctum for API authentication

laravel sanctum authentication
by Carlos Mendez 3 tabs
ruby
class DownloadsController < ApplicationController
  before_action :authenticate_user!, only: :create
  skip_before_action :verify_authenticity_token, only: :show

  def create
    document = current_user.documents.find(params[:document_id])

Signed, Expiring Download URLs With HMAC Verification in Rails

rails hmac security
by codesnips 3 tabs