Skip to content

Commit 927ac55

Browse files
committed
add authentication
1 parent 71fed62 commit 927ac55

12 files changed

Lines changed: 518 additions & 237 deletions

File tree

app/Data/Models/User.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Data\Models;
4+
5+
use Illuminate\Notifications\Notifiable;
6+
use Illuminate\Foundation\Auth\User as Authenticatable;
7+
8+
class User extends Authenticatable
9+
{
10+
use Notifiable;
11+
12+
/**
13+
* The attributes that are mass assignable.
14+
*
15+
* @var array
16+
*/
17+
protected $fillable = [
18+
'name', 'email', 'password',
19+
];
20+
21+
/**
22+
* The attributes that should be hidden for arrays.
23+
*
24+
* @var array
25+
*/
26+
protected $hidden = [
27+
'password', 'remember_token',
28+
];
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Exceptions\Auth;
4+
5+
6+
use Symfony\Component\HttpKernel\Exception\HttpException;
7+
8+
class InvalidCredentialsException extends HttpException
9+
{
10+
public function __construct($message = 'Invalid credentials provided', \Exception $previous = null, array $headers = array(), $code = 0)
11+
{
12+
parent::__construct(401, $message, $previous, $headers, $code);
13+
}
14+
15+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Data\Models\User;
6+
use App\Exceptions\Auth\InvalidCredentialsException;
7+
use Illuminate\Http\Request;
8+
use Tymon\JWTAuth\JWTAuth;
9+
10+
class AuthController extends Controller
11+
{
12+
public function register(Request $request)
13+
{
14+
$this->validate($request, [
15+
'name' => 'required|max:255',
16+
'email' => 'required|email|max:255|unique:users',
17+
'password' => 'required|min:6',
18+
]);
19+
20+
$user = User::create(
21+
[
22+
'email' => $request->email,
23+
'name' => $request->name,
24+
'password' => bcrypt($request->password)
25+
]);
26+
return $user;
27+
}
28+
29+
public function login(Request $request)
30+
{
31+
//Retrieve user based on the credentials provided
32+
$user = $this->authenticateUser($request);
33+
//Generate a token for the user and return it
34+
$token = app(JWTAuth::class)->fromUser($user);
35+
return ['token' => $token];
36+
}
37+
38+
39+
private function authenticateUser($request)
40+
{
41+
//Validate request data
42+
$this->validate($request, [
43+
'email' => 'required|email',
44+
'password' => 'required',
45+
]);
46+
47+
//Check if a user with this credentials exist, throw error if not
48+
if (!\Auth::guard()->attempt($request->only('email', 'password'))) {
49+
throw new InvalidCredentialsException('Invalid email/password combination');
50+
}
51+
52+
//Retrieve the user details and return
53+
return User::where('email', $request->email)->first();
54+
}
55+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Requests;
6+
use App\Http\Controllers\Controller as BaseController;
7+
use Dingo\Api\Exception\ResourceException;
8+
use Illuminate\Http\Request;
9+
10+
class Controller extends BaseController
11+
{
12+
protected function throwValidationException(Request $request, $validator)
13+
{
14+
throw new ResourceException('Validation failed', $validator->getMessageBag());
15+
}
16+
}

app/Providers/RouteServiceProvider.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ protected function mapWebRoutes()
6868
*/
6969
protected function mapApiRoutes()
7070
{
71-
Route::group([
72-
'middleware' => 'api',
73-
'namespace' => $this->namespace,
74-
'prefix' => 'api',
75-
], function ($router) {
76-
require base_path('routes/api.php');
71+
$api = app('Dingo\Api\Routing\Router');
72+
73+
$api->version('v1', function ($api) {
74+
$api->group(['namespace' => $this->namespace . '\Api'], function ($api) {
75+
require base_path('routes/api.php');
76+
});
7777
});
7878
}
7979
}

artisan

100755100644
File mode changed.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": ">=5.6.4",
99
"laravel/framework": "5.3.*",
1010
"dingo/api": "1.0.x@dev",
11-
"barryvdh/laravel-cors": "^0.8.2"
11+
"barryvdh/laravel-cors": "^0.8.2",
12+
"tymon/jwt-auth": "^0.5.9"
1213
},
1314
"require-dev": {
1415
"fzaninotto/faker": "~1.4",

0 commit comments

Comments
 (0)