Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\Http\Controllers\Auth;
- // use Auth;
- use App\User;
- use App\Refcode;
- use GuzzleHttp\Client;
- use Illuminate\Foundation\Auth\AuthenticatesUsers;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Routing\Controller;
- use App\Http\Requests\LoginRequest;
- use Illuminate\Http\Request;
- use Webpatser\Uuid\Uuid;
- use Carbon\Carbon;
- class LoginController extends Controller
- {
- use AuthenticatesUsers;
- /**
- * Where to redirect users after login.
- *
- * @var string
- */
- protected $redirectTo = '/ghb';
- /**
- * Create a new controller instance.
- *
- * @return void
- */
- public function __construct()
- {
- $this->middleware('guest')->except('logout');
- }
- public function showLoginForm()
- {
- if(Auth::check())
- {
- return redirect('/GHbankExpo2018');
- }
- else
- {
- return redirect('/GHbankExpo2018/login');
- }
- }
- public function postProcess(LoginRequest $request){
- $username = $request->username;
- $password = $request->password;
- if(Auth::attempt(['username' => $username, 'password'=> $password]))
- {
- return redirect()->to('/GHbankExpo2018');
- }
- else
- {
- return redirect()->back()->withErrors('ชื่อผู้หรือรหัสผ่านไม่ถูกต้อง');
- }
- }
- public function logout(Request $request){
- $request->session()->flush();
- Auth::logout();
- return redirect('/GHbankExpo2018/login');
- }
- public function loginwithoneid(Request $request)
- {
- $username = $request->input('username');
- $password = $request->input('password');
- $authenResult = $this->getAccessToken($username, $password);
- if (!$authenResult) {
- return redirect()->back()->withErrors('ชื่อผู้หรือรหัสผ่านไม่ถูกต้อง');
- }
- $this->logingIn($username);
- }
- private function getAccessToken($username, $password)
- {
- $client = new Client(['base_uri' => env('BACKEND_IP'), 'verify' => false]);
- $response = $client->post('/oauth/token', [
- 'form_params' => [
- 'grant_type' => 'password',
- 'client_id' => env('CLIENT_ID'),
- 'client_secret' => env('CLIENT_SECRET'),
- 'username' => "$username",
- 'password' => "$password",
- 'scope' => '',
- ],
- 'http_errors' => false,
- ]);
- if ($response->getStatusCode() == 200) {
- $accessToken = json_decode((string) $response->getBody(), true)['access_token'];
- $this->getUserAndCreate($accessToken, $username, $password);
- return true;
- }
- return $response->getStatusCode() == 200 ? true : false;
- }
- private function logingIn($username)
- {
- $userModel = User::where('username', $username)->first();
- Auth::login($userModel);
- $this->sendLoginResponse(\request())->throwResponse();
- }
- private function getUserAndCreate($accessToken, $username, $password)
- {
- $client = new Client(['base_uri' => env('BACKEND_IP'), "verify" => false]);
- $response = $client->get('/api/account', [
- 'headers' => ['Authorization' => 'Bearer ' . $accessToken],
- 'http_errors' => false,
- ]);
- $jsonResponse = json_decode($response->getBody(), true);
- $userModel = User::where('username', "$username")->first();
- User::where('username', "$username")->update(["updated_at" => Carbon::now()]);
- if (!$userModel)
- {
- $userModel = new User();
- $userModel->id = Uuid::generate()->string;
- $userModel->username = $username;
- $userModel->password = bcrypt($password);
- $userModel->title = $jsonResponse['account_title_th'];
- $userModel->id_passport = $jsonResponse['id_card_num'];
- $userModel->birth_date = $jsonResponse['birth_date'];
- $userModel->phone = $jsonResponse['mobile'][0]['mobile_no'];
- $userModel->email = $jsonResponse['email'][0]['email'];
- $userModel->name = $jsonResponse['first_name_eng'];
- $userModel->surname = $jsonResponse['last_name_eng'];
- $userModel->nameTH = $jsonResponse['first_name_th'];
- $userModel->surnameTH = $jsonResponse['last_name_th'];
- $userModel->has_onecard = 'I';
- $userModel->status = 'one_id';
- $userModel->save();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement