PHP code example of mariojgt / castle

1. Go to this page and download the library: Download mariojgt/castle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mariojgt / castle example snippets


use Mariojgt\Castle\Trait\Castle;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable, Castle;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

use Mariojgt\Castle\Helpers\AuthenticatorHandle;

class myController
{
    public register () {
	    // Start the class that handle most of the logic
	    $handle = new AuthenticatorHandle();
	    // Generate the code
		$codeInfo =	    $handle->generateCode($userEmail);
		// Sync that code with the user using the trait
		Auth()->user()->syncAuthenticator($codeInfo['secret']);
    }

// Auth Route Example
Route::group([
    'middleware' => ['web', '2fa'], // note you can use (2fa:admin) for admin guard or leave empty for web as default
], function () {
    // Example page 

Auth()->user()->getCodes; // this will return the backup codes for that user

use Mariojgt\Castle\Helpers\AuthenticatorHandle;

myclass {

	public myFunction () {
		 // Start the class that handle most of the logic
		$handle = new AuthenticatorHandle();
		// the encryption is using the normal laravel encrypt fuction // example encrypt('user_secret')
		$handle->useBackupCode($codeYouType, $encryptauthenticatorSecret); // The second parameter is not 
bash
  composer install::castle