1
votes

please see the below code of UserController . Iam trying to use VUEJS via api .So i have created routes for register and login at api.php . For testing im trying to pass data through postman and via http://localhost:8000/api/register with Accepted application/json and Content-type with application/json. I'm passing data via body as form data. The problem is that when I try to send data postman send data continuously with out a response and when I cancelled the request the port 8000 doesn't work anymore. So i have to stop the server and run again. Can anyone tell me are there are any errors in my code.Im using laravel 5.7

<?php

namespace App\Http\Controllers\Api;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
   public function  register(Request $request){
       $request->validate([
          'name' => 'required',
          'email'=> 'required',
          'password'=> 'required'
       ]);
       //add some cde that what happen if validation fils
       $newUser = User::firstOrNew(['email'=> $request->email]);
       $newUser->name = $request->name;
       $newUser->email =$request->email;
       $newUser->password = bcrypt($request->password);
       $newUser->save();

        $http = new Client;

        $response = $http->post(url('oauth/token'), [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => '2',
                'client_secret' => '9J0sU4Ctz5p3AUz7ROiv7jELnGrU5waepprqICyH',
                'username' => $request->email,
                'password' => $request->password,
                'scope' => '',
            ],
        ]);

        return response(['data' => json_decode((string)$response->getBody(), true) ]) ;

       }
   public function login(Request $request){
        $request->validate([

            'email'=> 'required',
            'password'=> 'required'
       ]);

       $user = User::where('email', $request->email)->first();

       if(!$user){
           return response(['status' => 'error' , 'message'=> 'user not found']);
       }
       if(Hash::check($request->password, $$user->password)){
            $http = new Client;

            $response = $http->post(url('oauth/token'), [
                'form_params' => [
                    'grant_type' => 'password',
                    'client_id' => '2',
                    'client_secret' => '9J0sU4Ctz5p3AUz7ROiv7jELnGrU5waepprqICyH',
                    'username' => $request->email,
                    'password' => $request->password,
                    'scope' => '',
                ],
            ]);

            return response(['data' => json_decode((string)$response->getBody(), true)]);
       }
   }
}
1

1 Answers

0
votes

I have pretty much the same login-code. This one is working for me:

public function login(Route $route, Request $request)
{
    // validation stuff...

    $user = User::where('email', $request->email)->first();

    if ($user && Hash::check($request->password, $user->password)) {
        $http = new Client();
        $response = $http->post(url('oauth/token'), array(
            'form_params' => array(
                'grant_type' => 'password',
                'client_id' => 2,
                'client_secret' => '...',
                'username' => $request['email'],
                'password' => $request['password']
            )));
        return json_decode($response->getBody(), true)['access_token'];
    } else {
        abort(401);
    }
}

And: You wrote $$user in your second 'if'...