Created
November 22, 2023 08:10
-
-
Save lechuhuuha/6f4a7c7390c5a630b0e557293bde3dd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function showLoginForm(\Illuminate\Http\Request $request) | |
{ | |
$sso_url = env('SSO_URL', '#') . '?callback_url=' . action('Controller@loginSSO'); | |
return view('auth.login', ['sso_url' => $sso_url]); | |
} | |
// em cần có 1 function ở trong 1 class controller rồi tạo 1 route với controller đấy | |
Route::get('login-sso', 'Controller@loginSSO'); | |
function loginSSO(\Illuminate\Http\Request $request) | |
{ | |
if (!isset($request->code) || !$this->isSSOEnable()) { | |
return redirect()->intended('/'); | |
} | |
try { | |
$decoded = JWT::decode(trim($request->code), env('PHP_JWT_KEY'), array('HS256')); | |
$account = isset($decoded->account) && isJsonString($decoded->account) | |
? json_decode($decoded->account, true) : []; | |
if (isset($account['email'])) { | |
$user = User::where('email', '=', trim($account['email']))->first(); | |
if (!$user || !isset($user->email)) { | |
// Create Contact | |
$contact = new Contact(); | |
$contact->first_name = $account['fullname']; | |
$contact->last_name = ''; | |
$contact->email = $account['email']; | |
$contact->company = isset($account['company']) ? $account['company'] : ''; | |
$contact->address_1 = isset($account['address']) ? $account['address'] : 'Hà Nội'; | |
$contact->address_2 = ''; | |
$contact->city = ''; | |
$contact->phone = isset($account['phone']) ? $account['phone'] : ''; | |
$contact->country_id = isset($account['country_id']) ? $account['country_id'] : 228; | |
$contact->zip = isset($account['zip']) ? $account['zip'] : 100000; | |
$contact->save(); | |
// Create User | |
$user = new User(); | |
$user->email = $account['email']; | |
$user->phone = $account['phone']; | |
$user->activated = true; | |
// Gen password random | |
$hashed_random_password = str_random(8); | |
$user->password = bcrypt($hashed_random_password); | |
$user->save(); | |
// Create Customer | |
$customer = new Customer(); | |
$customer->user_id = $user->id; | |
$customer->contact_id = $contact->id; | |
$customer->first_name = $account['fullname']; | |
$customer->last_name = ''; | |
$customer->timezone = 'Asia/Ho_Chi_Minh'; | |
$customer->status = 'active'; | |
$customer->language_id = Setting::get('default_language'); | |
$customer->save(); | |
LaravelLog::info('Create new user and customer: ' . $account['email']); | |
// Validate plan name | |
$planname = (isset($body->plan_name)) ? $body->plan_name : 'Free'; | |
$plan = DB::table('plans')->where('name', $planname)->first(); | |
if (isset($plan->id)) { | |
// assign Plan | |
$plan = DB::table('plans')->where('name', 'Free')->first(); | |
$customer = Customer::findByUid($customer->uid); | |
$plan = Plan::findByUid($plan->uid); | |
$customer->assignPlan($plan); | |
LaravelLog::info('Assign `' . $planname . '` plan to customer success: ' . $account['email']); | |
} else { | |
LaravelLog::info('Assign `' . $planname . '` plan to customer Faild. Service Plan not available!'); | |
} | |
} | |
// Login auth | |
\Auth::login($user); | |
if (isset($user->dc) && $user->dc == 's1') { | |
return redirect(env('APPV1_URL')); | |
} | |
return redirect('/'); | |
} | |
LaravelLog::info('Login via iNET SSO Account success. email: ' . $account['email']); | |
} catch (\Exception $ex) { | |
LaravelLog::info('Login via iNET SSO Account failed. token: ' . $request->code); | |
return redirect()->intended('/'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment