Skip to content

Commit 05255e3

Browse files
committed
api authentication
1 parent 2c4bec0 commit 05255e3

File tree

85 files changed

+60907
-3239
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+60907
-3239
lines changed

.env.example

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
APP_NAME=Laravel
12
APP_ENV=local
23
APP_KEY=
34
APP_DEBUG=true
@@ -21,12 +22,12 @@ REDIS_PASSWORD=null
2122
REDIS_PORT=6379
2223

2324
MAIL_DRIVER=smtp
24-
MAIL_HOST=mailtrap.io
25+
MAIL_HOST=smtp.mailtrap.io
2526
MAIL_PORT=2525
2627
MAIL_USERNAME=null
2728
MAIL_PASSWORD=null
2829
MAIL_ENCRYPTION=null
2930

3031
PUSHER_APP_ID=
31-
PUSHER_KEY=
32-
PUSHER_SECRET=
32+
PUSHER_APP_KEY=
33+
PUSHER_APP_SECRET=

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
* text=auto
22
*.css linguist-vendored
33
*.scss linguist-vendored
4+
*.js linguist-vendored
5+
CHANGELOG.md export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
/node_modules
2+
/public/hot
23
/public/storage
34
/storage/*.key
45
/vendor
56
/.idea
7+
/.vagrant
68
Homestead.json
79
Homestead.yaml
10+
npm-debug.log
11+
yarn-error.log
812
.env

app/Console/Kernel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ protected function schedule(Schedule $schedule)
2929
}
3030

3131
/**
32-
* Register the Closure based commands for the application.
32+
* Register the commands for the application.
3333
*
3434
* @return void
3535
*/
3636
protected function commands()
3737
{
38+
$this->load(__DIR__.'/Commands');
39+
3840
require base_path('routes/console.php');
3941
}
4042
}

app/Exceptions/Handler.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,27 @@
33
namespace App\Exceptions;
44

55
use Exception;
6-
use Illuminate\Auth\AuthenticationException;
76
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
87

98
class Handler extends ExceptionHandler
109
{
1110
/**
12-
* A list of the exception types that should not be reported.
11+
* A list of the exception types that are not reported.
1312
*
1413
* @var array
1514
*/
1615
protected $dontReport = [
17-
\Illuminate\Auth\AuthenticationException::class,
18-
\Illuminate\Auth\Access\AuthorizationException::class,
19-
\Symfony\Component\HttpKernel\Exception\HttpException::class,
20-
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
21-
\Illuminate\Session\TokenMismatchException::class,
22-
\Illuminate\Validation\ValidationException::class,
16+
//
17+
];
18+
19+
/**
20+
* A list of the inputs that are never flashed for validation exceptions.
21+
*
22+
* @var array
23+
*/
24+
protected $dontFlash = [
25+
'password',
26+
'password_confirmation',
2327
];
2428

2529
/**
@@ -46,20 +50,4 @@ public function render($request, Exception $exception)
4650
{
4751
return parent::render($request, $exception);
4852
}
49-
50-
/**
51-
* Convert an authentication exception into an unauthenticated response.
52-
*
53-
* @param \Illuminate\Http\Request $request
54-
* @param \Illuminate\Auth\AuthenticationException $exception
55-
* @return \Illuminate\Http\Response
56-
*/
57-
protected function unauthenticated($request, AuthenticationException $exception)
58-
{
59-
if ($request->expectsJson()) {
60-
return response()->json(['error' => 'Unauthenticated.'], 401);
61-
}
62-
63-
return redirect()->guest('login');
64-
}
6553
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\User;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Facades\DB;
8+
use Illuminate\Validation\ValidationException;
9+
10+
class ApiController extends Controller
11+
{
12+
/**
13+
* Register a user and create a valid token
14+
* @param Request $request
15+
* @return \Illuminate\Http\JsonResponse
16+
*/
17+
public function register(Request $request)
18+
{
19+
$request->validate([
20+
'name' => 'required|string|max:255',
21+
'email' => 'required|string|email|max:255|unique:users',
22+
'password' => 'required|string|min:6',
23+
]);
24+
$user = User::create([
25+
'name' => $request->input('name'),
26+
'email' => $request->input('email'),
27+
'password' => bcrypt($request->input('password'))
28+
]);
29+
$token = $user->createToken('login')->accessToken;
30+
return response()->json([
31+
'user' => $user,
32+
'token' => $token
33+
], 200);
34+
}
35+
36+
/**
37+
* Sign in user and create a valid token
38+
* @param Request $request
39+
* @return \Illuminate\Http\JsonResponse
40+
*/
41+
public function userLogin(Request $request)
42+
{
43+
44+
if (auth()->attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
45+
$user = auth()->user();
46+
$token = $user->createToken('login')->accessToken;
47+
return response()->json([
48+
'user' => $user,
49+
'token' => $token
50+
], 200);
51+
}
52+
53+
return $this->sendFailedLoginResponse($request);
54+
}
55+
56+
/**
57+
* Logout a user and delete his oauth token
58+
* @param Request $request
59+
* @return \Illuminate\Http\JsonResponse
60+
*/
61+
public function logout(Request $request)
62+
{
63+
DB::table('oauth_access_tokens')->where('user_id', $request->get('id'))->delete();
64+
65+
return response()->json(['message' => 'You are Logged out.'], 200);
66+
}
67+
68+
/**
69+
* Get the failed login response instance.
70+
*
71+
* @param \Illuminate\Http\Request $request
72+
* @return \Symfony\Component\HttpFoundation\Response
73+
*
74+
* @throws ValidationException
75+
*/
76+
protected function sendFailedLoginResponse(Request $request)
77+
{
78+
throw ValidationException::withMessages([
79+
$this->username() => [trans('auth.failed')],
80+
]);
81+
}
82+
83+
/**
84+
* Get the login username to be used by the controller.
85+
*
86+
* @return string
87+
*/
88+
public function username()
89+
{
90+
return 'email';
91+
}
92+
93+
}

app/Http/Controllers/Auth/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,6 @@ class LoginController extends Controller
3434
*/
3535
public function __construct()
3636
{
37-
$this->middleware('guest', ['except' => 'logout']);
37+
$this->middleware('guest')->except('logout');
3838
}
3939
}

