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

Commit 805c9a4

Browse files
authored
Merge pull request #11 from cleaniquecoders/develop
Develop
2 parents 6f4df63 + d7f0492 commit 805c9a4

File tree

26 files changed

+618
-557
lines changed

26 files changed

+618
-557
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,5 @@ DOCUMENT_SEQUENCE_LENGTH=6
3838

3939
LAYOUT_ADMIN=default
4040
LAYOUT_PUBLIC=default
41+
42+
JWT_SECRET=
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Str;
7+
8+
class MakeJwtTokenCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'make:jwt';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Create a new JWT Token';
23+
24+
/**
25+
* Create a new command instance.
26+
*/
27+
public function __construct()
28+
{
29+
parent::__construct();
30+
}
31+
32+
/**
33+
* Execute the console command.
34+
*
35+
* @return mixed
36+
*/
37+
public function handle()
38+
{
39+
$key = Str::random(32);
40+
41+
file_put_contents($this->laravel->environmentFilePath(), preg_replace(
42+
$this->keyReplacementPattern(),
43+
'JWT_SECRET=' . $key,
44+
file_get_contents($this->laravel->environmentFilePath())
45+
));
46+
47+
$this->info("jwt-auth secret [$key] set successfully.");
48+
}
49+
50+
/**
51+
* Get a regex pattern that will match env JWT_SECRET with any random key.
52+
*
53+
* @return string
54+
*/
55+
protected function keyReplacementPattern()
56+
{
57+
$escaped = preg_quote('=' . $this->laravel['config']['jwt.secret'], '/');
58+
59+
return "/^JWT_SECRET{$escaped}/m";
60+
}
61+
}

app/Console/Commands/ReloadAllCommand.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ public function handle()
3939
$this->call('reload:cache');
4040
$this->call('reload:db');
4141
$this->call('storage:link');
42-
$this->call('passport:install', [
43-
'--force' => true,
44-
]);
42+
$this->call('make:jwt');
4543

