This repository has been archived by the owner on Jul 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from prateekkarki/develop
add users and auth
- Loading branch information
Showing
17 changed files
with
655 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Models\User; | ||
use Auth; | ||
use Socialite; | ||
|
||
class AuthController extends Controller | ||
{ | ||
/** | ||
* Redirect the user to the GitHub authentication page. | ||
* | ||
* @return Response | ||
*/ | ||
public function redirectToProvider($provider) | ||
{ | ||
return Socialite::driver($provider)->redirect(); | ||
} | ||
|
||
/** | ||
* Obtain the user information from provider. | ||
* | ||
* @return Response | ||
*/ | ||
public function handleProviderCallback($provider) | ||
{ | ||
try { | ||
$user = Socialite::driver($provider)->stateless()->user(); | ||
} catch (Exception $e) { | ||
return redirect()->route('login'); | ||
} | ||
|
||
$authUser = $this->findOrCreateUser($user, $provider); | ||
|
||
Auth::login($authUser, true); | ||
|
||
return redirect()->route('home')->withSuccess(__('auth.logged_in_provider', ['provider' => $provider])); | ||
} | ||
|
||
/** | ||
* Return user if exists; create and return if doesn't | ||
* | ||
* @param $user | ||
* @return User | ||
*/ | ||
private function findOrCreateUser($user, $provider) | ||
{ | ||
$authUser = User::where('provider_id', $user->id)->first(); | ||
|
||
if ($authUser) { | ||
return $authUser; | ||
} | ||
|
||
return User::create([ | ||
'name' => $user->name ?? $user->email, | ||
'email' => $user->email, | ||
'provider' => $provider, | ||
'provider_id' => $user->id | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use Illuminate\Http\Request; | ||
|
||
class HomeController extends Controller | ||
{ | ||
/** | ||
* Create a new controller instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->middleware('auth'); | ||
} | ||
|
||
/** | ||
* Show the application dashboard. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function index() | ||
{ | ||
return view('home'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\UsersRequest; | ||
use App\Models\Role; | ||
use App\Models\User; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Illuminate\View\View; | ||
|
||
class UserController extends Controller | ||
{ | ||
/** | ||
* Display the specified resource. | ||
*/ | ||
public function show(Request $request, User $user): View | ||
{ | ||
return view('users.show', [ | ||
'user' => $user, | ||
'posts_count' => $user->posts()->count(), | ||
'comments_count' => $user->comments()->count(), | ||
'likes_count' => $user->likes()->count(), | ||
'posts' => $user->posts()->withCount('likes', 'comments')->latest()->limit(5)->get(), | ||
'comments' => $user->comments()->with('post.author')->latest()->limit(5)->get() | ||
]); | ||
} | ||
|
||
/** | ||
* Show the form for editing the specified resource. | ||
*/ | ||
public function edit(): View | ||
{ | ||
$user = auth()->user(); | ||
|
||
$this->authorize('update', $user); | ||
|
||
return view('users.edit', [ | ||
'user' => $user, | ||
'roles' => Role::all() | ||
]); | ||
} | ||
|
||
/** | ||
* Update the specified resource in storage. | ||
*/ | ||
public function update(UsersRequest $request): RedirectResponse | ||
{ | ||
$user = auth()->user(); | ||
|
||
$this->authorize('update', $user); | ||
|
||
$user->update($request->validated()); | ||
|
||
return redirect()->route('users.edit')->withSuccess(__('users.updated')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Http\Requests\UserPasswordsRequest; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Support\Facades\Hash; | ||
use Illuminate\View\View; | ||
|
||
class UserPasswordController extends Controller | ||
{ | ||
/** | ||
* Show the form for editing the specified resource. | ||
*/ | ||
public function edit(): View | ||
{ | ||
$user = auth()->user(); | ||
|
||
$this->authorize('update', $user); | ||
|
||
return view('users.password', ['user' => $user]); | ||
} | ||
|
||
/** | ||
* Update password for the specified resource in storage. | ||
*/ | ||
public function update(UserPasswordsRequest $request): RedirectResponse | ||
{ | ||
$user = auth()->user(); | ||
|
||
$this->authorize('update', $user); | ||
|
||
$request->merge([ | ||
'password' => Hash::make($request->input('password')) | ||
]); | ||
|
||
$user->update($request->only('password')); | ||
|
||
return redirect()->route('users.password')->withSuccess(__('users.password_updated')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Token; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\View\View; | ||
|
||
class UserTokenController extends Controller | ||
{ | ||
/** | ||
* Show the form for editing the specified resource. | ||
*/ | ||
public function edit(): View | ||
{ | ||
$user = auth()->user(); | ||
|
||
$this->authorize('api_token', $user); | ||
|
||
return view('users.token', ['user' => $user]); | ||
} | ||
|
||
/** | ||
* Generate a personnal access token for the specified resource in storage. | ||
*/ | ||
public function update(): RedirectResponse | ||
{ | ||
$user = auth()->user(); | ||
|
||
$this->authorize('api_token', $user); | ||
|
||
$user->update([ | ||
'api_token' => Token::generate() | ||
]); | ||
|
||
return redirect()->route('users.token')->withSuccess(__('tokens.updated')); | ||
} | ||
} |
Oops, something went wrong.