Skip to content

Instantly share code, notes, and snippets.

View thibaut-decherit's full-sized avatar

Thibaut Decherit thibaut-decherit

View GitHub Profile
@thibaut-decherit
thibaut-decherit / React Native - AsyncStorage custom service.md
Last active August 23, 2023 16:21
React Native - AsyncStorage custom service

React Native - AsyncStorage custom service

app/components/services/storageService/StorageService.js:

import {AsyncStorage} from 'react-native';

export default class StorageService {

    static getAsyncStorageItem(key) {
        return new Promise((resolve, reject) => {
@thibaut-decherit
thibaut-decherit / index.php
Created March 15, 2019 11:20 — forked from thomasmerlin/index.php
[PHP] - Getting array max depth | Disclaimer & Warning : This function may not handle every case, but works for almost basic ones.
<?php
// Function
/**
* Get the array max depth.
*
* @param array $array
*
* @return int
*/
private function getArrayMaxDepth(array $array): int
@thibaut-decherit
thibaut-decherit / Symfony - LocaleRedirectToClientPreference.md
Last active August 23, 2023 16:32
Symfony - Locale Redirect To Client Preference

Symfony - LocaleRedirectToClientPreference

Redirects to browser preferred locale if this locale is supported by the application.

config/services.yml

App\EventListener\LocaleRedirectToClientPreference:
  arguments:
    $router: '@router'
    $defaultLocale: '%kernel.default_locale%' # framework.default_locale in config/packages/translation.yaml
@thibaut-decherit
thibaut-decherit / jQuery - Selector supporting future DOM elements (nonexistent on document load).md
Last active August 23, 2023 16:31
jQuery - Selector supporting future DOM elements (nonexistent on document load)

jQuery - Selector supporting future DOM elements (nonexistent on document load)

Also supports DOM elements existent on document load. Potentially heavier on performance than "classic" selector.

Selector

/*
Used instead of $('.my-element').click(function (e) {} to be able to listen to click on elements with my-element class
created by JS insertion into DOM after document load.
@thibaut-decherit
thibaut-decherit / Symfony - Password rehash on authentication if auto encoder settings changed & legacy password hashes migration.md
Last active August 24, 2023 09:13
Symfony - Password rehash on authentication if auto encoder settings changed & legacy password hashes migration

Disclaimer

Password rehash on login if needed is natively handled by Symfony since 4.4. See https://symfony.com/blog/new-in-symfony-4-4-password-migrations.

The legacy password hashes migration part might still be of use though, but beware of password shucking: If the legacy hash is not salted and is present in data breaches from other platforms, overhashing might have little to no effect.

Password rehash on authentication if auto encoder settings changed

config/packages/security.yaml

security:
@thibaut-decherit
thibaut-decherit / Symfony - Head and Page Title Generation.md
Last active August 23, 2023 16:25
Symfony - Head and Page Title Generation

Symfony - Head and Page Title Generation

app/config/config.yml:

parameters:
  website_name: Website name here
twig:
  globals:
    website_name: '%website_name%'
@thibaut-decherit
thibaut-decherit / Symfony - Twig to JavaScript Data Passing.md
Last active March 1, 2024 23:47
Symfony - Twig to JavaScript Data Passing

Symfony - Twig to JavaScript Data Passing

See https://symfony.com/doc/current/frontend/encore/server-data.html for context.

Requirements

Twig filter to merge arrays with array_merge_recursive() instead of array_merge(). Required to prevent Twig merge filter from overwritting twig_to_js_global_data string keys if extra_data has identical keys (e.g. both have a translations key).

Basic setup

Add this to your _base.html.twig, probably just before {% block javascripts %}

@thibaut-decherit
thibaut-decherit / Symfony - Response Header Setter (static, CSP and response authenticity).md
Last active November 17, 2023 20:18
Symfony - Response Header Setter (static, CSP and response authenticity)

Features

  • Event listener triggered on each response through onKernelResponse() method
  • Adds custom headers to the response
  • Support for "static" headers specified in config/response_header_setter/response_headers.yaml
    • Currently includes security / privacy related headers:
      • Cross-Origin-Opener-Policy
      • Cross-Origin-Resource-Policy
      • Referrer-Policy
  • Strict-Transport-Security (remember to register the domain on https://hstspreload.org/ or preload will not work)
@thibaut-decherit
thibaut-decherit / jQuery - Submit and button spamming prevention + Bootstrap spinner.md
Last active August 23, 2023 16:24
jQuery - Submit and button spamming prevention + Bootstrap spinner

jQuery - Submit and button spamming prevention + Bootstrap spinner

Supports any <form> with disable-on-submit class AND including a <button> with type="submit". Also supports any standalone <button> with disable-on-click class.

Supports replacement of Font Awesome icons: On submit/click, if button has a Font Awesome icon it will be replaced by the Bootstrap spinner.

Note: You can use it on <input type="submit"> instead of <button> but only for spamming prevention, Bootstrap spinner will not be displayed.

assets/js/components/submit-and-button-spamming-prevention.js

@thibaut-decherit
thibaut-decherit / jQuery - Click outside of element listener.md
Last active August 23, 2023 16:24
jQuery - Click outside of element listener

jQuery - Click outside of element listener

Can be used to close an element previously opened by the user (e.g. a menu, a collapse...)

example.js:

$('#my-element').click(function () {
    if (!$(this).hasClass('open')) {
        openElement();
    } else {