<?php

namespace ALajusticia\Logins;

use Illuminate\Auth\EloquentUserProvider;
use Illuminate\Contracts\Auth\Authenticatable as UserContract;
use Illuminate\Database\Eloquent\Model;

class LoginsUserProvider extends EloquentUserProvider
{
    /**
     * Retrieve a user by their unique identifier and "remember me" token.
     *
     * @param  mixed  $identifier
     * @param  string  $token
     * @return Model|null|void
     */
    public function retrieveByToken($identifier, $token)
    {
        $model = $this->createModel();

        $retrievedModel = $this->newModelQuery($model)->with('logins')->where(
            $model->getAuthIdentifierName(), $identifier
        )->first();

        if (! $retrievedModel) {
            return;
        }

        $login = $retrievedModel->logins->whereStrict('remember_token', $token)->first();

        if ($login && hash_equals($login->remember_token, $token)) {
            session(['login_id' => $login->id]);

            $login->update(['session_id' => null]);

            return $retrievedModel;
        }

        return null;
    }

    /**
     * Update the "remember me" token for the given user.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  string  $token
     * @return void
     */
    public function updateRememberToken(UserContract $user, $token)
    {
        $user->setRememberToken($token);
    }
}
