Thursday 19 October 2017

To create a custom token password reset in Laravel 5.5 (Or Lumen)

Please use the below passwordResetToken() function to get the token for password reset for custom use.

namespace App\Traits;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Mail\Message;
use Illuminate\Support\Facades\Password;

trait ResetPasswordTrait 
{
    ...
    ...
    ...
    ...

    /**
     * Get the token for password reset and update the token in the database table.
     *
     * @return string|null
     */
    public function passwordResetToken($user)
    {
        $this->broker = 'users';

        $broker = $this->getBroker();

        return Password::broker($broker)->createToken($user);
    }

    /**
     * Get the broker to be used during password reset.
     *
     * @return string|null
     */
    public function getBroker()
    {
        return property_exists($this, 'broker') ? $this->broker : null;
    }

}


// Reuse in the class
use App\Traits\ResetPasswordTrait;

class NewUserAccount 
{

    use ResetPasswordTrait;

    // Sample function to print the password reset token
    public function printPasswordResetToken(Request $request)
    {
        $user = User::find( $request->input('id') );
        $passwordResetToken = $this->passwordResetToken($user);
        print_r($passwordResetToken);
    }

}

Thursday 12 October 2017

Generate Ramdon String on Shell

# Bash function to generate the random alphanumeric string with provided length.
randomString() {
    DEFAULT_LENGTH=6

    # if the parameter is not null, apply new length
    if [ ! -z "$1" -a "$1" != " " ]; then
        LENGTH=$1
    else
        LENGTH=$DEFAULT_LENGTH
    fi
    echo $LENGTH;

    LC_ALL=C tr -dc 'a-zA-Z0-9' < /dev/urandom | fold -w $LENGTH | head -n 1
}

Test One:
MyRadomString=$(randomString)
echo "My Radom String: '$MyRadomString'";

# Output:
My Radom String: 'a2d6sd';

Test Two:
MyRadomString=$(randomString 8)
echo "My Radom String2: $MyRadomString";

# Output:
My Radom String2: 'S2sP6sk7';

Tuesday 10 October 2017

Only update the lock file hash after updating the version of your own package


Only updates the lock file hash to suppress warning about the lock file being out of date.

Rewrite the lock and autoloaders, but otherwise will keep everything that's installed as is.

 composer update --lock 
OR
composer update nothing

Friday 6 October 2017

Regular expression to filter the float value with minimum maximum length before and after the decimal

The below javascript example shows whether the given input has float value with limited length numbers before (min 2 to max 5) and after (2) the decimal point .

The Regix is : /^([0-9]{2,5}\.?[0-9]{2})$/
 
function isFloat(input)
{
  var regxExp = /^([0-9]{2,5}\.?[0-9]{2})$/
 var result = input.search(regxExp);
  console.log('Is '+ input + ' valid float number: ', result);
  
  return (result < -1) ? true : false;
}

var input = '312g37.78';
var result = isFloat(input);
console.log('Is '+ input + ' valid float number: ', result);

// Output (FALSE due to invalid letter)
Is "312s37.78" valid float number:  false

var input = '312327.782';
var result = isFloat(input);
console.log('Is '+ input + ' valid float number: ', result);

// Output: (FALSE due to invalid length)
Is "312327.782" valid float number:  false

var input = '3.78';
var result = isFloat(input);
console.log('Is "'+ input + '" valid float number: ', result);
// Output: (FALSE due to invalid length which is less than two (before the decimal point))
Is "3.78" valid float number:  false

var input = '31237.78';
var result = isFloat(input);
console.log('Is '+ input + ' valid float number: ', result);
// Output
Is "31237.78" valid float number:  true

var input = '317.78';
var result = isFloat(input);
console.log('Is "'+ input + '" valid float number: ', result);
// Output
Is "317.78" valid float number:  true