Skip to content

Commit 85b646f

Browse files
committed
✨ feat: Integrar login com Google, adicionando controlador e ajustes na view de login
1 parent dc668f0 commit 85b646f

File tree

5 files changed

+282
-2
lines changed

5 files changed

+282
-2
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Models\User;
6+
use Illuminate\Support\Facades\Auth;
7+
use Illuminate\Http\Request;
8+
use App\Models\UsuarioAcao;
9+
use Google_Client;
10+
11+
class GoogleLoginController extends Controller
12+
{
13+
public function handleGoogleCallback(Request $request)
14+
{
15+
$client = new Google_Client(['client_id' => env('GOOGLE_CLIENT_ID')]);
16+
$id_token = $request->input('credential');
17+
$google_user = $client->verifyIdToken($id_token);
18+
19+
if (!$google_user) {
20+
return response()->json(['error' => 'Token inválido',], 401);
21+
}
22+
23+
$user = User::where('Login', $google_user['email'])->first();
24+
if ($user && !$user->is_migrated) {
25+
$user->Senha = bcrypt($user->Senha);
26+
$user->is_migrated = true;
27+
$user->save();
28+
}
29+
if ($user && !$user->IDGoogle) {
30+
$user->IDGoogle = $google_user['sub'];
31+
$user->Foto = $google_user['picture'] ?? null;
32+
$user->save();
33+
}
34+
if (!$user) {
35+
return redirect()->route('usuario.create')->with('erro', 'Usuário não encontrado, faça seu cadastro!');
36+
}
37+
38+
Auth::login($user);
39+
UsuarioAcao::registrarAcao($user->IDUsuario,'Acessou com conta google');
40+
41+
$request->session()->regenerate();
42+
return redirect()->intended('/dashboard');
43+
}
44+
}

app/Http/Middleware/VerifyCsrfToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ class VerifyCsrfToken extends Middleware
1212
* @var array<int, string>
1313
*/
1414
protected $except = [
15-
//
15+
'google/callback',
1616
];
1717
}

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"license": "MIT",
77
"require": {
88
"php": "^8.2",
9+
"google/apiclient": "^2.18",
910
"guzzlehttp/guzzle": "^7.2",
1011
"laravel/framework": "^11.0",
1112
"laravel/sanctum": "^4.0",

composer.lock

Lines changed: 224 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/views/usuario/login.blade.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@extends('master')
22
@section('title', 'Login')
33
@section('conteudo')
4+
<script src="https://accounts.google.com/gsi/client" async></script>
45
<div class="wrapper">
56
<section class="login-content">
67
<div class="row align-items-center m-0 bg-white">
@@ -47,6 +48,17 @@
4748
<span>Cadastre aqui.</span>
4849
</a>
4950
</div>
51+
<div>
52+
<div id="g_id_onload" data-client_id="{{ env('GOOGLE_CLIENT_ID') }}"
53+
data-context="signin" data-ux_mode="popup"
54+
data-login_uri="{{ env('GOOGLE_REDIRECT_URL') }}" data-auto_prompt="false">
55+
</div>
56+
57+
<div class="g_id_signin" data-type="standard" data-shape="rectangular"
58+
data-theme="filled_blue" data-text="signin_with" data-size="large"
59+
data-locale="pt-BR" data-logo_alignment="left">
60+
</div>
61+
</div>
5062
</div>
5163
</div>
5264
</div>

0 commit comments

Comments
 (0)