DekGenius.com
SHELL
sanctum auth check?
auth('sanctum')->check();
laravel sanctum
//install
composer require laravel/sanctum
// vendor
php artisan vendor:publish --provider="LaravelSanctumSanctumServiceProvider"
laravel sanctum
Broadcast::routes(['middleware' => ['auth:sanctum']]);
laravel sanctum Provoking tokens
// Revoke all tokens...
$user->tokens()->delete();
// Revoke a specific token...
$user->tokens()->where('id', $tokenId)->delete();
sanctum laravel
composer require laravel/sanctum
laravel sanctum issue token
use IlluminateHttpRequest;
Route::post('/tokens/create', function (Request $request) {
$token = $request->user()->createToken($request->token_name);
return ['token' => $token->plainTextToken];
});
sanctum laravel github
php artisan vendor:publish --provider="LaravelSanctumSanctumServiceProvider"
laravel sanctum
return $user->createToken('token-name', ['server:update'])->plainTextToken;
laravel sanctum
use AppModelsSanctumPersonalAccessToken;
use LaravelSanctumSanctum;
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
}
laravel sanctum
use IlluminateHttpRequest;
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
laravel sanctum
axios.defaults.withCredentials = true;
laravel sanctum
use LaravelSanctumHasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
}
laravel sanctum
// Revoke all tokens...
$user->tokens()->delete();
// Revoke the token that was used to authenticate the current request...
$request->user()->currentAccessToken()->delete();
// Revoke a specific token...
$user->tokens()->where('id', $tokenId)->delete();
laravel sanctum
if ($user->tokenCan('server:update')) {
//
}
laravel sanctum
axios.get('/sanctum/csrf-cookie').then(response => {
// Login...
});
laravel sanctum
use LaravelSanctumPersonalAccessToken as SanctumPersonalAccessToken;
class PersonalAccessToken extends SanctumPersonalAccessToken
{
// ...
}
laravel sanctum
foreach ($user->tokens as $token) {
//
}
laravel sanctum
return $request->user()->id === $server->user_id &&
$request->user()->tokenCan('server:update')
laravel sanctum
'domain' => '.domain.com',
laravel sanctum api
<?php
public function login(Request $request)
{
if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){
$user = Auth::user();
$success['token'] = $user->createToken('MyApp')->plainTextToken;
$success['name'] = $user->name;
return $this->sendResponse($success, 'User login successfully.');
}
else{
return $this->sendError('Unauthorised.', ['error'=>'Unauthorised']);
}
}
}
© 2022 Copyright:
DekGenius.com