@Nakov
migration file
public function up()
{
Schema::create('personal_access_tokens', function (Blueprint $table) {
$table->id();
$table->morphs('tokenable');
$table->string('name');
$table->string('token', 64)->unique();
$table->text('abilities')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamps();
});
}
default model created for personal access tokens table
{
protected $casts = [
'abilities' => 'json',
'last_used_at' => 'datetime',
'expires_at' => 'datetime',
];
protected $fillable = [
'name',
'token',
'abilities',
'expires_at',
];
protected $hidden = [
'token',
];
public function tokenable()
{
return $this->morphTo('tokenable');
}
public static function findToken($token)
{
if (strpos($token, '|') === false) {
return static::where('token', hash('sha256', $token))->first();
}
[$id, $token] = explode('|', $token, 2);
if ($instance = static::find($id)) {
return hash_equals($instance->token, hash('sha256', $token)) ? $instance : null;
}
}
public function can($ability)
{
return in_array('*', $this->abilities) ||
array_key_exists($ability, array_flip($this->abilities));
}
public function cant($ability)
{
return ! $this->can($ability);
}
}
in sanctum config file i only changed the 'expiration' =>10,
the controller file
public function login(Request $request) {
$employee = Employee::where(['userName' => $request->userName, 'password' => $request->password])->first();
if(!$employee || Hash::check($request->password, $employee->password)) {
return response()->json("check userName or password", 404, ['content-type' => 'text/json']);
}
if($employee->tokens()->where('tokenable_id', $employee->id)->exists()) {
$employee->tokens()->delete();
}
$token = $employee->createToken($request->userName)->plainTextToken;
$response =[
'employee' => $employee,
'token' => $token
];
return response()->json($response, 200, ['content-type' => 'text/json']);
}