Advertisement
Guest User

Server api

a guest
Feb 16th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.12 KB | None | 0 0
  1. //on the server i have this:
  2.  
  3.     public function login(Request $request){
  4.     if(Auth::attempt(['email' => $request->email, 'password' => $request->password])){
  5.         $user = Auth::user();
  6.         $success['token'] =  $user->createToken('MyApp')->accessToken;
  7.         return response()->json(['success' => $success], 200);
  8.     }
  9.     else{
  10.         return response()->json(['error'=>'Unauthorised'], 401);
  11.     }
  12. }
  13. /// Client
  14.     public function postLogin(Request $request){
  15.                $client = new Client();  
  16.              $email=$request->email;
  17.              $password=$request->password;
  18.  
  19.              $response = $client->request('POST', 'http://serverapi-v2.io/api/login', [
  20.             'form_params' => [
  21.                'email' => $email,
  22.               ' password' => $password,
  23.             ]
  24.        ]);
  25.       $apiResponse = collect(json_decode((string) $response->getBody(), true));
  26.        dd($apiResponse);
  27.        session(['api'=> $apiResponse]);
  28.        session(['api-token'=> $apiResponse['access_token']]);
  29.  
  30.      //return redirect()->back()->with('success','Data submitted successfully!');
  31.        }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement