mardi 11 février 2020

Laravel not logging in after getting response from API

I was using Truecaller API to get user info from truecaller.

The API actually sends a post request to the website. I used TruecallerController to receive information from the API. The API is working fine and I am also able to store user information in my database. But when I used Auth::login($user), it does not work. It does not redirect too.

Here is my controller code(I have removed the redirect part as it is not my concern for now):

public function login(Request $request){
        $this->validate($request,[
            'accessToken' => 'required',
        ]);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$request->endpoint);
        curl_setopt($ch, CURLOPT_HTTPHEADER,array(
            'Authorization: Bearer '.$request->accessToken,
            'Cache-Control: no-cache'
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $res = curl_exec($ch);
        curl_close ($ch);
        $res = json_decode($res, true);
        $name = $res['name']['first']." ".$res['name']['last'];
        $email = $res['onlineIdentities']['email'];
        $phone = $res['phoneNumbers'][0];
        $user = new User;
        $user->email = $email;
        $user->name = $name;
        $user->phone = $phone;
        $user->save();
        Auth::login($user); //This is not working
    }

The code inserts the user data in the database, it just does not Authorise the user.

I have tried:

  1. Auth::login($user)
  2. $request->session()->put('user',$user) -- This is not working too.
  3. Uncommented the AuthenticateSession line from kernel.php.

Please help.

Thanks.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2uF6XzM
via IFTTT

Aucun commentaire:

Enregistrer un commentaire