Laravel Single Page Application (SPA) Authentication
This package allows you to add authentication in to your Laravel single page application.
Installation
You can install the package via composer:
composer require zerura/laravel-spa-authentication
The Passport service provider registers its own database migration directory with the framework, so you should migrate your database after installing the package. The Passport migrations will create the tables your application needs to store clients and access tokens:
php artisan migrate
Next, you should run the passport:install command. This command will create the encryption keys needed to generate secure access tokens. In addition, the command will create "personal access" and "password grant" clients which will be used to generate access tokens:
php artisan passport:install
Usage
Login
axios.post('api/login', {
email: 'jdoe@gmail.com',
password: 'password',
remember: true,
})
.then((data) => {
window.localStorage.removeItem('token');
window.sessionStorage.removeItem('token');
if (data.remember) {
window.localStorage.setItem('token', data.token);
} else {
window.sessionStorage.setItem('token', data.token);
}
})
.catch(({ response }) => {
// handle the errors.
})
Logout
axios.post('api/logout')
.then((data) => {
window.localStorage.removeItem('token');
window.sessionStorage.removeItem('token');
})
.catch(({ response }) => {
// handle the errors.
})
Register
axios.post('api/register', {
name: 'John Doe',
email: 'jdoe@gmail.com',
password: 'password'
})
.then((data) => {
window.localStorage.removeItem('token');
window.sessionStorage.removeItem('token');
if (data.remember) {
window.localStorage.setItem('token', data.token);
} else {
window.sessionStorage.setItem('token', data.token);
}
})
.catch(({ response }) => {
// handle the errors.
})
Forgot Password
axios.post('api/password/email', {
email: 'jdoe@gmail.com',
})
.then((data) => {
// handle success
})
.catch(({ response }) => {
// handle the errors.
})
Reset Password
axios.post('api/password/reset', {
token: // token from the url reset link,
email: // email from the url reset link
password: // new password
pasword_confirmation: // confirm new password
})
.then((data) => {
// handle success
})
.catch(({ response }) => {
// handle the errors.
})
Configuration
To customize the package configuration, use the vendor:publish Artisan command:
php artisan vendor:publish --tag=laravel-spa-authentication-config
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email darwinluague9001@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Laravel Package Boilerplate
This package was generated using the Laravel Package Boilerplate.