initial project
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
namespace App\Controllers\Backend;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use CodeIgniter\HTTP\ResponseInterface;
|
||||
use CodeIgniter\API\ResponseTrait;
|
||||
use App\Models\User;
|
||||
use Firebase\JWT\JWT;
|
||||
|
||||
class LoginController extends BaseController
|
||||
{
|
||||
use ResponseTrait;
|
||||
public function index()
|
||||
{
|
||||
$userModel = new User();
|
||||
|
||||
$username = $this->request->getVar('username');
|
||||
$password = $this->request->getVar('password_hash');
|
||||
|
||||
$user = $userModel->where('username', $username)->first();
|
||||
if(is_null($user)) {
|
||||
return $this->failUnauthorized('Invalid username or password.');
|
||||
}
|
||||
|
||||
$hashedPassword = md5($password);
|
||||
|
||||
if ($hashedPassword !== $user['password_hash']) {
|
||||
return $this->failUnauthorized('Invalid username or password.');
|
||||
}
|
||||
|
||||
$key = getenv('JWT_SECRET');
|
||||
$iat = time(); // current timestamp value
|
||||
$exp = $iat + 86400;
|
||||
|
||||
$payload = array(
|
||||
"iss" => "Issuer of the JWT",
|
||||
"aud" => "Audience that the JWT",
|
||||
"sub" => "Subject of the JWT",
|
||||
"iat" => $iat, //Time the JWT issued at
|
||||
"exp" => $exp, // Expiration time of token
|
||||
"username" => $user['username'],
|
||||
);
|
||||
|
||||
$token = JWT::encode($payload, $key, 'HS256');
|
||||
|
||||
$userData = [
|
||||
'user_id' => $user['id'],
|
||||
'username' => $user['username'],
|
||||
'name' => $user['name'],
|
||||
'role' => $user['role'],
|
||||
'status'=> $user['status'],
|
||||
'outlet_id'=> $user['outlet_id'],
|
||||
// 'created_by'=> $user['created_by'],
|
||||
'updated_at' => $user['updated_at'],
|
||||
'created_at' => $user['created_at'],
|
||||
];
|
||||
$response = [
|
||||
'message' => 'Login Succesful',
|
||||
'token' => $token,
|
||||
'userData' => $userData
|
||||
];
|
||||
|
||||
// session()->set('merchant_id', $user['merchant_id']);
|
||||
|
||||
return $this->respond($response, 200);
|
||||
}
|
||||
|
||||
public function requestToken()
|
||||
{
|
||||
$userModel = new User();
|
||||
|
||||
$merchant = $this->request->getVar('merchant');
|
||||
$token_key = $this->request->getVar('token_key');
|
||||
|
||||
$user = $userModel->where('username', $merchant)->first();
|
||||
if(is_null($user)) {
|
||||
return $this->failUnauthorized('Invalid username.');
|
||||
}
|
||||
|
||||
if ($token_key !== $user['token_key']) {
|
||||
return $this->failUnauthorized('Invalid token key.');
|
||||
}
|
||||
|
||||
$key = getenv('JWT_SECRET');
|
||||
$iat = time(); // current timestamp value
|
||||
$exp = $iat + 86400;
|
||||
|
||||
$payload = array(
|
||||
"iss" => "Issuer of the JWT",
|
||||
"aud" => "Audience that the JWT",
|
||||
"sub" => "Subject of the JWT",
|
||||
"iat" => $iat, //Time the JWT issued at
|
||||
"exp" => $exp, // Expiration time of token
|
||||
"user_id" => $user['user_id'],
|
||||
"token_key" => $token_key,
|
||||
);
|
||||
|
||||
$token = JWT::encode($payload, $key, 'HS256');
|
||||
|
||||
$response = [
|
||||
'message' => 'Request Succesfully',
|
||||
'token' => $token,
|
||||
'expires' => date('Y-m-d H:i:s', $exp)
|
||||
];
|
||||
|
||||
return $this->respond($response, 200);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the properties of a resource object
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function show($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new resource object, with default properties
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function new()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new resource object, from "posted" parameters
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the editable properties of a resource object
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function edit($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Add or update a model resource, from "posted" properties
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete the designated resource object from the model
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function delete($id = null)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user