Skip to content
This repository has been archived by the owner on Nov 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #11 from cleaniquecoders/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nasrulhazim authored Apr 27, 2018
2 parents 6f4df63 + d7f0492 commit 805c9a4
Show file tree
Hide file tree
Showing 26 changed files with 618 additions and 557 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ DOCUMENT_SEQUENCE_LENGTH=6

LAYOUT_ADMIN=default
LAYOUT_PUBLIC=default

JWT_SECRET=
61 changes: 61 additions & 0 deletions app/Console/Commands/MakeJwtTokenCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;

class MakeJwtTokenCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'make:jwt';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new JWT Token';

/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$key = Str::random(32);

file_put_contents($this->laravel->environmentFilePath(), preg_replace(
$this->keyReplacementPattern(),
'JWT_SECRET=' . $key,
file_get_contents($this->laravel->environmentFilePath())
));

$this->info("jwt-auth secret [$key] set successfully.");
}

/**
* Get a regex pattern that will match env JWT_SECRET with any random key.
*
* @return string
*/
protected function keyReplacementPattern()
{
$escaped = preg_quote('=' . $this->laravel['config']['jwt.secret'], '/');

return "/^JWT_SECRET{$escaped}/m";
}
}
4 changes: 1 addition & 3 deletions app/Console/Commands/ReloadAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ public function handle()
$this->call('reload:cache');
$this->call('reload:db');
$this->call('storage:link');
$this->call('passport:install', [
'--force' => true,
]);
$this->call('make:jwt');

if ($this->option('dev')) {
$this->call('db:seed', [
Expand Down
7 changes: 4 additions & 3 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
\App\Console\Commands\ReloadAllCommand::class,
\App\Console\Commands\ReloadCacheCommand::class,
\App\Console\Commands\ReloadDbCommand::class,
Commands\MakeJwtTokenCommand::class,
Commands\ReloadAllCommand::class,
Commands\ReloadCacheCommand::class,
Commands\ReloadDbCommand::class,
];

/**
Expand Down
37 changes: 37 additions & 0 deletions app/Http/Controllers/Api/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;

class ForgotPasswordController extends Controller
{
use SendsPasswordResetEmails;

/**
* Create a new controller instance.
*/
public function __construct()
{
$this->middleware('guest');
}

public function __invoke(Request $request)
{
$this->validateEmail($request);

// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('email')
);

return Password::RESET_LINK_SENT == $response
? response()->api([], 'Reset link sent to your email.')
: response()->api([], 'Unable to send reset link');
}
}
26 changes: 26 additions & 0 deletions app/Http/Controllers/Api/Auth/LoginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class LoginController extends Controller
{
public function __invoke(Request $request)
{
$credentials = $request->only('email', 'password');

try {
if (! $token = JWTAuth::attempt($credentials)) {
return response()->api([], 'Invalid Credentials.', false, 401);
}
} catch (JWTException $e) {
return response()->api([], 'Could not create token.', false, 500);
}

return response()->api($token);
}
}
17 changes: 17 additions & 0 deletions app/Http/Controllers/Api/Auth/LogoutController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use JWTAuth;

class LogoutController extends Controller
{
public function __invoke(Request $request)
{
JWTAuth::invalidate(JWTAuth::getToken());

return response()->api([], 'You have sucessfully logout.');
}
}
33 changes: 33 additions & 0 deletions app/Http/Controllers/Api/Auth/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Controllers\Api\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use JWTAuth;

class RegisterController extends Controller
{
public function __invoke(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);

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

event(new Registered($user));

$token = JWTAuth::fromUser($user);

return response()->api($token, 'Registration successful.', true, 201);
}
}
15 changes: 8 additions & 7 deletions app/Http/Controllers/Api/Manage/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;

class UserController extends Controller
{
Expand Down Expand Up @@ -39,7 +39,8 @@ public function store(Request $request)
'password' => bcrypt($data['password']),
]);
event(new Registered($user));
$user->syncRoles([$request->role]);
$user->syncRoles($request->roles);

return response()->api([], __('User successfully stored.'), true, 201);
}

Expand All @@ -53,12 +54,12 @@ public function store(Request $request)
public function show($id)
{
$user = User::details()->findByHashSlug($id);

/**
* @todo should have a transformer to do this.
*/
$user = collect($user->only('name', 'email', 'roles_to_string', 'roles'));
$roles = $user->get('roles')->mapWithKeys(function($role){
$user = collect($user->only('name', 'email', 'roles_to_string', 'roles'));
$roles = $user->get('roles')->mapWithKeys(function ($role) {
return [$role->id => $role->name];
});
$user->put('roles', $roles);
Expand All @@ -77,12 +78,12 @@ public function show($id)
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required|string|max:255',
'name' => 'required|string|max:255',
]);

$fields = $request->only('name');

if(!empty($request->input('password'))) {
if (! empty($request->input('password'))) {
$this->validate($request, [
'password' => 'required|string|min:6|confirmed',
]);
Expand Down
62 changes: 62 additions & 0 deletions app/Http/Controllers/Api/User/ProfileController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Http\Controllers\Api\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
}

/**
* Display the specified resource.
*
* @return \Illuminate\Http\Response
*/
public function show()
{
return response()->api(auth()->user()->only('name', 'email', 'hashslug'));
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
}

/**
* Remove the specified resource from storage.
*
* @param int $id
*
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
}
}
21 changes: 11 additions & 10 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class Kernel extends HttpKernel
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
\Spatie\Referer\CaptureReferer::class,
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],

'api' => [
Expand All @@ -59,14 +58,16 @@ class Kernel extends HttpKernel
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
'minify' => \App\Http\Middleware\MinifyHtml::class,
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
'minify' => \App\Http\Middleware\MinifyHtml::class,
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
];
}
Loading

0 comments on commit 805c9a4

Please sign in to comment.