php - 如何以编程方式登录然后使用 Laravel 5.5 重定向?

标签 php laravel redirect authentication

我正在使用 Laravel 5.5 和 PHP 7.1 我使用的是标准的内置 Auth 和中间件,我没有自定义任何东西。

我想在 api.php 路由文件中创建一个如下所示的路由:

Route::group(array('prefix' => 'mobile'), function () {
      Route::get('requestaccess', ['uses' => 'MobileController@RequestAccess']);
});

然后在我的 MobileController 中我将拥有以下代码:

public function RequestAccess() {
    try {
        $input = request()->all();
        $token = $input['token'];
        //$page = $input['page'];

        $user = User::where('mobile_access_token', $token)->first();
        if (!$user)
            return view('public.error', ['errorCode' => 901]);

        if ($user->mobile_access_expires < Carbon::now())
            return view('public.error', ['errorCode' => 902]);

         // I tried with and without the second param (true)
        if (!Auth::loginUsingId($user->id, true))
            return view('public.error', ['errorCode' => 903]);

        // My code makes it to here just fine which means the token
        // was valid and I 
        // successfully logged in.
        // Now I want to send them to a page where the user
        // can browse and remain logged in. 

        // I tried the following line but it does not work
        // It just immediately redirects me to the login page
        return redirect(url('dashboard'));

        // The following line works but if user clicks a link
        // to go to any other page it redirects them to the login page 
        return view('console.dashboard.dashboard');

    } catch (\Exception $e) {
        return view('public.error', ['errorCode' => 900, 'errorMessage' => $e->getMessage()]);
    }
}

在 web.php 路由文件中我有以下内容:

Route::group(['middleware' => 'auth'], function() {
    Route::get('dashboard', ['uses' => 'DashboardController@getDashboard']);
});

当然,为了测试,我在浏览器中输入如下内容:

example.com/api/mobile/requestaccess?token=abc

一切都在一个域中,我只想让用户登录并让他们在浏览网站时保持登录状态。

任何帮助将不胜感激。

最佳答案

您可以像这样使用Auth::login() 登录用户

public function RequestAccess() {
    try {
        $input = request()->all();
        $token = $input['token'];
        $user = User::where('mobile_access_token', $token)->first();

        if (!$user){
             return view('public.error', ['errorCode' => 901, 'errorMessage' => 'Could not find user']);
        }

        Auth::login($user);

        redirect()->('dashboard');
    }catch(\Exception $e){
        return view('public.error', ['errorCode' => 900, 'errorMessage' => $e->getMessage()]);
    }
}

注意:您已经写过您的路由在 api 中,并且在 Controller 中您正在呈现 View ,请确保此代码适用于具有 web 守卫StartSession 中间件。如果您正在寻找 api 身份验证,那么您可以查看 this

检查其他身份验证方法部分 https://laravel.com/docs/5.6/authentication#authenticating-users

关于php - 如何以编程方式登录然后使用 Laravel 5.5 重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51183172/

相关文章:

Laravel 调度程序在 tmp 文件夹中创建数千个日志文件

laravel - 授权 header 仅在 GET 请求上无法到达 API (nginx)

http - 如何将文件提供给telnet

php - 如何使用省略号构建分页

php - 有效解决方案: base32 encoding in php

php - 查找 sql 值最新变化的更好方法

php - extract() 如何在当前范围内创建变量?

php - Laravel Valet 安装后 Ping test.dev 返回 "Unknown Host"

http - .htaccess 强制 https 但只有一个页面带有 http 或 https

javascript - 使用 Node Js 重定向无法正常工作