JWT almost works.
このコミットが含まれているのは:
コミット
90b7bdc068
|
@ -2,25 +2,78 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Route;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use App\Legislature;
|
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Tymon\JWTAuth\Facades\JWTAuth;
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
||||||
use Tymon\JWTAuth\Exceptions\JWTException;
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
||||||
|
|
||||||
class AuthController extends Controller {
|
class AuthController extends Controller {
|
||||||
|
/**
|
||||||
|
* Create a new AuthController instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
$this->middleware('auth:api', ['except' => ['login']]);
|
||||||
|
}
|
||||||
|
|
||||||
public function authenticate(\Illuminate\Http\Request $request) {
|
/**
|
||||||
$credentials = $request->only('username', 'password'); // grab credentials from the request
|
* Get a JWT via given credentials.
|
||||||
try {
|
*
|
||||||
if (!$token = JWTAuth::attempt($credentials)) { // attempt to verify the credentials and create a token for the user
|
* @return \Illuminate\Http\JsonResponse
|
||||||
return response()->json(['error' => 'invalid_credentials'], 401);
|
*/
|
||||||
}
|
public function login() {
|
||||||
} catch (JWTException $e) {
|
$credentials = request(['username', 'password']);
|
||||||
return response()->json(['error' => 'could_not_create_token'], 500); // something went wrong whilst attempting to encode the token
|
|
||||||
|
if (!$token = auth()->attempt($credentials)) {
|
||||||
|
return response()->json(['error' => 'Unauthorized'], 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json(['token' => "Bearer $token"]);
|
return $this->respondWithToken($token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the authenticated User.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function me() {
|
||||||
|
return response()->json(auth()->user());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log the user out (Invalidate the token).
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function logout() {
|
||||||
|
auth()->logout();
|
||||||
|
|
||||||
|
return response()->json(['message' => 'Successfully logged out']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh a token.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
public function refresh() {
|
||||||
|
return $this->respondWithToken(auth()->refresh());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the token array structure.
|
||||||
|
*
|
||||||
|
* @param string $token
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\JsonResponse
|
||||||
|
*/
|
||||||
|
protected function respondWithToken($token) {
|
||||||
|
return response()->json([
|
||||||
|
'access_token' => $token,
|
||||||
|
'token_type' => 'bearer',
|
||||||
|
'expires_in' => auth()->factory()->getTTL() * 60
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class HomeController extends Controller
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create a new controller instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->middleware('auth');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Show the application dashboard.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\Http\Response
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
return view('home');
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
namespace App;
|
namespace App;
|
||||||
|
|
||||||
|
use Tymon\JWTAuth\Contracts\JWTSubject;
|
||||||
use Illuminate\Notifications\Notifiable;
|
use Illuminate\Notifications\Notifiable;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
use Tymon\JWTAuth\Contracts\JWTSubject;
|
|
||||||
|
|
||||||
class User extends Authenticatable implements JWTSubject {
|
class User extends Authenticatable implements JWTSubject {
|
||||||
use Notifiable;
|
use Notifiable;
|
||||||
|
|
|
@ -14,7 +14,7 @@ return [
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'defaults' => [
|
'defaults' => [
|
||||||
'guard' => 'web',
|
'guard' => 'api',
|
||||||
'passwords' => 'users',
|
'passwords' => 'users',
|
||||||
],
|
],
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
@extends('layouts.app')
|
||||||
|
|
||||||
|
@section('content')
|
||||||
|
<div class="container">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-md-8 col-md-offset-2">
|
||||||
|
<div class="panel panel-default">
|
||||||
|
<div class="panel-heading">Dashboard</div>
|
||||||
|
|
||||||
|
<div class="panel-body">
|
||||||
|
@if (session('status'))
|
||||||
|
<div class="alert alert-success">
|
||||||
|
{{ session('status') }}
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
|
|
||||||
|
You are logged in!
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endsection
|
|
@ -0,0 +1,80 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="{{ app()->getLocale() }}">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<!-- CSRF Token -->
|
||||||
|
<meta name="csrf-token" content="{{ csrf_token() }}">
|
||||||
|
|
||||||
|
<title>{{ config('app.name', 'Laravel') }}</title>
|
||||||
|
|
||||||
|
<!-- Styles -->
|
||||||
|
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app">
|
||||||
|
<nav class="navbar navbar-default navbar-static-top">
|
||||||
|
<div class="container">
|
||||||
|
<div class="navbar-header">
|
||||||
|
|
||||||
|
<!-- Collapsed Hamburger -->
|
||||||
|
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#app-navbar-collapse" aria-expanded="false">
|
||||||
|
<span class="sr-only">Toggle Navigation</span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
<span class="icon-bar"></span>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<!-- Branding Image -->
|
||||||
|
<a class="navbar-brand" href="{{ url('/') }}">
|
||||||
|
{{ config('app.name', 'Laravel') }}
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="collapse navbar-collapse" id="app-navbar-collapse">
|
||||||
|
<!-- Left Side Of Navbar -->
|
||||||
|
<ul class="nav navbar-nav">
|
||||||
|
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<!-- Right Side Of Navbar -->
|
||||||
|
<ul class="nav navbar-nav navbar-right">
|
||||||
|
<!-- Authentication Links -->
|
||||||
|
@guest
|
||||||
|
<li><a href="{{ route('login') }}">Login</a></li>
|
||||||
|
<li><a href="{{ route('register') }}">Register</a></li>
|
||||||
|
@else
|
||||||
|
<li class="dropdown">
|
||||||
|
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true">
|
||||||
|
{{ Auth::user()->name }} <span class="caret"></span>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<ul class="dropdown-menu">
|
||||||
|
<li>
|
||||||
|
<a href="{{ route('logout') }}"
|
||||||
|
onclick="event.preventDefault();
|
||||||
|
document.getElementById('logout-form').submit();">
|
||||||
|
Logout
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
|
||||||
|
{{ csrf_field() }}
|
||||||
|
</form>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</li>
|
||||||
|
@endguest
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</nav>
|
||||||
|
|
||||||
|
@yield('content')
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<!-- Scripts -->
|
||||||
|
<script src="{{ asset('js/app.js') }}"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -13,6 +13,12 @@ use Illuminate\Http\Request;
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::middleware('auth:api')->get('/user', function (Request $request) {
|
Route::group([
|
||||||
return $request->user();
|
'middleware' => 'api',
|
||||||
});
|
'prefix' => 'auth'
|
||||||
|
], function ($router) {
|
||||||
|
Route::post('login', 'AuthController@login');
|
||||||
|
Route::post('logout', 'AuthController@logout');
|
||||||
|
Route::post('refresh', 'AuthController@refresh');
|
||||||
|
Route::post('me', 'AuthController@me');
|
||||||
|
});
|
|
@ -10,15 +10,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Auth
|
// Auth
|
||||||
Route::post('/api/rpc/user/auth/isauth', 'UserController@isAuth');
|
/*Route::group([
|
||||||
//Route::post('/api/rpc/user/auth/login', 'UserController@login');
|
'middleware' => 'api'
|
||||||
Route::post('/api/rpc/user/auth/login', 'AuthController@authenticate');
|
], function ($router) {
|
||||||
Route::post('/api/rpc/user/auth/register', 'UserController@register');
|
Route::post('/api/rpc/user/auth/login', 'AuthController@login');
|
||||||
Route::post('/api/rpc/user/auth/logout', 'UserController@logout');
|
Route::post('/api/rpc/user/auth/logout', 'AuthController@logout');
|
||||||
Route::group(['middleware' => 'jwt'], function () {
|
Route::post('/api/rpc/user/auth/refresh', 'AuthController@refresh');
|
||||||
// Protected routes
|
Route::post('/api/rpc/user/auth/me', 'AuthController@me');
|
||||||
Route::resource('getUserPosts', 'BoardController');
|
});*/
|
||||||
});
|
|
||||||
|
//Route::post('/api/rpc/user/auth/register', 'UserController@register');
|
||||||
|
|
||||||
// Owner
|
// Owner
|
||||||
Route::get('/api/rpc/user/owner/countownersofentry/{id}', 'UserController@countOwnersOfEntry');
|
Route::get('/api/rpc/user/owner/countownersofentry/{id}', 'UserController@countOwnersOfEntry');
|
||||||
|
|
新しいイシューから参照