PHP code example of notion / swaycookie

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

    

notion / swaycookie example snippets


'guards' => [
    'user:api' => [
        'driver' => 'sway',
        'provider' => 'users',  // Define the provider for your model
    ],
    'admin:api' => [
        'driver' => 'sway',
        'provider' => 'admins',  // Define the provider for your model
    ],
],

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->middleware(["authorized:" . 'user:api'])->group(function () {
    // Your routes
});

Route::prefix('v1')->middleware(["authorized:" . 'admin:api'])->group(function () {
    // Your routes
});

public function login(Request $request)
    {
        // Validate the request input
        $request->validate([
            'email' => 'he custom guard to attempt login and generate tokens
        $accessToken = Auth::guard('user:api')->attempt($credentials);

        return response()->json([
            'tokens' => $accessToken,
            'token_type' => 'bearer',
        ]);
    }

// Get Authenticated User model
Auth::guard('user:api')->user();
// Get Authenticated Admin model
Auth::guard('admin:api')->user();

// Recomended approach
$request->user()

use Sway\Traits\InvalidatableToken;

class User extends Authenticatable
{
    use InvalidatableToken;
}

public function logout(Request $request)
{
    $user = $request->user();
    $deleted = $user->invalidateToken(); // Calls the invalidateToken method defined in the trait

    return response()->json([
        'success' => $deleted,
    ]);
}