Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
C
CO-OP Search
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kittisak Maneewong
CO-OP Search
Commits
9aab7476
Commit
9aab7476
authored
Apr 07, 2019
by
Kittisak Maneewong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add AuthController
parent
7183cf80
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
398 additions
and
4 deletions
+398
-4
AuthController.php
app/Http/Controllers/AuthController.php
+73
-0
User.php
app/User.php
+12
-1
app.php
config/app.php
+3
-0
jwt.php
config/jwt.php
+304
-0
api.php
routes/api.php
+6
-3
No files found.
app/Http/Controllers/AuthController.php
0 → 100644
View file @
9aab7476
<?php
namespace
App\Http\Controllers
;
use
App\User
;
use
Illuminate\Http\Request
;
use
Illuminate\Support\Facades\Hash
;
use
Illuminate\Support\Facades\Validator
;
use
Tymon\JWTAuth\Facades\JWTAuth
;
use
Tymon\JWTAuth\Facades\JWTFactory
;
use
Tymon\JWTAuth\Exceptions\JWTException
;
use
Tymon\JWTAuth\Contracts\JWTSubject
;
use
Tymon\JWTAuth\PayloadFactory
;
use
Tymon\JWTAuth\JWTManager
as
JWT
;
class
AuthController
extends
Controller
{
public
function
register
(
Request
$request
)
{
$validator
=
Validator
::
make
(
$request
->
json
()
->
all
()
,
[
'name'
=>
'required|string|max:255'
,
'email'
=>
'required|string|email|max:255|unique:users'
,
'password'
=>
'required|string|min:8|confirmed'
,
]);
if
(
$validator
->
fails
())
{
return
response
()
->
json
(
$validator
->
errors
()
->
toJson
(),
400
);
}
$user
=
User
::
create
([
'name'
=>
$request
->
json
()
->
get
(
'name'
),
'email'
=>
$request
->
json
()
->
get
(
'email'
),
'password'
=>
Hash
::
make
(
$request
->
json
()
->
get
(
'password'
)),
]);
$token
=
JWTAuth
::
fromUser
(
$user
);
return
response
()
->
json
(
compact
(
'user'
,
'token'
),
201
);
}
public
function
login
(
Request
$request
)
{
$credentials
=
$request
->
json
()
->
all
();
try
{
if
(
!
$token
=
JWTAuth
::
attempt
(
$credentials
))
{
return
response
()
->
json
([
'error'
=>
'invalid_credentials'
],
400
);
}
}
catch
(
JWTException
$e
)
{
return
response
()
->
json
([
'error'
=>
'could_not_create_token'
,
500
]);
}
return
response
()
->
json
(
compact
(
'token'
));
}
public
function
getAuthenticatedUser
()
{
try
{
if
(
!
user
==
JWTAuth
::
parseToken
()
->
authenticate
())
{
return
response
()
->
json
([
'user_not_found'
],
404
);
}
}
catch
(
Tymon\JWTAuth\Exceptions\TokenExpiredException
$e
)
{
return
response
()
->
json
([
'token_expired'
],
$e
->
getStatusCode
());
}
catch
(
Tymon\JWTAuth\Exceptions\TokenInvalidException
$e
)
{
return
response
()
->
json
([
'token_invalid'
,
$e
->
getStatusCode
()]);
}
catch
(
Tymon\JWTAuth\Exceptions\JWTException
$e
)
{
return
response
()
->
json
([
'token_absent'
],
$e
->
getStatusCode
());
}
return
response
()
->
json
(
compact
(
'user'
));
}
}
app/User.php
View file @
9aab7476
...
...
@@ -5,8 +5,9 @@ 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
class
User
extends
Authenticatable
implements
JWTSubject
{
use
Notifiable
;
...
...
@@ -36,4 +37,14 @@ class User extends Authenticatable
protected
$casts
=
[
'email_verified_at'
=>
'datetime'
,
];
public
function
getJWTIdentifier
()
{
return
$this
->
getKey
();
}
public
function
getJWTCustomClaims
()
{
return
[];
}
}
config/app.php
View file @
9aab7476
...
...
@@ -174,6 +174,7 @@ return [
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider
::
class
,
App\Providers\RouteServiceProvider
::
class
,
Tymon\JWTAuth\Providers\LaravelServiceProvider
::
class
,
],
...
...
@@ -225,6 +226,8 @@ return [
'URL'
=>
Illuminate\Support\Facades\URL
::
class
,
'Validator'
=>
Illuminate\Support\Facades\Validator
::
class
,
'View'
=>
Illuminate\Support\Facades\View
::
class
,
'JWTAuth'
=>
Tymon\JWTAuth\Facades\JWTAuth
::
class
,
'JWTFactory'
=>
Tymon\JWTAuth\Facades\JWTFactory
::
class
,
],
...
...
config/jwt.php
0 → 100644
View file @
9aab7476
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
return
[
/*
|--------------------------------------------------------------------------
| JWT Authentication Secret
|--------------------------------------------------------------------------
|
| Don't forget to set this in your .env file, as it will be used to sign
| your tokens. A helper command is provided for this:
| `php artisan jwt:secret`
|
| Note: This will be used for Symmetric algorithms only (HMAC),
| since RSA and ECDSA use a private/public key combo (See below).
|
*/
'secret'
=>
env
(
'JWT_SECRET'
),
/*
|--------------------------------------------------------------------------
| JWT Authentication Keys
|--------------------------------------------------------------------------
|
| The algorithm you are using, will determine whether your tokens are
| signed with a random string (defined in `JWT_SECRET`) or using the
| following public & private keys.
|
| Symmetric Algorithms:
| HS256, HS384 & HS512 will use `JWT_SECRET`.
|
| Asymmetric Algorithms:
| RS256, RS384 & RS512 / ES256, ES384 & ES512 will use the keys below.
|
*/
'keys'
=>
[
/*
|--------------------------------------------------------------------------
| Public Key
|--------------------------------------------------------------------------
|
| A path or resource to your public key.
|
| E.g. 'file://path/to/public/key'
|
*/
'public'
=>
env
(
'JWT_PUBLIC_KEY'
),
/*
|--------------------------------------------------------------------------
| Private Key
|--------------------------------------------------------------------------
|
| A path or resource to your private key.
|
| E.g. 'file://path/to/private/key'
|
*/
'private'
=>
env
(
'JWT_PRIVATE_KEY'
),
/*
|--------------------------------------------------------------------------
| Passphrase
|--------------------------------------------------------------------------
|
| The passphrase for your private key. Can be null if none set.
|
*/
'passphrase'
=>
env
(
'JWT_PASSPHRASE'
),
],
/*
|--------------------------------------------------------------------------
| JWT time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token will be valid for.
| Defaults to 1 hour.
|
| You can also set this to null, to yield a never expiring token.
| Some people may want this behaviour for e.g. a mobile app.
| This is not particularly recommended, so make sure you have appropriate
| systems in place to revoke the token if necessary.
| Notice: If you set this to null you should remove 'exp' element from 'required_claims' list.
|
*/
'ttl'
=>
env
(
'JWT_TTL'
,
60
),
/*
|--------------------------------------------------------------------------
| Refresh time to live
|--------------------------------------------------------------------------
|
| Specify the length of time (in minutes) that the token can be refreshed
| within. I.E. The user can refresh their token within a 2 week window of
| the original token being created until they must re-authenticate.
| Defaults to 2 weeks.
|
| You can also set this to null, to yield an infinite refresh time.
| Some may want this instead of never expiring tokens for e.g. a mobile app.
| This is not particularly recommended, so make sure you have appropriate
| systems in place to revoke the token if necessary.
|
*/
'refresh_ttl'
=>
env
(
'JWT_REFRESH_TTL'
,
20160
),
/*
|--------------------------------------------------------------------------
| JWT hashing algorithm
|--------------------------------------------------------------------------
|
| Specify the hashing algorithm that will be used to sign the token.
|
| See here: https://github.com/namshi/jose/tree/master/src/Namshi/JOSE/Signer/OpenSSL
| for possible values.
|
*/
'algo'
=>
env
(
'JWT_ALGO'
,
'HS256'
),
/*
|--------------------------------------------------------------------------
| Required Claims
|--------------------------------------------------------------------------
|
| Specify the required claims that must exist in any token.
| A TokenInvalidException will be thrown if any of these claims are not
| present in the payload.
|
*/
'required_claims'
=>
[
'iss'
,
'iat'
,
'exp'
,
'nbf'
,
'sub'
,
'jti'
,
],
/*
|--------------------------------------------------------------------------
| Persistent Claims
|--------------------------------------------------------------------------
|
| Specify the claim keys to be persisted when refreshing a token.
| `sub` and `iat` will automatically be persisted, in
| addition to the these claims.
|
| Note: If a claim does not exist then it will be ignored.
|
*/
'persistent_claims'
=>
[
// 'foo',
// 'bar',
],
/*
|--------------------------------------------------------------------------
| Lock Subject
|--------------------------------------------------------------------------
|
| This will determine whether a `prv` claim is automatically added to
| the token. The purpose of this is to ensure that if you have multiple
| authentication models e.g. `App\User` & `App\OtherPerson`, then we
| should prevent one authentication request from impersonating another,
| if 2 tokens happen to have the same id across the 2 different models.
|
| Under specific circumstances, you may want to disable this behaviour
| e.g. if you only have one authentication model, then you would save
| a little on token size.
|
*/
'lock_subject'
=>
true
,
/*
|--------------------------------------------------------------------------
| Leeway
|--------------------------------------------------------------------------
|
| This property gives the jwt timestamp claims some "leeway".
| Meaning that if you have any unavoidable slight clock skew on
| any of your servers then this will afford you some level of cushioning.
|
| This applies to the claims `iat`, `nbf` and `exp`.
|
| Specify in seconds - only if you know you need it.
|
*/
'leeway'
=>
env
(
'JWT_LEEWAY'
,
0
),
/*
|--------------------------------------------------------------------------
| Blacklist Enabled
|--------------------------------------------------------------------------
|
| In order to invalidate tokens, you must have the blacklist enabled.
| If you do not want or need this functionality, then set this to false.
|
*/
'blacklist_enabled'
=>
env
(
'JWT_BLACKLIST_ENABLED'
,
true
),
/*
| -------------------------------------------------------------------------
| Blacklist Grace Period
| -------------------------------------------------------------------------
|
| When multiple concurrent requests are made with the same JWT,
| it is possible that some of them fail, due to token regeneration
| on every request.
|
| Set grace period in seconds to prevent parallel request failure.
|
*/
'blacklist_grace_period'
=>
env
(
'JWT_BLACKLIST_GRACE_PERIOD'
,
0
),
/*
|--------------------------------------------------------------------------
| Cookies encryption
|--------------------------------------------------------------------------
|
| By default Laravel encrypt cookies for security reason.
| If you decide to not decrypt cookies, you will have to configure Laravel
| to not encrypt your cookie token by adding its name into the $except
| array available in the middleware "EncryptCookies" provided by Laravel.
| see https://laravel.com/docs/master/responses#cookies-and-encryption
| for details.
|
| Set it to true if you want to decrypt cookies.
|
*/
'decrypt_cookies'
=>
false
,
/*
|--------------------------------------------------------------------------
| Providers
|--------------------------------------------------------------------------
|
| Specify the various providers used throughout the package.
|
*/
'providers'
=>
[
/*
|--------------------------------------------------------------------------
| JWT Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to create and decode the tokens.
|
*/
'jwt'
=>
Tymon\JWTAuth\Providers\JWT\Lcobucci
::
class
,
/*
|--------------------------------------------------------------------------
| Authentication Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to authenticate users.
|
*/
'auth'
=>
Tymon\JWTAuth\Providers\Auth\Illuminate
::
class
,
/*
|--------------------------------------------------------------------------
| Storage Provider
|--------------------------------------------------------------------------
|
| Specify the provider that is used to store tokens in the blacklist.
|
*/
'storage'
=>
Tymon\JWTAuth\Providers\Storage\Illuminate
::
class
,
],
];
routes/api.php
View file @
9aab7476
...
...
@@ -13,6 +13,8 @@ use Illuminate\Http\Request;
|
*/
Route
::
middleware
(
'auth:api'
)
->
get
(
'/user'
,
function
(
Request
$request
)
{
return
$request
->
user
();
});
Route
::
group
([
'prefix'
=>
'auth'
],
function
(
$router
)
{
Route
::
post
(
'register'
,
'AuthController@register'
);
Route
::
post
(
'login'
,
'AuthController@login'
);
Route
::
post
(
'current'
,
'AuthController@getAuthenticatedUser'
);
});
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment