Skip to content

Commit

Permalink
feat: added auth via oath app closes #134
Browse files Browse the repository at this point in the history
  • Loading branch information
stevan06v committed Mar 19, 2024
1 parent 6b266f8 commit 511e857
Show file tree
Hide file tree
Showing 15 changed files with 567 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .env.dev.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ DB_DATABASE=propromo
DB_USERNAME=postgres
DB_PASSWORD=propromo

GITHUB_CLIENT_ID=b6e2c09ada28dae5186e
GITHUB_CLIENT_SECRET=ff191c19a886b5e159e7882d0511d6e217f37d9a
GITHUB_CALLBACK_URI=http://propromo.test/auth/github/callback

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
Expand Down
4 changes: 4 additions & 0 deletions .env.docker.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ DB_DATABASE=propromo
DB_USERNAME=postgres
DB_PASSWORD=propromo

GITHUB_CLIENT_ID=b6e2c09ada28dae5186e
GITHUB_CLIENT_SECRET=ff191c19a886b5e159e7882d0511d6e217f37d9a
GITHUB_CALLBACK_URI=http://propromo.test/auth/github/callback

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
Expand Down
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ DB_DATABASE=propromo
DB_USERNAME=postgres
DB_PASSWORD=propromo

GITHUB_CLIENT_ID=b6e2c09ada28dae5186e
GITHUB_CLIENT_SECRET=ff191c19a886b5e159e7882d0511d6e217f37d9a
GITHUB_CALLBACK_URI=http://propromo.test/auth/github/callback

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
Expand Down
44 changes: 44 additions & 0 deletions app/Http/Controllers/GithubController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Laravel\Socialite\Facades\Socialite;
use Str;

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

/**
* @throws Exception
*/
public function callback()
{
try {
$user = Socialite::driver('github')->user();

$git_user = User::updateOrCreate([
'github_id' => $user->id
], [
'name' => $user->name,
'nickname' => $user->nickname,
'email' => $user->email,
'github_token' => $user->token,
'auth_type' => 'GITHUB',
'password' => Hash::make(Str::random(10))
]);
Auth::login($git_user);

return redirect(route('home.index'));
} catch (Exception $e) {
throw new Exception("Error occured while trying to authorize via github...".$e->getMessage());
}
}
}
2 changes: 2 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class User extends Authenticatable
protected $fillable = [
'name',
'email',
'github_id',
'auth_type',
'password',
];

Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/socialite": "^5.12",
"laravel/tinker": "^2.8",
"livewire/livewire": "^3.4",
"livewire/volt": "^1.6",
"robsontenorio/mary": "^1.25"
"robsontenorio/mary": "^1.25",
"spatie/laravel-markdown": "^2.5",
"ueberdosis/pandoc": "^0.9.0"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^3.0",
Expand Down
Loading

0 comments on commit 511e857

Please sign in to comment.