Skip to content

Commit

Permalink
Merge pull request #53 from GDGAlgiers/dev
Browse files Browse the repository at this point in the history
Added Email verification
  • Loading branch information
Brivan-26 authored Oct 21, 2023
2 parents fc2c81f + ad48fee commit ff98ac9
Show file tree
Hide file tree
Showing 16 changed files with 276 additions and 101 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/API/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Auth\Events\Registered;

class AuthController extends BaseController
{
public function register(Request $request){
$validator = Validator::make($request->all(), [
'full_name' => 'required|string|unique:users,full_name',
'email' => 'required|email|unique:users,email',
'track' => 'required|string|exists:tracks,type',
'password' => 'required|string|min:6',
]);
Expand All @@ -33,13 +35,15 @@ public function register(Request $request){

$user = User::create([
'full_name' => $request->full_name,
'email' => $request->email,
'track_id' => $trackID,
'password' => Hash::make($request->password),
'role' => 'participant',
'points' => 0,
'ip' => $request->ip()
]);

event(new Registered($user));
$token = $user->createToken('arizona-platform')->plainTextToken;
$result = [
'user' => new ParticipantResource($user),
Expand Down
49 changes: 49 additions & 0 deletions app/Http/Controllers/VerifyEmailController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\EmailVerificationRequest;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

class VerifyEmailController extends Controller
{

public function sendVerificationEmail(Request $request)
{
if ($request->user()->hasVerifiedEmail()) {
return response()->json([
'success' => false,
'message' => 'email already verified'
], 400);
}

$request->user()->sendEmailVerificationNotification();

return response()->json([
'success' => true,
'message' => 'email verification link has been sent'
]);
}

public function verify(EmailVerificationRequest $request)
{
if ($request->user()->hasVerifiedEmail()) {
return response()->json([
'success' => false,
'message' => 'email already verified'
], 400);
}

if ($request->user()->markEmailAsVerified()) {
event(new Verified($request->user()));
}

return response()->json([
'success' => true,
'message' => 'email has been verified'
]);
}
}
7 changes: 4 additions & 3 deletions app/Http/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Mail;
use App\Mail\ParticipantAccountCreated;
use App\Mail\JudgeAccountCreated;

Class UserRepository {

Expand All @@ -26,6 +23,7 @@ public function create_participant($request) {
$response = [];
$validator = Validator::make($request->all(), [
'full_name' => 'required|unique:users,full_name',
'email' => 'required|unique:users,email',
'password' => 'required|min:6',
'track' => 'required|exists:tracks,type'
]);
Expand All @@ -39,6 +37,7 @@ public function create_participant($request) {
$trackID = Track::where('type', $request->track)->pluck('id')->first();
$user = User::create([
'full_name' => $request->full_name,
'email' => $request->email,
'track_id' => $trackID,
'password' => Hash::make($request->password),
'role' => 'participant',
Expand All @@ -55,6 +54,7 @@ public function create_judge($request)
$response = [];
$validator = Validator::make($request->all(), [
'full_name' => 'required|string|unique:users,full_name',
'email' => 'required|unique:users,email',
'password' => 'required|string|min:6',
'track' => 'required|exists:tracks,type'
]);
Expand All @@ -67,6 +67,7 @@ public function create_judge($request)
$trackID = Track::where('type', $request->track)->pluck('id')->first();
$user = User::create([
'full_name' => $request->full_name,
'email' => $request->email,
'password' => Hash::make($request->password),
'role' => 'judge',
'track_id' => $trackID
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/User/AdministratorResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function toArray($request)
return [
'id' => $this->id,
'full_name' => $this->full_name,
'email' => $this->email,
'role' => 'administrator'
];
}
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/User/JudgeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function toArray($request)
return [
'id' => $this->id,
'full_name' => $this->full_name,
'email' => $this->email,
'role' => 'judge',
'track' => $this->track->type
];
Expand Down
1 change: 1 addition & 0 deletions app/Http/Resources/User/ParticipantResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function toArray($request)
return [
'id' => $this->id,
'full_name' => $this->full_name,
'email' => $this->email,
'points' => $this->points,
'role' => 'participant',
'email_verified' => $this->email_verified_at ? true: false,
Expand Down
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class User extends Authenticatable implements MustVerifyEmail
*/
protected $fillable = [
'full_name',
'email',
'password',
'points',
'role',
Expand Down
11 changes: 10 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace App\Providers;

// use Illuminate\Support\Facades\Gate;

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Notifications\Messages\MailMessage;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -25,6 +28,12 @@ public function boot()
{
$this->registerPolicies();

//
VerifyEmail::toMailUsing(function ($notifiable, $url) {
$spaUrl = env('FRONT_URL').'/email_verification/verify?url='.$url;
return (new MailMessage)
->subject('Verify Your Email Address')
->line('Are you real? If so, please click the button below to verify your email address')
->action('Verify Email Address', $spaUrl);
});
}
}
Loading

0 comments on commit ff98ac9

Please sign in to comment.