I'm new in codeigniter 4 rest api and oath. I'm able to create login that return the token and the refresh token. My problem is when the token has expired. How can I get new token using the refresh token? Do I have to create a new function in the controller for that? Or can it be the same endpoint as the login? I've read in some articles that I need to send grant_type, client_id, client_secret and the refresh token as a post. But I don't know the endpoint on where to send this. I'm totally new to this, please help me. Thanks.
User.php(Controller)
<?php namespace App\Controllers;
use \App\Libraries\Oauth;
use \OAuth2\Request;
use CodeIgniter\API\ResponseTrait;
use App\Models\UserModel;
class User extends BaseController
{
use ResponseTrait;
public function login(){
$oauth = new Oauth();
$request = new Request();
$respond = $oauth->server->handleTokenRequest($request->createFromGlobals());
$code = $respond->getStatusCode();
$body = $respond->getResponseBody();
return $this->respond(json_decode($body), $code);
}
Oauth.php
<?php namespace App\Libraries;
//use \OAuth2\Storage\Pdo;
use \App\Libraries\CustomOauthStorage;
class Oauth{
var $server;
function __construct(){
$this->init();
}
public function init(){
$dsn = getenv('database.default.DSN');
$username = getenv('database.default.username');
$password = getenv('database.default.password');
$storage = new CustomOauthStorage(['dsn' => $dsn, 'username' => $username, 'password' => $password]);
$this->server = new \OAuth2\Server($storage);
$this->server->addGrantType(new \OAuth2\GrantType\UserCredentials($storage));
}
}