Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email Verify and Password Resets #15

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions .env.example

This file was deleted.

32 changes: 32 additions & 0 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;

class ForgotPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset emails and
| includes a trait which assists in sending these notifications from
| your application to your users. Feel free to explore this trait.
|
*/

use SendsPasswordResetEmails;

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
78 changes: 78 additions & 0 deletions app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Http\Controllers\Auth;

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

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

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

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}

/**
* 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'],
'username' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:6'],
]);
}

/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
$user = new User([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$user->save();
$user->sendEmailVerificationNotification();

return $user;
}
}
59 changes: 59 additions & 0 deletions app/Http/Controllers/Auth/ResetPasswordController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\PasswordResets;
use App\User;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class ResetPasswordController extends Controller
{
/*
|--------------------------------------------------------------------------
| Password Reset Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling password reset requests
| and uses a simple trait to include this behavior. You're free to
| explore this trait and override any methods you wish to tweak.
|
*/

public function reset(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required|confirmed|min:6'
]);
$check = PasswordResets::where('email',$request->email)->first();
if(Hash::check($request->token,$check->token)){
$user = User::where('email',$request->email)->whereNull('deleted_at')->update(['password' => Hash::make($request->password)]);
if($user){
PasswordResets::where('email',$request->email)->delete();
return redirect('login');
}
}

}
use ResetsPasswords;

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

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
}
55 changes: 55 additions & 0 deletions app/Http/Controllers/Auth/VerificationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\VerifiesEmails;
use Illuminate\Http\Request;

class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification 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.
|
*/

public function verify(Request $request)
{
if ($request->route('id') == $request->user()->getKey()) {
$user = User::where('id', $request->route('id'))
->update(['email_verified_at' => now()]);
event(new Verified($request->user()));
}
return redirect($this->redirectPath())->with('verified', true);
}


use VerifiesEmails;

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

/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:6,1')->only('verify', 'resend');
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ public function handleProviderCallback()
try {
/** @var User $createdUser */
$createdUser = User::firstOrCreate([
'github_id' => $user->getId(),
'email' => $user->getEmail()
], [
'name' => $user->getName(),
'email' => $user->getEmail(),
'email_verified_at' => new \DateTime(),
'username' => $user->getNickname(),
'password' => bcrypt(substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') , 0 , 10 )),
'github_id' => $user->getId(),
'avatar' => $user->getAvatar(),
]);
Expand Down
8 changes: 8 additions & 0 deletions app/Http/Controllers/TalksController.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,12 @@ public function approveAction(Request $request, Talk $talk): RedirectResponse

return redirect()->route('talks.show', $talk->slug);
}


public function votes($talk)
{
$votes = Vote::with('user')->where('talk_id',$talk->id)->get();
return view('talks.votes',compact('votes','talk'));

}
}
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,7 @@ class Kernel extends HttpKernel
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'role' => Role::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
];
}
11 changes: 11 additions & 0 deletions app/PasswordResets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PasswordResets extends Model
{
protected $table = "password_resets";
public $timestamps = false;
}
3 changes: 2 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -13,7 +14,7 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
//
Schema::defaultStringLength(191);
}

/**
Expand Down
11 changes: 8 additions & 3 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Watson\Validating\ValidatingTrait;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Authenticatable
class User extends Authenticatable implements MustVerifyEmail,CanResetPasswordContract
{
use Notifiable, SoftDeletes, ValidatingTrait;

Expand All @@ -20,8 +22,10 @@ class User extends Authenticatable
protected $fillable = [
'name',
'email',
'email_verified_at',
'username',
'github_id',
'password',
'avatar',
'bio',
'airport_code',
Expand All @@ -48,7 +52,7 @@ class User extends Authenticatable
*/
protected $dates = [
'last_login',
'deleted_at'
'deleted_at',
];

/**
Expand All @@ -58,7 +62,8 @@ class User extends Authenticatable
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email',
'username' => 'required|string|max:255|unique:users,username',
'github_id' => 'required|numeric|unique:users,github_id',
'password' => 'required|string|max:255',
'github_id' => 'numeric|unique:users,github_id',
'avatar' => 'nullable|string',
'bio' => 'nullable|string',
'airport_code' => 'nullable|size:3',
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"require-dev": {
"barryvdh/laravel-debugbar": "^3.1",
"filp/whoops": "~2.0",
"friendsofphp/php-cs-fixer": "^2.8",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0",
"phpunit/phpunit": "~6.0",
Expand Down
Loading