Skip to content

Commit b1fa1c5

Browse files
author
mostafa
committed
kauth user type
new jwt attributes user type as ut
1 parent 975f8a9 commit b1fa1c5

File tree

4 files changed

+21
-10
lines changed

4 files changed

+21
-10
lines changed

config/kauth.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
| Kauth jwt payload iss and aud
3030
|--------------------------------------------------------------------------
3131
|
32-
| You can set jwt iss and aud
32+
| You can set jwt iss
3333
|
3434
| your url host name
3535
*/
3636
"payload" => [
37-
"iss" => "",
38-
"aud" => ""
37+
"iss" => ""
3938
],
4039

4140
/*

migrations/2019_02_04_082555_create_kauth_table.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function up()
1515
{
1616
Schema::create('kauth', function (Blueprint $table) {
1717
$table->increments('id');
18-
$table->string('tokon',255)->nullable();
18+
$table->text('tokon')->nullable();
1919
$table->integer('user_id')->nullable();
2020
$table->integer('iat')->nullable();
2121
$table->integer('exp')->nullable();
@@ -24,6 +24,8 @@ public function up()
2424
$table->string('device')->nullable();
2525
$table->string('os')->nullable();
2626
$table->string('login')->nullable();
27+
$table->string('guard')->nullable();
28+
$table->string('user_type')->nullable();
2729
$table->boolean('active')->default(true);
2830
$table->timestamps();
2931
});

src/Auth/Auth.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Kauth\Model\KauthModel;
1414
use Kauth\Token\Token;
1515
use DateTime;
16+
use Schema;
1617
use Config;
1718
use Hash;
1819
use DB;
@@ -24,6 +25,8 @@ class Auth
2425

2526
protected $socialite;
2627

28+
protected $user_type = '';
29+
2730
public function __construct()
2831
{
2932
$this->guard();
@@ -80,7 +83,7 @@ public function attempt($credentials)
8083

8184
// fetch user by credentials
8285

83-
$getToken = DB::table(Config::get('kauth.guard.' . $this->guard. '.table'))
86+
$getToken = DB::table(Config::get('kauth.guard.' . $this->guard . '.table'))
8487
// multiple username accept (id||username||email >> etc)
8588
->where(function ($query) use ($getTokennames,$getTokenname){
8689
foreach ($getTokennames as $key => $value) {
@@ -114,14 +117,22 @@ public function attempt($credentials)
114117
$jwt->os = \Request::get('os');
115118
$jwt->device = \Request::get('device');
116119
$jwt->active = true;
120+
$jwt->guard = $this->guard;
117121
$jwt->save();
118122

119123
$secret = new Token();
120-
$tokon = $secret->create($jwt->id);
124+
if (Schema::hasColumn(Config::get('kauth.guard.' . $this->guard . '.table'), 'user_type')) {
125+
$this->user_type = $getToken->user_type;
126+
}
127+
$tokon = $secret->create($jwt->id,$this->user_type);
121128
$payloader = $secret->payloader($tokon);
122129
$jwt->tokon = $tokon;
123130
$jwt->iat = $payloader['iat'];
124131
$jwt->exp = $payloader['expM'];
132+
133+
if (Schema::hasColumn($this->guard, 'user_type')) {
134+
$jwt->user_type = $getToken->user_type;
135+
}
125136
$jwt->save();
126137
return $jwt->tokon;
127138
}

src/Token/Token.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class Token
1919
{
20-
public function create($tokenID){
20+
public function create($tokenID,$userType){
2121
$issueDate = new DateTime();
2222
$expiredDate = new DateTime();
2323
$tokenDuration = Config::get('kauth.token_exp') ? Config::get('kauth.token_exp') : 'P32D';
@@ -27,11 +27,10 @@ public function create($tokenID){
2727
$key = "example_key";
2828
$token = array(
2929
"iss" => $jwtIss,
30-
"aud" => $jwtAud,
31-
"auth" => true,
30+
"ut" => $userType,
3231
"iat" => $issueDate->getTimestamp(),
3332
"expM" => $expiredDate->getTimestamp(),
34-
"token_id" => $tokenID,
33+
"tid" => $tokenID,
3534
);
3635
$jwt = JWT::encode($token,$key);
3736

0 commit comments

Comments
 (0)