1composer create-project --prefer-dist laravel/laravel appname
2
3composer require laravel/sanctum
4
5php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
6
7composer require laravel/jetstream
8
9php artisan jetstream:install inertia
10
11php artisan migrate
12
13npm install
14
15php artisan vendor:publish --tag=jetstream-views
16
17php artisan serve
18
19npm run hot
1use App\Models\User;
2use Illuminate\Http\Request;
3use Illuminate\Support\Facades\Hash;
4use Illuminate\Validation\ValidationException;
5
6Route::post('/sanctum/token', function (Request $request) {
7 $request->validate([
8 'email' => 'required|email',
9 'password' => 'required',
10 'device_name' => 'required',
11 ]);
12
13 $user = User::where('email', $request->email)->first();
14
15 if (! $user || ! Hash::check($request->password, $user->password)) {
16 throw ValidationException::withMessages([
17 'email' => ['The provided credentials are incorrect.'],
18 ]);
19 }
20
21 return $user->createToken($request->device_name)->plainTextToken;
22});
1php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
1window.Echo = new Echo({
2 broadcaster: "pusher",
3 cluster: process.env.MIX_PUSHER_APP_CLUSTER,
4 encrypted: true,
5 key: process.env.MIX_PUSHER_APP_KEY,
6 authorizer: (channel, options) => {
7 return {
8 authorize: (socketId, callback) => {
9 axios.post('/api/broadcasting/auth', {
10 socket_id: socketId,
11 channel_name: channel.name
12 })
13 .then(response => {
14 callback(false, response.data);
15 })
16 .catch(error => {
17 callback(true, error);
18 });
19 }
20 };
21 },
22})
1use Illuminate\Http\Request;
2
3Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
4 return $request->user();
5});
1// Revoke all tokens...
2$user->tokens()->delete();
3
4// Revoke the token that was used to authenticate the current request...
5$request->user()->currentAccessToken()->delete();
6
7// Revoke a specific token...
8$user->tokens()->where('id', $tokenId)->delete();