Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.57 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers\Api\Auth;
  4.  
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use App\User;
  8.  
  9. class LoginController extends Controller
  10. {
  11. private $ig;
  12. private $username;
  13. private $password;
  14.  
  15. public function __construct()
  16. {
  17. $this->ig = new \InstagramAPI\Instagram();
  18. $this->username = "nastyaantipovaa";
  19. $this->password = "789456123qweRTY";
  20. }
  21.  
  22. public function login(Request $request)
  23. {
  24.  
  25. $this->ig->login($this->username, $this->password);
  26.  
  27. $isAuth = false;
  28.  
  29. try {
  30. $userInfo = $this->ig->people->getInfoByName($request->username)->getUser();
  31.  
  32. $isAuth = true;
  33.  
  34. //Я предполагаю, что проверку есть ли такой акк в бд - надо делать тут.
  35. //Так как будет потом проверка на пароль (если пользователь указал)
  36. //Попробуй разобраться со встроенной. Пока не сделал - отложил. 1.03
  37. //Бери recipe как пример по авторизации
  38.  
  39.  
  40. $data = [
  41. "username"=> $userInfo->getUsername(),
  42. "fullName" => $userInfo->getFullName()
  43. ];
  44.  
  45. $this->create($data); //надо сделать проверку, существует ли аккаунт
  46.  
  47. } catch (Exception $e) {}
  48. finally {
  49. return response()
  50. ->json([
  51. 'is_auth' => $isAuth,
  52. 'message' => "",
  53. ], 200);
  54. }
  55.  
  56. }
  57.  
  58. public function verify(Request $request)
  59. {
  60.  
  61. $this->ig->login($this->username, $this->password);
  62.  
  63. $mediaId = $this->ig->media->getMediaId("https://www.instagram.com/p/BuR2C3eHn96/");
  64.  
  65. $likes = $this->ig->media->getLikers($mediaId)->getUsers();
  66.  
  67. $isLike = false;
  68.  
  69. foreach ($likes as $i){
  70. if($i->getUsername() == $request->username){
  71. $isLike = true;
  72. break;
  73. }
  74. }
  75.  
  76. return response()
  77. ->json([
  78. 'is_like' => $isLike,
  79. ], 200);
  80. }
  81.  
  82. protected function create(array $data)
  83. {
  84. return User::firstOrCreate([
  85. 'username' => $data["username"],
  86. 'instagram_link' => "https://www.instagram.com/". $data["username"],
  87. 'full_name' => $data["fullName"],
  88. ]);
  89. }
  90.  
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement