Skip to content
This repository has been archived by the owner on Jul 13, 2019. It is now read-only.

Commit

Permalink
Merge pull request #5 from prateekkarki/develop
Browse files Browse the repository at this point in the history
add users and auth
  • Loading branch information
prateekkarki authored Dec 10, 2018
2 parents cf6fad6 + 77d8895 commit abfa0b5
Show file tree
Hide file tree
Showing 17 changed files with 655 additions and 19 deletions.
63 changes: 63 additions & 0 deletions app/Http/Controllers/Auth/AuthController.php
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
]);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LoginController extends Controller
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/';

/**
* Create a new controller instance.
Expand Down
22 changes: 9 additions & 13 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Models\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Validator;

class RegisterController extends Controller
{
Expand All @@ -24,11 +24,11 @@ class RegisterController extends Controller
use RegistersUsers;

/**
* Where to redirect users after registration.
* Where to redirect users after login / registration.
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/';

/**
* Create a new controller instance.
Expand All @@ -43,25 +43,21 @@ public function __construct()
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6', 'confirmed'],
'name' => 'required|string|max:255|alpha_dash',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
protected function create(array $data): User
{
return User::create([
'name' => $data['name'],
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class ResetPasswordController extends Controller
use ResetsPasswords;

/**
* Where to redirect users after resetting their password.
* Where to redirect users after reset password.
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/';

/**
* Create a new controller instance.
Expand Down
6 changes: 3 additions & 3 deletions app/Http/Controllers/Auth/VerificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\VerifiesEmails;
use Illuminate\Routing\Controller;

class VerificationController extends Controller
{
Expand All @@ -14,7 +14,7 @@ class VerificationController extends Controller
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
| be resent if the user did not receive the original email message.
|
*/

Expand All @@ -25,7 +25,7 @@ class VerificationController extends Controller
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/';

/**
* Create a new controller instance.
Expand Down
28 changes: 28 additions & 0 deletions app/Http/Controllers/HomeController.php
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');
}
}
57 changes: 57 additions & 0 deletions app/Http/Controllers/UserController.php
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'));
}
}
41 changes: 41 additions & 0 deletions app/Http/Controllers/UserPasswordController.php
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'));
}
}
38 changes: 38 additions & 0 deletions app/Http/Controllers/UserTokenController.php
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'));
}
}
Loading

0 comments on commit abfa0b5

Please sign in to comment.