GuzzleHttp Laravel login API using GET request

守給你的承諾、 提交于 2020-01-06 14:34:13

问题


I want to create Laravel REST API for user registration and login. I created POST method for registration. Now I want to use GET method for login using GuzzleHttp. How can I do that? My routes:

Route::group(['middleware' => 'api'], function() {
Route::group(['prefix' => 'v1'], function () {
    Route::post('/register', 'Api\V1\AuthController@register')->name('user.register');
    Route::get('/signin', 'Api\V1\AuthController@signin')->name('user.signin');
});

});

And this is my controller:

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\JWTAuth;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;

    class AuthController extends Controller
    {
         public function register(Request $request)
        {
            $this->validate($request, [
                'name' => 'required',
                'email' => 'required|email',
                'password' => 'required|min:5'
            ]);

            $name = $request->input('name');
            $email = $request->input('email');
            $password = $request->input('password');

            $user = new User([
                'name' => $name,
                'email' => $email,
                'password' => bcrypt($password)
            ]);

            if($user->save()){
                $response = [
                    'msg' => 'User created',
                    'user' => $user
                ];

                return response()->json($response, 201);
            }

            $response = [
                'msg' => 'Error occured'
            ];

            return response()->json($response, 404);
        }

        public function signin(Request $request)
        {
            $client = new GuzzleHttp\Client();
            $this->validate($request, [
                'email' => 'required|email',
                'password' => 'required'
            ]);
        }    
    }

Anyone did something like this. Thanks in advance.


回答1:


Access the signin routes like this http://your.domain/signin?email=cool@email.com&password=fooandbar

To access the email and password parameter

use $request->email and $request->password



来源:https://stackoverflow.com/questions/50591063/guzzlehttp-laravel-login-api-using-get-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!