samedi 26 décembre 2015

Set the active status of a user in the credentials and still return an activation message in Laravel 5.1

I wanted to find out how I would do as requested in the subject line, as the code below works fine but the user is logged in before checking the $user->Activated status. Here is some code to illustrate:

AuthController

public function authenticated(Request $request, User $user)
{
    if ($user->Activated) {
        return redirect()->intended($this->redirectPath());
    } else {
        Auth::logout();
        return redirect($this->loginPath())
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                'activated' => 'You need to activate your account to login'
            ]);
    }
}

Preferably I would like to do the following:

AuthController

public function getCredentials(Request $request)
{
    $credentials = $request->only($this->loginUsername(), 'password');

    return array_add($credentials, 'Activated', '1');
}

But then the only message that gets returned is "These credentials do not match our records.", instead of "You need to activate your account to login". Also how would I update a LoginStatusId once the user is logged in, currently I do it like this:

AuthController

public function authenticated(Request $request, User $user)
{
    if ($user->Activated) {
        $user->LoginStatusId = 1;
        $user->save();
        return redirect()->intended($this->redirectPath());
    } else {
        Auth::logout();
        return redirect($this->loginPath())
            ->withInput($request->only('email', 'remember'))
            ->withErrors([
                'activated' => 'You need to activate your account to login'
            ]);
    }
}

Is there a better place to set the $user->LoginStatusId once they login, or is this the best place to put it?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire