Skip to content

Commit

Permalink
Merge pull request #14 from Crudzaso/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
DiegoAndresRamirez authored Nov 3, 2024
2 parents 3f651cc + 7acfff9 commit 1d75f3d
Show file tree
Hide file tree
Showing 13 changed files with 827 additions and 149 deletions.
9 changes: 9 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,12 @@ AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

VITE_APP_NAME="${APP_NAME}"

GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
GITHUB_REDIRECT_URL=your-github-redirect-url


GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GOOGLE_REDIRECT_URI=your-google-redirect-uri
102 changes: 102 additions & 0 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Laravel\Socialite\Facades\Socialite;
use App\Models\User;

class AuthController extends Controller
{
public function redirectToGitHub()
{
return Socialite::driver('github')->redirect();
}

public function handleGitHubCallback()
{
try {
$githubUser = Socialite::driver('github')->user();

// Verifica los datos recibidos
Log::info('Usuario de GitHub: ', [
'id' => $githubUser->id,
'nickname' => $githubUser->nickname,
'name' => $githubUser->name,
'email' => $githubUser->email,
'avatar' => $githubUser->avatar,
]);

// Encuentra o crea un usuario
$user = User::firstOrCreate([
'email' => $githubUser->email,
], [
'name' => $githubUser->name ?: 'Nombre por defecto',
'github_id' => $githubUser->id,
'lastname' => 'Apellidos por defecto',
'password' => bcrypt('random_password'),
'phone_number' => 'Número por defecto',
'document' => '00000000',
'document_type' => 'CC',
]);

// Inicia sesión al usuario
Auth::login($user);

return redirect('/dashboard');
} catch (\Exception $e) {
// Registra el error
Log::error('Error en la autenticación de GitHub: ' . $e->getMessage());
return redirect('/')->with('error', 'No se pudo iniciar sesión con GitHub.');
}
}

public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}

public function handleGoogleCallback()
{
try {
Log::info('Entrando en el callback de Google.');

// Obtener el usuario de Google
$googleUser = Socialite::driver('google')->user();

Log::info('Usuario de Google obtenido', [
'email' => $googleUser->getEmail(),
'name' => $googleUser->getName(),
]);

// Buscar o crear un usuario en tu base de datos
$authUser = User::firstOrCreate(
['email' => $googleUser->getEmail()],
[
'name' => $googleUser->getName(),
'google_id' => $googleUser->getId(), // Guarda el google_id
'lastname' => 'Apellidos por defecto', // Asegúrate de que este campo exista
'password' => 'sadsadasd', // Generar una contraseña aleatoria
'phone_number' => 'Número por defecto', // Asegúrate de que este campo exista
'document' => uniqid(), // O una lógica para generar un documento único
'document_type' => 'CC', // Asegúrate de que este campo exista
]
);

Log::info('Usuario autenticado o creado', [
'authUser' => $authUser,
]);

// Iniciar sesión con el usuario autenticado
Auth::login($authUser, true);

// Redirigir a la página deseada
return redirect()->route('dashboard'); // Cambia a la ruta que desees
} catch (\Exception $e) {
// Registra el error
Log::error('Error en la autenticación de Google: ' . $e);
return redirect('/')->with('error', 'No se pudo iniciar sesión con Google.');
}
}
}
8 changes: 3 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements MustVerifyEmail
class User extends Authenticatable
{
use HasApiTokens;

/** @use HasFactory<\Database\Factories\UserFactory> */
use HasFactory;
use HasProfilePhoto;
use Notifiable;
Expand All @@ -33,7 +30,8 @@ class User extends Authenticatable implements MustVerifyEmail
'phone_number',
'email',
'password',

'google_id', // Agrega este campo
'github_id', // Si también estás usando GitHub, agrega este campo
];

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"laravel/framework": "^11.9",
"laravel/jetstream": "^5.3",
"laravel/sanctum": "^4.0",
"laravel/socialite": "^5.16",
"laravel/tinker": "^2.9",
"nwidart/laravel-modules": "^11.1",

"tightenco/ziggy": "^2.0"
},
"require-dev": {
Expand Down
Loading

0 comments on commit 1d75f3d

Please sign in to comment.