Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/salmanzafar949/laravel-jwt-auto-installer

A Laravel Library that let's you add tymon jwt auth library and all it's features with single command
https://github.com/salmanzafar949/laravel-jwt-auto-installer

jwt-auth laravel laravel-framework laravel-package

Last synced: 6 days ago
JSON representation

A Laravel Library that let's you add tymon jwt auth library and all it's features with single command

Awesome Lists containing this project

README

        

# laravel-jwt-auto-installer

[![Latest Version](https://img.shields.io/github/issues/salmanzafar949/laravel-jwt-auto-installer)](https://github.com/salmanzafar949/laravel-jwt-auto-installer/releases)
![forks](https://img.shields.io/github/forks/salmanzafar949/laravel-jwt-auto-installer)
![stars](https://img.shields.io/github/stars/salmanzafar949/laravel-jwt-auto-installer)
![license](https://img.shields.io/github/license/salmanzafar949/laravel-jwt-auto-installer)
[![Total Downloads](https://img.shields.io/packagist/dt/salmanzafar/laravel-jwt-auto-installer?style=flat-square)](https://packagist.org/packages/salmanzafar/laravel-jwt-auto-installer)

A Laravel Library that let's you add tymon jwt auth library and all it's features with single command

## Why this was required?
The people who uses `tymon/jwt-auth` knows that every time they installs package they need to manually create AuthController and then copy and paste the entire code their and same things for Model and Routes.
So every time when you have to do this it takes some time and being a developer we want more easy and simple way of doing thing. so the i came up with this idea of creating this and hopefully it will help you and will make your work easy.

### what this package will do?
The package will create the following things for you

* installs ```tymon/jwt-auth``` for you
* Will publish ```AuthController (with code)``` inside ```App\Http\Controllers```
* ```User.php``` (Model with all code including jwt functions)
* Will publish Routes (will create all the routes for auth e.g```(login, register, authenticated user detail, refresh token, logout)``` in `api.php`)
* Will also publish the ```JWT_SECRET``` in your .env

## Installation

Use the package manager [composer](https://packagist.org/packages/salmanzafar/laravel-jwt-auto-installer) to install laravel-jwt-auto-installer.

```bash
composer require salmanzafar/laravel-jwt-auto-installer
```
## Enable the package (Optional)
This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel >= 5.5.

## Configuration
Publish the service provider file

This step is required
```bash
php artisan vendor:publish --provider="Salman\AutoJWT\AutoJWTServiceProvider"
```

## Usage

```bash
php artisan jwt:init

Controller Model and Routes and Jwt Secret published.Thanks
```
### That's it now it'll publish every thing you want e.g Controller Model(with functions jwt) and api routes for JWT

### AuthController.php:

```php
middleware('auth:api', ['except' => ['login','register']]);
}

/**
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
* @throws \Illuminate\Validation\ValidationException
*/
public function register(Request $request)
{
$this->validate($request, [
"name" => "required|string|min:4",
"email" => "required|email",
"password" => "required|min:8|max:20|confirmed"
]);

$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);

return response([
'data' => 'Thank you.Your account has been created'
],Response::HTTP_CREATED);
}

/**
* Get a JWT via given credentials.
*
* @return \Illuminate\Http\JsonResponse
*/
public function login()
{
$credentials = request(['email', 'password']);

if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}

return $this->respondWithToken($token);
}

/**
* Get the authenticated User.
*
* @return \Illuminate\Http\JsonResponse
*/
public function me()
{
return response()->json(auth()->user());
}

/**
* Log the user out (Invalidate the token).
*
* @return \Illuminate\Http\JsonResponse
*/
public function logout()
{
auth()->logout();

return response()->json(['message' => 'Successfully logged out']);
}

/**
* Refresh a token.
*
* @return \Illuminate\Http\JsonResponse
*/
public function refresh()
{
return $this->respondWithToken(auth()->refresh());
}

/**
* Get the token array structure.
*
* @param string $token
*
* @return \Illuminate\Http\JsonResponse
*/
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60
]);
}
}
```

### User.php

```php
'datetime',
];

/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}

/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
```

### api.php
```php
Route::group(['prefix' => 'user'], function () {
Route::post('register', 'AuthController@register');
Route::post('login', 'AuthController@login');
Route::group(['middleware' => ['auth:api']], function () {
Route::post('refresh', 'AuthController@refresh');
Route::post('me', 'AuthController@me');
Route::post('logout', 'AuthController@logout');
});
});
```
## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

### Tested on php 7.3 & 7.4 and laravel 6^

## License
[MIT](https://choosealicense.com/licenses/mit/)