Postingan lainnya
Tidak bisa men-decode token, rest api codeigniter
saya lagi ngikut tutorial rest api codeigniter di playlist sekolah koding, nah di saat di bagian check token saya mendapat error saat ingin mendecode token
Hasil dari Postman :
Routes :
<?php
defined('BASEPATH') or exit('No direct script access allowed');
// ROUTE API
$route['api/users']['GET'] = 'UserController/get_all';
$route['api/user/(:num)']['GET'] = 'UserController/get/$1';
$route['api/register']['POST'] = 'UserController/register';
$route['api/user/(:num)']['PUT'] = 'UserController/update/$1';
$route['api/user/(:num)']['DELETE'] = 'UserController/delete/$1';
$route['api/login'] = 'UserController/login';
$route['api/check_token']['GET'] = 'UserController/check_token';
$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
Controller :
<?php
defined('BASEPATH') or exit('No direct script access allowed');
require_once APPPATH . '/libraries/JWT.php';
use \Firebase\JWT\JWT;
class UserController extends CI_Controller
{
private $secret = 'this is key secret';
public function __construct()
{
parent::__construct();
$this->load->model('user');
}
public function response($data)
{
$this->output
->set_content_type('application/json')
->set_status_header(200)
->set_output(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES))
->_display();
exit;
}
public function register()
{
return $this->response($this->user->save());
}
public function get_all()
{
return $this->response($this->user->get());
}
public function get($id)
{
return $this->response($this->user->get('id', $id));
}
public function login()
{
$date = new DateTime();
if (!$this->user->is_valid()) {
return $this->response([
'success' => false,
'message' => 'email atau password salah'
]);
}
$user = $this->user->get('email', $this->input->post('email'));
// encode data
$payload['id'] = $user->id;
$payload['iat'] = $date->getTimestamp();
$payload['exp'] = $date->getTimestamp() + 60 * 60 * 2;
$output['id_token'] = JWT::encode($payload, $this->secret);
$this->response($output);
}
public function check_token()
{
$jwt = $this->input->get_request_header('Authorization');
try {
$decoded = JWT::decode($jwt, $this->secret, array('HS256'));
var_dump($decoded);
} catch (\Exception $e) {
// return $this->response([
// 'success' => false,
// 'message' => 'gagal, error token'
// ]);
var_dump($e);
}
}
public function delete($id)
{
// check user login
$this->check_token();
// orang yang login yg mau hapus
}
}
Models :
<?php
class User extends CI_Model
{
public function save()
{
$data = [
'email' => $this->input->post('email'),
'password' => password_hash($this->input->post('password'), PASSWORD_DEFAULT),
];
if ($this->db->insert('users', $data)) {
return [
'id' => $this->db->insert_id(),
'success' => true,
'message' => 'data berhasil dimasukan'
];
}
}
public function get($key = null, $value = null)
{
if ($key != null) {
$query = $this->db->get_where('users', array($key => $value));
return $query->row();
}
$query = $this->db->get('users');
return $query->result();
}
public function is_valid()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$hash = $this->get('email', $email)->password;
if (password_verify($password, $hash))
return true;
return false;
}
}
4 Jawaban:
pada saat mengirim di postman, coba mainkan di bagian tab Body > di bawahnya pilih 'application/x-www-form-urlencoded',
halo mas hilman, sy udah nyoba juga pakai body seperti ini <img src='https://i.imgur.com/ZOlIjTO.png'>
tapi hasilnya masih sama juga
Tanggapan
halo Mas Ariel, coba download kode yang ada pada bagian akhir , dan test apakah errornya sama dengan sebelumnya https://sekolahkoding.com/kelas/membuat-rest-api-codeigniter kamu bisa bandingkan kode kamu dengan ini
pastikan aja token yang kamu paste tidak ada spasi di bagian awal dan akhirnya
coba di debug print $jwt nya apakah ada isinya, kalau kosong perlu di cek bagian set dari client & get dari header nya