app/Http/Controllers/Auth/RegisterController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ public function __construct()
4848
protected function validator(array $data)
4949
{
5050
return Validator::make($data, [
51-
'name' => 'required|max:255',
52-
'email' => 'required|email|max:255|unique:users',
53-
'password' => 'required|min:6|confirmed',
51+
'name' => 'required|string|max:255',
52+
'email' => 'required|string|email|max:255|unique:users',
53+
'password' => 'required|string|min:6|confirmed',
5454
]);
5555
}
5656

5757
/**
5858
* Create a new user instance after a valid registration.
5959
*
6060
* @param array $data
61-
* @return User
61+
* @return \App\User
6262
*/
6363
protected function create(array $data)
6464
{

app/Http/Controllers/BookController.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ class BookController extends Controller
1414
*/
1515
public function index()
1616
{
17-
$book = Book::all();
17+
$books = Book::all();
1818

19-
return response()->json($book);
19+
return response()->json($books);
2020
}
2121

2222
/**
@@ -32,38 +32,38 @@ public function create()
3232
/**
3333
* Store a newly created resource in storage.
3434
*
35-
* @param \Illuminate\Http\Request $request
35+
* @param \Illuminate\Http\Request $request
3636
* @return \Illuminate\Http\Response
3737
*/
3838
public function store(Request $request)
3939
{
40-
$this->validate($request, [
41-
'author'=>'required',
42-
'description'=>'required'
40+
$book = $request->validate([
41+
'author' => 'required',
42+
'description' => 'required'
4343
]);
4444

45-
$book = Book::create($request->all());
45+
$books = Book::create($book);
4646

47-
return response()->json($book);
47+
return response()->json($books);
4848
}
4949

5050
/**
5151
* Display the specified resource.
5252
*
53-
* @param int $id
53+
* @param int $id
5454
* @return \Illuminate\Http\Response
5555
*/
5656
public function show($id)
5757
{
58-
$book = Book::find($id);
58+
$book = Book::findOrFail($id);
5959

6060
return response()->json($book);
6161
}
6262

6363
/**
6464
* Show the form for editing the specified resource.
6565
*
66-
* @param int $id
66+
* @param int $id
6767
* @return \Illuminate\Http\Response
6868
*/
6969
public function edit($id)
@@ -74,28 +74,32 @@ public function edit($id)
7474
/**
7575
* Update the specified resource in storage.
7676
*
77-
* @param \Illuminate\Http\Request $request
78-
* @param int $id
77+
* @param \Illuminate\Http\Request $request
78+
* @param int $id
7979
* @return \Illuminate\Http\Response
8080
*/
8181
public function update(Request $request, $id)
8282
{
83-
$book = new Book;
84-
$book->author = $request->input('author');
85-
$book->description = $request->input('description');
86-
$book->save();
83+
$data = $request->validate([
84+
'author' => 'required',
85+
'description' => 'required'
86+
]);
87+
88+
$book = Book::findOrFail($id);
89+
$book->update($data);
90+
8791
return response()->json($book);
8892
}
8993

9094
/**
9195
* Remove the specified resource from storage.
9296
*
93-
* @param int $id
97+
* @param int $id
9498
* @return \Illuminate\Http\Response
9599
*/
96100
public function destroy($id)
97101
{
98-
$book = Book::find($id);
102+
$book = Book::findOrFail($id);
99103
$book->delete();
100104

101105
return response()->json($book);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
7+
class HomeController extends Controller
8+
{
9+
/**
10+
* Create a new controller instance.
11+
*
12+
* @return void
13+
*/
14+
public function __construct()
15+
{
16+
$this->middleware('auth');
17+
}
18+
19+
/**
20+
* Show the application dashboard.
21+
*
22+
* @return \Illuminate\Http\Response
23+
*/
24+
public function index()
25+
{
26+
return view('home');
27+
}
28+
}

0 commit comments

Comments
 (0)