Laravelで認証APIを作ってみます。
JWT(JSON Web Token)
- 電子署名により改ざんをチェックできるようになっている
- 改ざんできないWEB Token
- 色々あるようなのでお勉強が必要
もくじ
JWT
マイグレーション
docker-compose exec php-fpm php artisan migrate docker-compose exec php-fpm php artisan make:auth
jwtのインストール
docker-compose exec php-fpm composer require tymon/jwt-auth:dev-develop --prefer-source
/config/app.php
'providers' => [ ・・・ + Tymon\JWTAuth\Providers\LaravelServiceProvider::class, 'aliases' => [ ・・・ + 'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class, + 'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
jwtの設定
docker-compose exec php-fpm php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider" docker-compose exec php-fpm php artisan jwt:secret
/config/auth.php
'defaults' => [ - 'guard' => 'web', + 'guard' => 'api', 'passwords' => 'users', ], ・・・ 'api' => [ - 'driver' => 'token', + 'driver' => 'jwt', 'provider' => 'users', 'hash' => false, ],
/Http/Kernel.php
protected $routeMiddleware = [ ・・・ + 'jwt_auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class, + 'jwt_refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
/app/User.php
<?php namespace App; use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable; + use Tymon\JWTAuth\Contracts\JWTSubject; class User extends Authenticatable implements JWTSubject { use Notifiable; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * The attributes that should be cast to native types. * * @var array */ protected $casts = [ 'email_verified_at' => 'datetime', ]; + public function getJWTIdentifier() + { + return $this->getKey(); + } + public function getJWTCustomClaims() + { + return []; + } }
/Http/Auth/APIRegisterController.php
<?php namespace App\Http\Controllers; use App\Http\Controllers\Controller; use Illuminate\Http\Request; use App\User; use JWTFactory; use JWTAuth; use Validator; use Response; class APIRegisterController extends Controller { public function register(Request $request) { $validator = Validator::make($request->all(), [ 'email' => 'required|string|email|max:255|unique:users', 'name' => 'required', 'password'=> 'required' ]); if ($validator->fails()) { return response()->json($validator->errors()); } User::create([ 'name' => $request->get('name'), 'email' => $request->get('email'), 'password' => bcrypt($request->get('password')), ]); $user = User::first(); $token = JWTAuth::fromUser($user); return Response::json(compact('token')); } }
/Http/Auth/APILoginController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Tymon\JWTAuth\Exceptions\JWTException; class APILoginController extends Controller { public function login() { $credentials = request(['email', 'password']); try { if(!$token = auth('api')->attempt($credentials)) { return response()->json(['error' => 'Unauthorized'], 401); } } catch(JWTException $e){ return response()->json(['error' => 'could_not_create_token'], 500); } return response()->json([ 'token' => $token, 'type' => 'bearer', 'exprires' => auth('api')->factory()->getTTL() * 60, ]); } }
routes/api.php
+ Route::post('user/register', 'APIRegisterController@register'); + Route::post('user/login', 'APILoginController@login');
ユーザ登録
http://localhost/api/user/register?email=yuu@example.net&name=yuu&password=secret
ユーザログイン
http://localhost/api/user/login?email=yuu@example.net&password=secret
おっけ。
登録とログインがAPIでできました(☻-☻)
メール認証について
下記の記事でまとめています😊
@see
JWT
- https://windii.jp/tag/laravel-api-series
- Laravel JWT Authentication Tutorial
- Laravel5でJWT+Twitter OAuthの認証機能をつくる 1/3
- jwt-auth/wiki
- Laravelでjson web token試してみました
メール
- https://readouble.com/laravel/5.3/ja/mail.html
- https://jimfrenette.com/2016/07/laravel-user-registration-with-email-activation/?utm_source=learninglaravel.net
わかりやすい。 - [Laravel]メール認証を使った会員登録
これ良い
Eloquent