Skip to content

Commit 76d4531

Browse files
authored
Merge pull request #8 from manojLondhe/release-2.0.0
Generate JWT token during login
2 parents b3e2ebc + 3f0d090 commit 76d4531

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/users/login.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
defined('_JEXEC') or die( 'Restricted access' );
1010

11+
require_once JPATH_SITE . '/components/com_api/vendors/php-jwt/src/JWT.php';
12+
13+
use Firebase\JWT\JWT;
14+
1115
jimport('joomla.plugin.plugin');
1216
jimport('joomla.html.html');
1317
jimport('joomla.application.component.controller');
@@ -39,7 +43,7 @@ public function keygen()
3943
//init variable
4044
$obj = new stdclass;
4145
$umodel = new JUser;
42-
$user = $umodel->getInstance();
46+
$user = $umodel->getInstance();
4347

4448
$app = JFactory::getApplication();
4549
$username = $app->input->get('username', 0, 'STRING');
@@ -50,7 +54,7 @@ public function keygen()
5054
if($id == null)
5155
{
5256
$model = FD::model('Users');
53-
$id = $model->getUserId('email', $username);
57+
$id = $model->getUserId('email', $username);
5458
}
5559

5660
$kmodel = new ApiModelKey;
@@ -83,31 +87,52 @@ public function keygen()
8387

8488
$result = $kmodel->save($data);
8589
$key = $result->hash;
86-
90+
8791
//add new key in easysocial table
8892
$easyblog = JPATH_ROOT . '/administrator/components/com_easyblog/easyblog.php';
8993
if (JFile::exists($easyblog) && JComponentHelper::isEnabled('com_easysocial', true))
9094
{
9195
$this->updateEauth( $user , $key );
9296
}
9397
}
94-
98+
9599
if( !empty($key) )
96100
{
97101
$obj->auth = $key;
98102
$obj->code = '200';
99103
//$obj->id = $user->id;
100104
$obj->id = $id;
105+
106+
// Generate claim for jwt
107+
$data = [
108+
"id" => trim($id),
109+
/*"iat" => '',
110+
"exp" => '',
111+
"aud" => '',
112+
"sub" => ''"*/
113+
];
114+
115+
// Using HS256 algo to generate JWT
116+
$jwt = JWT::encode($data, trim($key), 'HS256');
117+
118+
if (isset($jwt) && $jwt != '')
119+
{
120+
$obj->jwt = $jwt;
121+
}
122+
else
123+
{
124+
$obj->jwt = false;
125+
}
101126
}
102127
else
103128
{
104129
$obj->code = 403;
105130
$obj->message = JText::_('PLG_API_USERS_BAD_REQUEST_MESSAGE');
106131
}
107132
return( $obj );
108-
133+
109134
}
110-
135+
111136
/*
112137
* function to update Easyblog auth keys
113138
*/

0 commit comments

Comments
 (0)