Issues (150)

src/Extension/JWTTrait.php (1 issue)

1
<?php
2
namespace SoliDry\Extension;
3
4
use SoliDry\Helpers\Json;
5
use SoliDry\Helpers\Jwt;
6
use SoliDry\Types\JwtInterface;
7
8
/**
9
 * Class JWTTrait
10
 *
11
 * @package SoliDry\Extension
12
 *
13
 * @property ApiController model
14
 */
15
trait JWTTrait
16
{
17
    /**
18
     *  Creates new user with JWT + password hashed
19
     */
20
    protected function createJwtUser()
21
    {
22
        if(empty($this->model->password)) {
23
            Json::outputErrors(
24
                [
25
                    [
26
                        JSONApiInterface::ERROR_TITLE  => 'Password should be provided',
27
                        JSONApiInterface::ERROR_DETAIL => 'To get refreshed token in future usage of application - user password should be provided',
28
                    ],
29
                ]
30
            );
31
        }
32
33
        /** @var \Illuminate\Database\Eloquent\Model $model */
34
        $model           = $this->getEntity($this->model->id);
35
        $model->jwt      = Jwt::create($this->model->id);
36
        $model->password = password_hash($this->model->password, PASSWORD_DEFAULT);
37
        $model->save();
38
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by
It seems like $model of type Illuminate\Database\Eloquent\Model is incompatible with the declared type SoliDry\Extension\ApiController of property $model.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
39
        unset($this->model->password);
40
    }
41
42
    /**
43
     * @param $model
44
     * @param $jsonApiAttributes
45
     */
46
    private function updateJwtUser(&$model, $jsonApiAttributes)
47
    {
48
        if(password_verify($jsonApiAttributes[JwtInterface::PASSWORD], $model->password) === false) {
49
            Json::outputErrors(
50
                [
51
                    [
52
                        JSONApiInterface::ERROR_TITLE  => 'Password is invalid.',
53
                        JSONApiInterface::ERROR_DETAIL => 'To get refreshed token - pass the correct password',
54
                    ],
55
                ]
56
            );
57
        }
58
59
        $model->jwt = Jwt::create($model->id);
60
        unset($model->password);
61
    }
62
}