Laravel创建重新激活按钮错误

时间:2017-05-16 18:55:21

标签: php laravel middleware

我想创建一个重新启动帐户选项,以便在用户停用其帐户后,他们始终可以点击重新启动并返回。

现在的问题是,当我点击应该调用控制器功能的链接时,它无法正常工作并且无论我做什么都会继续返回/auth页面。

现在这是我的中间件:

public function handle($request, Closure $next)
{
    if (!Auth::check()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('/');
        }
    }
    else
    {
        $user = Auth::user();
        if (!$user->activated) {
            $activation = action('Auth\AuthController@getActivationMail', ['username' => $user->username]);
            Auth::logout();

            return redirect()->guest('auth')
                ->withErrors(array('message' => 'Please activate your account. Re-send your activation by clicking <a href=' . $activation . '>here</a>.'));
        }



         // the main code starts here

        else if (!$user->enabled) {
            $reactivation = action('UserController@postActivateUser');
            Auth::logout();
            //$reactivation = $user->enabled = 1;
            //$user->save();
            return redirect('/auth')->withErrors(array('message' => 'Your account has been deactivated. You can reactivate your account by clicking <a href='. $reactivation .'> Here </a>.'))->withInput();
        }

        $user->runDailyNotifications();
    }
    return $next($request);
}

}

我在UserController中创建了postActivate函数,因为它有一个类似的函数(这里类似的函数):

    public function postDeactivateUser(Request $request)
{
    if ($request->ajax()) {            
        $user = Auth::user();
        User::removeData($user);
        $user->enabled = false;
        $user->save();

        return response()->json(['result' => true, 'msg' => 'Your account has been deactivated.']);
    }
}

所以我创建了一个类似的:

public function postActivateUser(Request $request)
{
    $user = Auth::user();
    $user->enabled = 1;
    $user->save();

    return response()->json(['result' => true, 'msg' => 'Your account has been activated.']);
}

和路线:

Route::get('auth/activate', 'UserController@postActivateUser');

我不知道如何才能做到正确,谢谢有人帮助我。

1 个答案:

答案 0 :(得分:0)

你在运行什么Laravel版本? 您的中间件要求用户必须登录才能激活其帐户,否则会重定向到身份验证屏幕。

library("future")
plan(multiprocess)

tempjob1 %<-% myFunction(args)
tempjob2 %<-% myFunction(other args)
...
tempjobN %<-% myFunction(some other args here)

temp.list <- lapply(ls(pattern = "temp"), get)

您应该从中间件中排除激活路由,并使用激活令牌来识别未经身份验证的用户并重新激活其帐户。

将此行置于路由文件中的auth中间件之外。

if (!Auth::check()) {
        if ($request->ajax()) {
            return response('Unauthorized.', 401);
        } else {
            return redirect()->guest('/');
        }
    }

然后在postActivateUser方法中工作,您应该根据您的激活URL和User :: findByToken根据您的数据库字段更改request-&gt;标记参数名称。

Route::get('auth/activate', 'UserController@postActivateUser');