public function updateToken(User $user)
    {
        if (!$user->has('id')) {
            throw new FatalErrorException(__d('user', 'UsersTable::updateToken(), no ID was found for the given entity.'));
        }

        $token = md5(uniqid($user->id, true));
        $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
        while ($count > 0) {
            $token = str_shuffle(md5(uniqid($user->id, true) . rand(1, 9999)));
            $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
        }

        $user->set('token', $token);
        $user->set('token_expiration', time() + USER_TOKEN_EXPIRATION);
        $this->updateAll([
            'token' => $user->get('token'),
            'token_expiration' => $user->get('token_expiration'),
        ], ['id' => $user->id]);

        return $user;
    }
public function updateToken(User $user)
    {
        if (!$user->has('id')) {
            throw new FatalErrorException(__d('user', 'UsersTable::updateToken(), no ID was found for the given entity.'));
        }

        $token = md5(uniqid($user->id, true));
        $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
        while ($count > 0) {
            $token = str_shuffle(md5(uniqid($user->id, true) . rand(1, 9999)));
            $count = $this->find()->where(['Users.token' => $token])->limit(1)->count();
        }

        $user->set('token', $token);
        $user->set('token_expiration', time() + USER_TOKEN_EXPIRATION);
        $this->updateAll([
            'token' => $user->get('token'),
            'token_expiration' => $user->get('token_expiration'),
        ], ['id' => $user->id]);

        return $user;
    }
public function updateToken ( User $ user ) { if ( ! $ user -> has ( 'id' ) ) { throw new FatalErrorException ( __d ( 'user' , 'UsersTable::updateToken(), no ID was found for the given entity.' ) ) ; } $ token = md5 ( uniqid ( $ user -> id , true ) ) ; $ count = $ this -> find ( ) -> where ( [ 'Users.token' => $ token ] ) -> limit ( 1 ) -> count ( ) ; while ( $ count > 0 ) { $ token = str_shuffle ( md5 ( uniqid ( $ user -> id , true ) . rand ( 1 , 9999 ) ) ) ; $ count = $ this -> find ( ) -> where ( [ 'Users.token' => $ token ] ) -> limit ( 1 ) -> count ( ) ; } $ user -> set ( 'token' , $ token ) ; $ user -> set ( 'token_expiration' , time ( ) + USER_TOKEN_EXPIRATION ) ; $ this -> updateAll ( [ 'token' => $ user -> get ( 'token' ) , 'token_expiration' => $ user -> get ( 'token_expiration' ) , ] , [ 'id' => $ user -> id ] ) ; return $ user ; }
Generates a unique token for the given user entity. The generated token is
automatically persisted on DB.

Tokens are unique within the entire DB and follows the pattern below:

<32-random-letters-and-numbers>

@param \User\Model\Entity\User $user The user for which generate the token
@return \User\Model\Entity\User The user entity with a the new token property
@throws \Cake\Error\FatalErrorException When an invalid user entity was given
Generates a unique token for the given user entity . The generated token is automatically persisted on DB .