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

Adding v5.6 Auth to support username, email. #5

Open
wants to merge 5 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 51 additions & 1 deletion app/Http/Controllers/Auth/LoginController.php
Expand Up @@ -3,7 +3,9 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{
Expand All @@ -16,7 +18,7 @@ class LoginController extends Controller
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
*/

use AuthenticatesUsers;

Expand All @@ -36,4 +38,52 @@ public function __construct()
{
$this->middleware('guest')->except('logout');
}

/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
$field = $this->field($request);

return [
$field => $request->get($this->username()),
'password' => $request->get('password'),
'active' => User::ACTIVE,
];
}

/**
* Determine if the request field is email or username.
*
* @param \Illuminate\Http\Request $request
* @return string
*/
public function field(Request $request)
{
$email = $this->username();

return filter_var($request->get($email), FILTER_VALIDATE_EMAIL) ? $email : 'username';
}

/**
* Validate the user login request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
protected function validateLogin(Request $request)
{
$field = $this->field($request);

$messages = ["{$this->username()}.exists" => 'The account you are trying to login is not activated or it has been disabled.'];

$this->validate($request, [
$this->username() => "required|exists:users,{$field},active," . User::ACTIVE,
'password' => 'required',
], $messages);
}
}
55 changes: 51 additions & 4 deletions app/Http/Controllers/Auth/RegisterController.php
Expand Up @@ -2,11 +2,14 @@

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use App\Notifications\UserActivate;
use App\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
Expand All @@ -19,7 +22,7 @@ class RegisterController extends Controller
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
*/

use RegistersUsers;

Expand Down Expand Up @@ -50,6 +53,7 @@ protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'username' => 'required|string|max:20|unique:users',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
Expand All @@ -63,10 +67,53 @@ protected function validator(array $data)
*/
protected function create(array $data)
{
return User::create([
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'token' => str_random(40) . time(),
]);

$user->notify(new UserActivate($user));

return $user;
}

/**
* Handle a registration request for the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$this->validator($request->all())->validate();

event(new Registered($user = $this->create($request->all())));

return redirect()->route('login')
->with(['success' => 'Congratulations! your account is registered, you will shortly receive an email to activate your account.']);
}

/**
* @param $token
*/
public function activate($token = null)
{
$user = User::where('token', $token)->first();

if (empty($user)) {
return redirect()->to('/')
->with(['error' => 'Your activation code is either expired or invalid.']);
}

$user->token = '';
$user->active = User::ACTIVE;

$user->save();

return redirect()->route('login')
->with(['success' => 'Congratulations! your account is now activated.']);
}
}
28 changes: 28 additions & 0 deletions app/Http/Controllers/HomeController.php
@@ -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');
}
}
64 changes: 64 additions & 0 deletions app/Notifications/UserActivate.php
@@ -0,0 +1,64 @@
<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class UserActivate extends Notification
{
use Queueable;

/**
* Create a new notification instance.
*
* @param $user
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}

/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}

/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->from(env('ADMIN_MAIL_ADDRESS'))
->subject('Activate Account!')
->greeting(sprintf('Hi, %s', $this->user->name))
->line('We just noticed that you created a new account. You will need to activate your account to sign in into this account.')
->action('Activate', route('activate', [$this->user->token]))
->line('Thank you for using our application!');
}

/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
}
7 changes: 5 additions & 2 deletions app/User.php
Expand Up @@ -2,20 +2,23 @@

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
use Notifiable;

const ACTIVE = 1;
const INACTIVE = 0;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'username', 'email', 'password', 'token', 'active',
];

/**
Expand Down
3 changes: 3 additions & 0 deletions database/migrations/2014_10_12_000000_create_users_table.php
Expand Up @@ -16,8 +16,11 @@ public function up()
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->string('token');
$table->integer('active')->default(0);
$table->rememberToken();
$table->timestamps();
});
Expand Down
78 changes: 78 additions & 0 deletions resources/views/auth/login.blade.php
@@ -0,0 +1,78 @@
@extends('layouts.app')

@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
@if(session()->has('success'))
<div class="alert alert-success" role="alert">
{{ session('success') }}
</div>
@elseif(session()->has('error'))
<div class="alert alert-danger" role="alert">
{{ session('error') }}
</div>
@endif
<div class="card">
<div class="card-header">{{ __('Login') }}</div>

<div class="card-body">
<form method="POST" action="{{ route('login') }}">
@csrf

<div class="form-group row">
<label for="email" class="col-sm-4 col-form-label text-md-right">{{ __('E-Mail / Username') }}</label>

<div class="col-md-6">
<input id="email" type="text" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required autofocus>

@if ($errors->has('email'))
<span class="invalid-feedback">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

<div class="col-md-6">
<input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

@if ($errors->has('password'))
<span class="invalid-feedback">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>

<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> {{ __('Remember Me') }}
</label>
</div>
</div>
</div>

<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>

<a class="btn btn-link" href="{{ route('password.request') }}">
{{ __('Forgot Your Password?') }}
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection