Tutorial ini adalah untuk menyimpan data user yang telah log masuk dan keluar dashboard.
1. Jalankan arahan pada terminal cmd untuk membuat file baru.
php spark make:migration loggedin
2. Buka file app/Database/ dan pilih file php yang bertulis Loggedin.php.
3. Update file tersebut dengan kod di bawah;
class Loggedin extends Migration
{
public function up()
{
$this->forge->addField([
'id' => [
'type' => 'BIGINT',
'constraint' => 255,
'unsigned' => true,
'auto_increment' => true,
],
// all field here!
'useremail' => [
'type' => 'VARCHAR',
'constraint' => 255,
],
'usertoken' => [
'type' => 'VARCHAR',
'constraint' => 255,
],
'isloggedin' => [
'type' => 'INT',
'constraint' => 255,
],
'created_at' => [
'type' => 'TIMESTAMP',
'null' => true
],
'updated_at' => [
'type' => 'TIMESTAMP',
'null' => true
],
]);
$this->forge->addPrimaryKey('id');
$this->forge->createTable('loggedinInfo');
}
public function down()
{
$this->forge->dropTable('loggedinInfo');
}
}
4. Simpan dan pergi ke terminal cmd lalu jalankan arahan ini;
php spark migrate
5. Table loggedininfo akan terhasil di dalam database. Kemudian buka semula terminal cmd dan jalankan arahan ini;
php spark make:model LoginPrintModel
6. Buka file app/Models/LoginPrintModel.php dan update kod seperti ini;
class LoginPrintModel extends Model
{
protected $table = 'loggedininfo'; // table name
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = 'array';
protected $useSoftDeletes = false;
protected $protectFields = true;
protected $allowedFields = ['useremail', 'usertoken', 'isloggedin'];
protected bool $allowEmptyInserts = false;
protected bool $updateOnlyChanged = true;
protected array $casts = [];
protected array $castHandlers = [];
// Dates
protected $useTimestamps = true;
protected $dateFormat = 'datetime';
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
// Validation
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $cleanValidationRules = true;
// Callbacks
protected $allowCallbacks = true;
protected $beforeInsert = [];
protected $afterInsert = [];
protected $beforeUpdate = [];
protected $afterUpdate = [];
protected $beforeFind = [];
protected $afterFind = [];
protected $beforeDelete = [];
protected $afterDelete = [];
}
7. Kemudian buka file app/Controller/Login.php dan update kod ini;
$token = JWT::encode($payload, $key, 'HS256');
// login print here
$loginprintmodel = new LoginPrintModel();
$loginprint = [
'useremail' => $user['email'],
'usertoken' => $token,
'isloggedin' => 1,
];
$loginprintmodel->save($loginprint);
$response = [
'message' => 'Login Successful',
'token' => $token,
];
8. Simpan dan run HTTPie. Login user dan lihat table loggedininfo dalam database.
Maksudnya setiap kali user log masuk, info login akan disimpan dalam table loggedininfo database. Ini boleh digunakan untuk mengesan user login atau tidak dan lain-lain hal.
Comments
Post a Comment