Advertisement
langbung01

Untitled

Feb 27th, 2019
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.72 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\Http\Controllers\Auth;
  4.  
  5. // use Auth;
  6. use App\User;
  7. use App\Refcode;
  8. use GuzzleHttp\Client;
  9.  
  10. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Routing\Controller;
  13. use App\Http\Requests\LoginRequest;
  14. use Illuminate\Http\Request;
  15. use Webpatser\Uuid\Uuid;
  16. use Carbon\Carbon;
  17.  
  18. class LoginController extends Controller
  19. {
  20.     use AuthenticatesUsers;
  21.     /**
  22.      * Where to redirect users after login.
  23.      *
  24.      * @var string
  25.      */
  26.     protected $redirectTo = '/ghb';
  27.  
  28.     /**
  29.      * Create a new controller instance.
  30.      *
  31.      * @return void
  32.      */
  33.     public function __construct()
  34.     {
  35.         $this->middleware('guest')->except('logout');
  36.     }
  37.  
  38.     public function showLoginForm()
  39.     {
  40.         if(Auth::check())
  41.         {
  42.             return redirect('/GHbankExpo2018');
  43.         }
  44.         else
  45.         {
  46.             return redirect('/GHbankExpo2018/login');
  47.         }
  48.     }
  49.    
  50.     public function postProcess(LoginRequest $request){
  51.        
  52.         $username = $request->username;
  53.         $password = $request->password;
  54.         if(Auth::attempt(['username' => $username, 'password'=> $password]))
  55.         {
  56.             return redirect()->to('/GHbankExpo2018');
  57.         }
  58.         else
  59.         {
  60.             return redirect()->back()->withErrors('ชื่อผู้หรือรหัสผ่านไม่ถูกต้อง');
  61.         }
  62.     }
  63.  
  64.     public function logout(Request $request){
  65.         $request->session()->flush();
  66.         Auth::logout();
  67.         return redirect('/GHbankExpo2018/login');
  68.     }
  69.  
  70.     public function loginwithoneid(Request $request)
  71.     {
  72.         $username = $request->input('username');
  73.         $password = $request->input('password');
  74.         $authenResult = $this->getAccessToken($username, $password);
  75.         if (!$authenResult) {
  76.            return redirect()->back()->withErrors('ชื่อผู้หรือรหัสผ่านไม่ถูกต้อง');
  77.         }
  78.         $this->logingIn($username);
  79.     }
  80.  
  81.     private function getAccessToken($username, $password)
  82.     {
  83.         $client = new Client(['base_uri' => env('BACKEND_IP'), 'verify' => false]);
  84.         $response = $client->post('/oauth/token', [
  85.             'form_params' => [
  86.                 'grant_type' => 'password',
  87.                 'client_id' => env('CLIENT_ID'),
  88.                 'client_secret' => env('CLIENT_SECRET'),
  89.                 'username' => "$username",
  90.                 'password' => "$password",
  91.                 'scope' => '',
  92.             ],
  93.             'http_errors' => false,
  94.         ]);
  95.         if ($response->getStatusCode() == 200) {
  96.             $accessToken = json_decode((string) $response->getBody(), true)['access_token'];
  97.             $this->getUserAndCreate($accessToken, $username, $password);
  98.             return true;    
  99.         }
  100.         return $response->getStatusCode() == 200 ? true : false;
  101.     }
  102.  
  103.     private function logingIn($username)
  104.     {
  105.         $userModel = User::where('username', $username)->first();
  106.         Auth::login($userModel);
  107.         $this->sendLoginResponse(\request())->throwResponse();
  108.     }
  109.  
  110.     private function getUserAndCreate($accessToken, $username, $password)
  111.     {
  112.         $client = new Client(['base_uri' => env('BACKEND_IP'), "verify" => false]);
  113.         $response = $client->get('/api/account', [
  114.             'headers' => ['Authorization' => 'Bearer ' . $accessToken],
  115.             'http_errors' => false,
  116.         ]);
  117.         $jsonResponse = json_decode($response->getBody(), true);
  118.         $userModel = User::where('username', "$username")->first();
  119.         User::where('username', "$username")->update(["updated_at" => Carbon::now()]);
  120.         if (!$userModel)
  121.         {
  122.             $userModel = new User();
  123.             $userModel->id = Uuid::generate()->string;
  124.             $userModel->username = $username;
  125.             $userModel->password = bcrypt($password);
  126.             $userModel->title = $jsonResponse['account_title_th'];
  127.             $userModel->id_passport = $jsonResponse['id_card_num'];
  128.             $userModel->birth_date = $jsonResponse['birth_date'];
  129.             $userModel->phone = $jsonResponse['mobile'][0]['mobile_no'];
  130.             $userModel->email = $jsonResponse['email'][0]['email'];
  131.             $userModel->name = $jsonResponse['first_name_eng'];
  132.             $userModel->surname = $jsonResponse['last_name_eng'];
  133.             $userModel->nameTH = $jsonResponse['first_name_th'];
  134.             $userModel->surnameTH = $jsonResponse['last_name_th'];
  135.             $userModel->has_onecard = 'I';
  136.             $userModel->status = 'one_id';
  137.             $userModel->save();
  138.            
  139.         }
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement