Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit edfb34c

Browse files
email verification
1 parent ab47f1e commit edfb34c

File tree

7 files changed

+68
-4
lines changed

7 files changed

+68
-4
lines changed

‎app/ApiCode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ class ApiCode {
66
public const SOMETHING_WENT_WRONG = 250;
77
public const INVALID_CREDENTIALS = 251;
88
public const VALIDATION_ERROR = 252;
9+
public const EMAIL_ALREADY_VERIFIED = 253;
10+
public const INVALID_EMAIL_VERIFICATION_URL = 254;
911
}

‎app/Http/Controllers/RegistrationController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@
77

88
class RegistrationController extends Controller
99
{
10+
/**
11+
* Register User
12+
*
13+
* @param RegistrationRequest $request
14+
* @return \Symfony\Component\HttpFoundation\Response
15+
*/
1016
public function register(RegistrationRequest $request) {
11-
User::create($request->getAttributes());
17+
User::create($request->getAttributes())->sendEmailVerificationNotification();
1218

1319
return $this->respondWithMessage('User successfully created');
1420
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\ApiCode;
6+
use App\User;
7+
use Illuminate\Http\Request;
8+
9+
class VerificationController extends Controller {
10+
11+
public function __construct() {
12+
$this->middleware('auth:api')->except(['verify']);
13+
}
14+
15+
/**
16+
* Verify email
17+
*
18+
* @param $user_id
19+
* @param Request $request
20+
* @return \Illuminate\Http\RedirectResponse|\Symfony\Component\HttpFoundation\Response
21+
*/
22+
public function verify($user_id, Request $request) {
23+
if (! $request->hasValidSignature()) {
24+
return $this->respondUnAuthorizedRequest(ApiCode::INVALID_EMAIL_VERIFICATION_URL);
25+
}
26+
27+
$user = User::findOrFail($user_id);
28+
29+
if (!$user->hasVerifiedEmail()) {
30+
$user->markEmailAsVerified();
31+
}
32+
33+
return redirect()->to('/');
34+
}
35+
36+
/**
37+
* Resend email verification link
38+
*
39+
* @return \Symfony\Component\HttpFoundation\Response
40+
*/
41+
public function resend() {
42+
if (auth()->user()->hasVerifiedEmail()) {
43+
return $this->respondBadRequest(ApiCode::EMAIL_ALREADY_VERIFIED);
44+
}
45+
46+
auth()->user()->sendEmailVerificationNotification();
47+
48+
return $this->respondWithMessage("Email verification link sent on your email id");
49+
}
50+
}

‎app/Http/Middleware/Authenticate.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Authenticate extends Middleware
1515
protected function redirectTo($request)
1616
{
1717
if (! $request->expectsJson()) {
18-
return route('login');
18+
return redirect();
1919
}
2020
}
2121
}

‎config/response_builder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,8 @@
2222
ApiCode::SOMETHING_WENT_WRONG => 'api.something_went_wrong',
2323
ApiCode::INVALID_CREDENTIALS => 'api.invalid_credentials',
2424
ApiCode::VALIDATION_ERROR => 'api.validation_error',
25+
ApiCode::INVALID_EMAIL_VERIFICATION_URL => 'api.invalid_email_verification_url',
26+
ApiCode::EMAIL_ALREADY_VERIFIED => 'api.email_already_verified',
2527
],
28+
2629
];

‎resources/lang/en/api.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
'something_went_wrong' => 'Ops! Something went wrong.',
1717
'invalid_credentials' => 'Invalid email or password.',
18-
'validation_error' => 'Validation Error'
19-
18+
'validation_error' => 'Validation Error',
19+
'invalid_email_verification_url' => 'Invalid url provided.',
20+
'email_already_verified' => 'You have already verified email.'
2021
];

‎routes/api.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@
2525
Route::get('me', 'AuthController@me')->middleware('log.route');
2626

2727
Route::post('register', 'RegistrationController@register');
28+
Route::get('email/verify/{id}', 'VerificationController@verify')->name('verification.verify');
29+
Route::get('email/resend', 'VerificationController@resend')->name('verification.resend');
2830
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /