Skip to content

Commit

Permalink
Implemented email verification during registration
Browse files Browse the repository at this point in the history
  • Loading branch information
ramoncrisante committed Aug 26, 2021
1 parent 1db5d61 commit daff000
Show file tree
Hide file tree
Showing 13 changed files with 915 additions and 220 deletions.
46 changes: 2 additions & 44 deletions app/Http/Controllers/Api/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\LoginRequest;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\RegisterRequest;
use Illuminate\Auth\Events\Registered;


class AuthController extends Controller
Expand Down Expand Up @@ -49,47 +50,4 @@ public function user()
{
return response()->json(Auth::user());
}

public function register(RegisterRequest $request)
{
try {
$user = User::create([
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
])->sendEmailVerificationNotification();

return $user;
} catch (\Exception $e) {
return response([
'message' => 'Internal error, please try again later.' //$e->getMessage()
], 400);
}
$test = 0;
}

public function verify($userId, Request $request) {
if (!$request->hasValidSignature()) {
return response()->json(["msg" => "Invalid/Expired url provided."], 401);
}

$user = User::findOrFail($userId);

if (!$user->hasVerifiedEmail()) {
$user->markEmailAsVerified();
}

return redirect()->to('/');
}

public function resend() {
if (auth()->user()->hasVerifiedEmail()) {
return response()->json(["msg" => "Email already verified."], 400);
}

auth()->user()->sendEmailVerificationNotification();

return response()->json(["msg" => "Email verification link sent on your email."]);
}
}
33 changes: 33 additions & 0 deletions app/Http/Controllers/Api/RegisterController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use App\Http\Requests\RegisterRequest;
use Illuminate\Auth\Events\Registered;


class RegisterController extends Controller
{
public function register(RegisterRequest $request)
{
try {
$user = User::create([
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'email' => $request->input('email'),
'password' => Hash::make($request->input('password')),
]);

event(new Registered($user));

return response()->json($user);
} catch (\Exception $e) {
return response([
'message' => 'Internal error, please try again later.' //$e->getMessage()
], 400);
}
}
}
41 changes: 41 additions & 0 deletions app/Http/Controllers/Api/VerifyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Controllers\Api;

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


class VerifyController extends Controller
{
public function verify($userId, Request $request)
{
if (!$request->hasValidSignature()) {
return redirect('/login?verification_status=error');
}

$user = User::findOrFail($userId);

if (!$user->hasVerifiedEmail()) {
$user->markEmailAsVerified();
}

return redirect('/login?verification_status=success');
}

public function resend(User $user)
{
if (!$user) {
return response()->json(["msg" => "Invalid user."], 400);
} elseif ($user->hasVerifiedEmail()) {
return response()->json(["msg" => "Email already verified."], 400);
}

$user->sendEmailVerificationNotification();

return response()->json(["msg" => "Email verification link sent on your email."]);
}
}
Loading

0 comments on commit daff000

Please sign in to comment.