D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
tattooscyy
/
api2
/
app
/
Http
/
Controllers
/
Filename :
SocialiteController.php
back
Copy
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use Laravel\Socialite\Facades\Socialite; use App\Models\User; use Carbon\CarbonImmutable; use JWTAuth; use JWTFactory; use GuzzleHttp; use Str; class SocialiteController extends Controller { /* public function getAuthIos(Request $request) { $url = 'https://appleid.apple.com/auth/token'; $client_id = 'com.app.tattooscalculator'; if($request->has('code')){ $code = $request->code; }else{ $code = 'c29197d4ec3064bb6a1b679c861f55da9.0.rwyw.BuMWYnoeMtoEprgXfG5-Ow'; } $file = file_get_contents(public_path() . '/AuthKey_6G7D3T3WP5.p8'); $key = openssl_pkey_get_private($file); $now = CarbonImmutable::now(); if ($key === false) { dump(openssl_error_string()); } $claims = array( 'iss' => 'TTB65U3XQ6', 'iat' => $now->getTimestamp(), 'exp' => $now->addDays(1)->getTimestamp(), 'aud' => 'https://appleid.apple.com', 'sub' => 'com.app.tattooscalculator', ); $heads = [ "alg"=> "ES256", 'kid' => '6G7D3T3WP5', "typ"=>"JWT" ]; //openssl_private_encrypt(json_encode($claims), $crypted, $key, OPENSSL_PKCS1_PADDING); $payload = JWTFactory::claims($claims)->make(); $cript = JWTAuth::encode($payload, $key, $heads); //dd(urlencode($cript->get())); $json_data = [ "client_id" => $client_id, "client_secret" =>urlencode($cript->get()), "code" => $code, "grant_type" => "authorization_code", ]; //$client = new GuzzleHttp\Client([ 'headers' => ['content-type'=>'application/x-www-form-urlencoded']]); //$request = $client->post($url, ['form_params'=>$json_data]); //$response = $request->send(); $ch = curl_init($url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/x-www-form-urlencoded")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); //echo $response; curl_close($ch); return response()->json($response); } */ // methods for iOs public function getFacebookIos(Request $request) { abort_if(!$request->access_token, 403, 'access_token not found'); //return redirect($url); $url = 'https://graph.facebook.com/me?fields=id,name,email,picture&access_token=' . $request->access_token; try { $json = json_decode(file_get_contents($url), true); } catch (\Exception $e) { return response()->json([ 'message' => 'error with token' ]); } if (!isset($json['id'])) { return response()->json([ 'message' => 'error with facebook_id' ]); } $user = User::where('facebook_id', $json['id'])->first(); if (!$user) { $user = User::create([ 'facebook_id' => $json['id'], 'name' => $json['name'], 'email' => isset($json['email']) ? $json['email'] : '', 'password' => bcrypt(11111111) ]); } $token = JWTAuth::fromUser($user); $response = [ 'token' => $token, 'token_type' => 'Bearer', 'user' => $user, ]; return response()->json($response, 201); } // methods for web public function getFacebook() { return Socialite::driver('facebook')->redirect(); } public function getFacebookCallback() { $user = Socialite::driver('facebook')->stateless()->user(); //return response()->json($user); return $this->registerOrLogin($user); } private function registerOrLogin($userSocial = null) { abort_if(!$userSocial->email, 403, 'В запросе отсутствует email'); $user = User::where('email', $userSocial->email)->first(); if (!$user) { $password = User::generatePassword(); $user = User::create( [ 'name' => $userSocial->name, 'email' => $userSocial->email ?? '', 'password' => User::createPassword($password), ] ); } $token = JWTAuth::fromUser($user); $response = [ 'token' => $token, 'token_type' => 'Bearer', 'user' => $user, ]; return response()->json($response, 201); } }