4644
if ($this->option('dev')) {
4745
$this->call('db:seed', [

app/Console/Kernel.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ class Kernel extends ConsoleKernel
1313
* @var array
1414
*/
1515
protected $commands = [
16-
\App\Console\Commands\ReloadAllCommand::class,
17-
\App\Console\Commands\ReloadCacheCommand::class,
18-
\App\Console\Commands\ReloadDbCommand::class,
16+
Commands\MakeJwtTokenCommand::class,
17+
Commands\ReloadAllCommand::class,
18+
Commands\ReloadCacheCommand::class,
19+
Commands\ReloadDbCommand::class,
1920
];
2021

2122
/**
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Password;
9+
10+
class ForgotPasswordController extends Controller
11+
{
12+
use SendsPasswordResetEmails;
13+
14+
/**
15+
* Create a new controller instance.
16+
*/
17+
public function __construct()
18+
{
19+
$this->middleware('guest');
20+
}
21+
22+
public function __invoke(Request $request)
23+
{
24+
$this->validateEmail($request);
25+
26+
// We will send the password reset link to this user. Once we have attempted
27+
// to send the link, we will examine the response then see the message we
28+
// need to show to the user. Finally, we'll send out a proper response.
29+
$response = $this->broker()->sendResetLink(
30+
$request->only('email')
31+
);
32+
33+
return Password::RESET_LINK_SENT == $response
34+
? response()->api([], 'Reset link sent to your email.')
35+
: response()->api([], 'Unable to send reset link');
36+
}
37+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
use JWTAuth;
8+
use Tymon\JWTAuth\Exceptions\JWTException;
9+
10+
class LoginController extends Controller
11+
{
12+
public function __invoke(Request $request)
13+
{
14+
$credentials = $request->only('email', 'password');
15+
16+
try {
17+
if (! $token = JWTAuth::attempt($credentials)) {
18+
return response()->api([], 'Invalid Credentials.', false, 401);
19+
}
20+
} catch (JWTException $e) {
21+
return response()->api([], 'Could not create token.', false, 500);
22+
}
23+
24+
return response()->api($token);
25+
}
26+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
use JWTAuth;
8+
9+
class LogoutController extends Controller
10+
{
11+
public function __invoke(Request $request)
12+
{
13+
JWTAuth::invalidate(JWTAuth::getToken());
14+
15+
return response()->api([], 'You have sucessfully logout.');
16+
}
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\Auth;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\User;
7+
use Illuminate\Auth\Events\Registered;
8+
use Illuminate\Http\Request;
9+
use JWTAuth;
10+
11+
class RegisterController extends Controller
12+
{
13+
public function __invoke(Request $request)
14+
{
15+
$this->validate($request, [
16+
'name' => 'required|string|max:255',
17+
'email' => 'required|string|email|max:255|unique:users',
18+
'password' => 'required|string|min:6|confirmed',
19+
]);
20+
21+
$user = User::create([
22+
'name' => $request->get('name'),
23+
'email' => $request->get('email'),
24+
'password' => bcrypt($request->get('password')),
25+
]);
26+
27+
event(new Registered($user));
28+
29+
$token = JWTAuth::fromUser($user);
30+
31+
return response()->api($token, 'Registration successful.', true, 201);
32+
}
33+
}

app/Http/Controllers/Api/Manage/UserController.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Models\User;
7-
use Illuminate\Http\Request;
87
use Illuminate\Auth\Events\Registered;
8+
use Illuminate\Http\Request;
99

1010
class UserController extends Controller
1111
{
@@ -39,7 +39,8 @@ public function store(Request $request)
3939
'password' => bcrypt($data['password']),
4040
]);
4141
event(new Registered($user));
42-
$user->syncRoles([$request->role]);
42+
$user->syncRoles($request->roles);
43+
4344
return response()->api([], __('User successfully stored.'), true, 201);
4445
}
4546

@@ -53,12 +54,12 @@ public function store(Request $request)
5354
public function show($id)
5455
{
5556
$user = User::details()->findByHashSlug($id);
56-
57+
5758
/**
5859
* @todo should have a transformer to do this.
5960
*/
60-
$user = collect($user->only('name', 'email', 'roles_to_string', 'roles'));
61-
$roles = $user->get('roles')->mapWithKeys(function($role){
61+
$user = collect($user->only('name', 'email', 'roles_to_string', 'roles'));
62+
$roles = $user->get('roles')->mapWithKeys(function ($role) {
6263
return [$role->id => $role->name];
6364
});
6465
$user->put('roles', $roles);
@@ -77,12 +78,12 @@ public function show($id)
7778
public function update(Request $request, $id)
7879
{
7980
$this->validate($request, [
80-
'name' => 'required|string|max:255',
81+
'name' => 'required|string|max:255',
8182
]);
8283

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

85-
if(!empty($request->input('password'))) {
86+
if (! empty($request->input('password'))) {
8687
$this->validate($request, [
8788
'password' => 'required|string|min:6|confirmed',
8889
]);
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api\User;
4+
5+
use App\Http\Controllers\Controller;
6+
use Illuminate\Http\Request;
7+
8+
class ProfileController extends Controller
9+
{
10+
/**
11+
* Display a listing of the resource.
12+
*
13+
* @return \Illuminate\Http\Response
14+
*/
15+
public function index()
16+
{
17+
}
18+
19+
/**
20+
* Store a newly created resource in storage.
21+
*
22+
* @param \Illuminate\Http\Request $request
23+
*
24+
* @return \Illuminate\Http\Response
25+
*/
26+
public function store(Request $request)
27+
{
28+
}
29+
30+
/**
31+
* Display the specified resource.
32+
*
33+
* @return \Illuminate\Http\Response
34+
*/
35+
public function show()
36+
{
37+
return response()->api(auth()->user()->only('name', 'email', 'hashslug'));
38+
}
39+
40+
/**
41+
* Update the specified resource in storage.
42+
*
43+
* @param \Illuminate\Http\Request $request
44+
* @param int $id
45+
*
46+
* @return \Illuminate\Http\Response
47+
*/
48+
public function update(Request $request, $id)
49+
{
50+
}
51+
52+
/**
53+
* Remove the specified resource from storage.
54+
*
55+
* @param int $id
56+
*
57+
* @return \Illuminate\Http\Response
58+
*/
59+
public function destroy($id)
60+
{
61+
}
62+
}

0 commit comments

Comments
 (0)