validate([ 'username' => 'required|string|alpha_dash', 'password' => 'required|string', ]); $isAuth = Auth::guard('customer')->attempt(['username' => $request->username, 'password' => $request->password]); if ($isAuth) { return redirect()->route('home.index'); } return redirect()->route('customer.login') ->with('message', ['type' => 'error', 'message' => 'invalid credentials']); } public function signin_google() { $config = new Config( env('GOOGLE_CLIENT_ID'), env('GOOGLE_CLIENT_SECRET'), route('customer.login.callback_google') ); return Socialite::driver('google') ->setConfig($config) ->redirect(); } public function callback_google() { $config = new Config( env('GOOGLE_CLIENT_ID'), env('GOOGLE_CLIENT_SECRET'), route('customer.login.callback_google') ); $user = Socialite::driver('google') ->setConfig($config) ->user(); $customer = Customer::where('google_id', $user->id)->first(); if ($customer == null) { DB::beginTransaction(); $basic = CustomerLevel::where('key', CustomerLevel::BASIC)->first(); $customer = Customer::create([ 'fullname' => $user->name, 'name' => $user->nickname, 'email' => $user->email, 'username' => Str::random(10), 'google_id' => $user->id, 'google_oauth_response' => json_encode($user), 'customer_level_id' => $basic->id, ]); CustomerLevelHistory::create([ 'customer_id' => $customer->id, 'customer_level_id' => $basic->id, 'date_time' => now(), ]); DB::commit(); } Auth::guard('customer')->loginUsingId($customer->id); return redirect()->route('home.index'); } public function register() { return inertia('Home/Auth/Register'); } public function store(Request $request) { $request->validate([ 'fullname' => 'required|string', 'name' => 'required|string', 'address' => 'required|string', 'phone' => 'required|string|numeric', 'username' => 'required|string|min:5|alpha_dash|unique:customers,username', 'password' => 'required|string|min:8|confirmed', ]); DB::beginTransaction(); $basic = CustomerLevel::where('key', CustomerLevel::BASIC)->first(); $customer = Customer::create([ 'fullname' => $request->fullname, 'name' => $request->name, 'address' => $request->address, 'phone' => $request->phone, 'username' => $request->username, 'password' => bcrypt($request->password), 'customer_level_id' => $basic->id, ]); CustomerLevelHistory::create([ 'customer_id' => $customer->id, 'customer_level_id' => $basic->id, 'date_time' => now(), ]); DB::commit(); Auth::guard('customer')->loginUsingId($customer->id); return redirect()->route('home.index'); } public function destroy() { Auth::logout(); return redirect()->route('customer.login') ->with('message', ['type' => 'success', 'message' => 'you are logged out, see you next time']); } }