vendredi 8 janvier 2016

Laravel redirect after user activation failing to flash data

After a user registers an account on my site they are logged in, set a status of "inactive" in my users table, sent an activation email, and restricted of certain features until they activate their account by clicking a link in the email. I have all of this working but am running into an issue flashing data with a redirect. The session is working fine and I can put values successfully. However, if I flash data it does not appear to set. If I dd(Session::all()) in my home template the flash arrays are completely empty:

array:4 [
    "_token" => "..."
    "_previous" => array:1 [...]
    "flash" => array:2 [
        "old" => []
        "new" => []
    ]
    "login_..." => 1
]

routes.php

Route::get('activate/{code}', 'ActivateController@getActivate')
    ->where('code', '[0-9a-f]{64}');

ActivateController.php

<?php

namespace App\Http\Controllers;

use DB;
use Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class ActivateController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function getActivate(Request $request, $code)
    {
        $user = User::where('activation_code', $code)->where('status', 'inactive')->first();

        if ($user && $user->id == Auth::user()->id && $user->update(['status' => 'active'])) {
            $message = 'Account activated successfully.';
        } elseif ($user) {
            $message = 'Invalid activation token.';
        } else {
            $message = 'Activation code not found or user is already activated.';
        }

        return redirect('home')->with('msg', $message);
    }
}

If someone could help me I'd be very grateful. If any other information would be helpful, let me know and I will provide it.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire