View more: https://laravel.com/docs/9.x/socialite
1. Create api google:
2. Create OAuth client ID
- Select Web Application
- Name
- Link callback: https://demo.com/gmail/oauth/gmail/callback
4. Save file to env
GOOGLE_CLIENT_ID="10192090312319-sqt71csp05s9hrhgnul3f93finme0cg3.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="GOCSPX-2L9X0bHlYz0123123U8Yr123123"
GOOGLE_REDIRECT_URL="http://localhost:8080/login/google/callback"
5. Edit file AuthController.php:
1. Create api google:
Google Cloud Platform
Google Cloud Platform lets you build, deploy, and scale applications, websites, and services on the same infrastructure as Google.
console.cloud.google.com
- Select Web Application
- Name
- Link callback: https://demo.com/gmail/oauth/gmail/callback
4. Save file to env
GOOGLE_CLIENT_ID="10192090312319-sqt71csp05s9hrhgnul3f93finme0cg3.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET="GOCSPX-2L9X0bHlYz0123123U8Yr123123"
GOOGLE_REDIRECT_URL="http://localhost:8080/login/google/callback"
5. Edit file AuthController.php:
Mã:
<?php
namespace App\Http\Controllers\Admin\Auth;
use App\Http\Controllers\Controller;
use App\Models\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;
use Exception;
use App\Repositories\UserRepositoryInterface;
use App\Http\Requests\UpdateAuthRequest;
class AuthController extends Controller
{
/**
* @var UserRepositoryInterface|\App\Repositories\Repository
*/
public function __construct(UserRepositoryInterface $userRepository)
{
$this->userRepository = $userRepository;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function getlogin()
{
// $user = new \App\Models\User;
// $user->name = 'admin';
// $user->email = 'admin@admin.com';
// $user->password = \Hash::make('123456');
// $user->save();
return view('admin/users/login', [
'title' => 'Login',
]);
}
public function postLogin(Request $request)
{
$this->validate($request, [
'email' => 'required|email:filter',
'password' => 'required'
]);
$user = $this->userRepository->findUserByEmail($request->input('email'));
if($user && $user->isActive){
if (Auth::attempt([
'email' => $request->input('email'),
'password' => $request->input('password'),
// 'level' => 1
], $request->input('remember'))) {
return redirect('/');
} else {
return redirect('/login')->with('error', 'Email or password wrong!');
}
}else{
return redirect('/login')->with('error', 'Email or password wrong!');
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function logout()
{
Auth::logout();
return redirect('/login');
}
//Google login
public function redirectGoogle()
{
return Socialite::driver('google')->redirect();
}
public function handleGoogleCallback()
{
$googleUser = Socialite::driver('google')->user();
//All providers...
// $googleUser->getId();
// $googleUser->getNickname();
// $googleUser->getName();
// $googleUser->getEmail();
// $googleUser->getAvatar());
//return home after login
try {
$existUser = Users::where('email', $googleUser->email)->first();
if ($existUser) {
Auth::loginUsingId($existUser->id);
} else {
return redirect('/login')->with('error', 'Email not exits!');
// $user = new User;
// $user->name = $googleUser->name;
// $user->email = $googleUser->email;
// $user->password = md5(rand(1,10000));
// $user->save();
// Auth::loginUsingId($user->id);
}
return redirect()->to('/');
} catch (Exception $e) {
return $e;
}
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function edit()
{
//Auth::logout();
$auth = Auth::user();
$teams = $this->userRepository->getAllTeams();
// dd($auth);
return view('admin.auth.edit', [
'title' => 'Edit profile',
'auth' => $auth,
'teams' => $teams,
]);
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\UpdateAuthRequestRequest $request
* @return \Illuminate\Http\Response
*/
public function update(UpdateAuthRequest $request)
{
$data = $request->all();
//check have password and have checkbox
if(isset($data['password']) && $request->editPassword == 'on') {
$data = $request->only(['name','password']);
$data['password'] = bcrypt($data['password']);
} else {
$data = $request->only(['name']);
}
$userID = Auth::user()->id;
$auth = $this->userRepository->find($userID);
if($auth){
$auth_detail = $this->userRepository->update($userID, $data);
if ($auth_detail) {
return redirect()->back()->with('success', 'Update user detail Success!');
} else {
return redirect()->back()->with('error', 'Update user detail has something wrong!!');
}
}
else{
return redirect()->back()->with('error', 'user not found!');
}
}
}
Chỉnh sửa lần cuối: