Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AuthController for Laravel and Vue.js using JWTAuth
<?php
use Illuminate\Http\Request;
Route::group(['prefix' => 'auth'], function () {
Route::post('login', 'VueAuth\AuthController@login');
Route::post('logout', 'VueAuth\AuthController@logout');
Route::group(['middleware' => 'jwt.auth'], function(){
Route::get('user', 'VueAuth\AuthController@user');
});
Route::group(['middleware' => 'jwt.refresh'], function(){
Route::get('refresh', 'VueAuth\AuthController@refresh');
});
});
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'api' => [
'driver' => 'jwt',
'provider' => 'users'
],
'web' => [
'driver' => 'session',
'provider' => 'users'
]
],
'providers' => [
'adldap' => [
'driver' => 'adldap',
'model' => App\User::class,
],
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],
];
<?php
namespace App\Http\Controllers\VueAuth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
class AuthController extends Controller
{
/**
* Create a new AuthController instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth:api', ['except' => ['login']]);
}
/**
* Login using the JWTAuth and create token
* @param Request $request { username, password }
* @return Response json with Authorization header if successful
*/
public function login(Request $request)
{
$credentials = $request->only('username', 'password');
$validator = \Validator::make($credentials, [
'username' => 'required',
'password' => 'required'
]);
if ($validator->fails()) {
return response()
->json([
'code' => 1,
'message' => 'Validation failed.',
'errors' => $validator->errors()
], 422);
}
$token = \JWTAuth::attempt($credentials);
if ($token) {
return response([
'status' => 'success'
])->header('Authorization', $token);
} else {
return response()->json(['code' => 2, 'message' => 'Invalid credentials.'], 401);
}
}
/**
* Return the user model with associated roles
* @param Request $request (Not used)
* @return json User Model
*/
public function user(Request $request)
{
$user = \App\User::with('roles')
->findOrFail(\Auth::user()->id);
return [ 'status' => 'success', 'data' => $user ];
}
public function refresh()
{
return response([
'status' => 'success'
]);
}
public function logout()
{
\JWTAuth::invalidate();
return response([
'status' => 'success',
'msg' => 'Logged out Successfully.'
], 200);
}
}
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;
use Adldap\Laravel\Traits\AdldapUserModelTrait;
// Vue auth
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use Notifiable;
use HasRoles;
use AdldapUserModelTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'username', 'password', 'dn'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'api_token'
];
public function id()
{
return $this->id;
}
public function dn()
{
return $this->dn;
}
/**
* 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 [];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment