PHP code example of daycry / auth

1. Go to this page and download the library: Download daycry/auth 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/ */

    

daycry / auth example snippets


// Login
$result = auth()->attempt([
    'email'    => '[email protected]',
    'password' => 'secret',
]);

if ($result->isOK()) {
    return redirect()->to('/dashboard');
}

// Check authentication
if (auth()->loggedIn()) {
    $user = auth()->user();
    echo $user->email;
}

// Check authorization
if ($user->can('posts.create')) { ... }
if ($user->inGroup('admin')) { ... }

// Logout
auth()->logout();

// app/Config/Routes.php

// Require login
$routes->group('dashboard', ['filter' => 'session'], static function ($routes) {
    $routes->get('/', 'Dashboard::index');
});

// Require login + admin group
$routes->group('admin', ['filter' => 'session,group:admin'], static function ($routes) {
    $routes->get('/', 'Admin::index');
});

// Require a specific permission
$routes->post('posts/delete/(:num)', 'PostController::delete/$1', [
    'filter' => 'session,permission:posts.delete',
]);

// API with JWT
$routes->group('api', ['filter' => 'jwt'], static function ($routes) {
    $routes->get('profile', 'API\ProfileController::show');
});