Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lukapeharda/cookie-guard
Cookie Guard is a Laravel API authentication package using cookie tokens
https://github.com/lukapeharda/cookie-guard
cookie laravel middleware token
Last synced: about 2 months ago
JSON representation
Cookie Guard is a Laravel API authentication package using cookie tokens
- Host: GitHub
- URL: https://github.com/lukapeharda/cookie-guard
- Owner: lukapeharda
- License: mit
- Created: 2016-09-18T15:37:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-03-22T10:37:05.000Z (almost 4 years ago)
- Last Synced: 2024-11-03T02:05:59.416Z (3 months ago)
- Topics: cookie, laravel, middleware, token
- Language: PHP
- Size: 8.79 KB
- Stars: 5
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Cookie Guard
## Introduction
Cookie Guard is a Laravel API authentication package using cookie tokens. Most of its inner workings are taken from Laravel Passport package. It is meant to be used to consume your own API with JavaScript.
## Installation
Require this package, with [Composer](https://getcomposer.org/), in the root directory of your project.
```bash
$ composer require lukapeharda/cookie-guard
```Add the service provider to `config/app.php` in the `providers` array.
```php
LukaPeharda\CookieGuard\CookieGuardServiceProvider::class,
```Add the `LukaPeharda\CookieGuard\HasApiTokens` trait to your `App\User` model. This trait will provide a few helper methods to your model which allow you to inspect the authenticated user's token and scopes:
```php
[
// Other middleware...
\LukaPeharda\CookieGuard\Http\Middleware\CreateFreshApiToken::class,
],
```## Usage
This middleware will attach a `laravel_token` cookie to your outgoing responses.
Finally, in your `config/auth.php` configuration file, you should set the `driver` option of the `api` authentication guard to `cookie`. This will instruct your application to use CookieGuards's `CookieGuard` when authenticating incoming API requests:
```php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'cookie',
'provider' => 'users',
],
],
```This middleware will attach a laravel_token cookie to your outgoing responses. This cookie contains an encrypted JWT that CookieGuard will use to authenticate API requests from your JavaScript application.
When using this method of authentication, you will need to send the CSRF token with every request via the X-CSRF-TOKEN header. Laravel will automatically send this header if you are using the default Vue configuration that is included with the framework:
```php
Vue.http.interceptors.push((request, next) => {
request.headers['X-CSRF-TOKEN'] = Laravel.csrfToken;
next();
});
```## License
Cookie Guard is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)