Skip to content

Commit

Permalink
Merge pull request #103 from ihamzehald/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ihamzehald authored Sep 11, 2020
2 parents 4b132ef + 063e4e8 commit b539d8d
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 41 deletions.
4 changes: 2 additions & 2 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public function render($request, Throwable $exception)

protected function unauthenticated($request, AuthenticationException $exception)
{
$message = "Unauthorized";
$message = trans("common.error.unauthenticated_msg");

$errors = [
"unauthorized" => "Unauthorized request"
"unauthorized" => trans("common.error.unauthenticated_err")
];

return $this->sendResponse(Constants::HTTP_UNAUTHORIZED, $message, null, $errors);
Expand Down
97 changes: 75 additions & 22 deletions app/Http/Controllers/API/V1/Auth/JWT/JwtAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,20 @@ public function __construct()
*/
public function register(Request $request)
{
$request->validate([
$requestValidationRules = [
'name' => 'required',
'password' => 'required|confirmed|min:8',
'email' => 'required|email|unique:users',
]);
];

if ($errors = $this->requestHasErrors($request, $requestValidationRules)) {
return $this->sendResponse(
Constants::HTTP_ERROR,
trans("common.error.generic"),
null,
$errors
);
}

$userData = request(['name','email', 'password']);

Expand All @@ -185,7 +194,7 @@ public function register(Request $request)
'password' => Hash::make($userData['password']),
]);

$message = "User registered successfully";
$message = trans("auth_jwt.success.register");

$token = auth("api_jwt")->attempt($userData);
$tokenData = $this->generateAccessTokenDetails($token);
Expand Down Expand Up @@ -324,26 +333,41 @@ public function register(Request $request)
* )
*/

public function login()
public function login(Request $request)
{

$requestValidationRules = [
'password' => 'required|min:8',
'email' => 'required|email',
];

if ($errors = $this->requestHasErrors($request, $requestValidationRules)) {
return $this->sendResponse(
Constants::HTTP_ERROR,
trans("common.error.generic"),
null,
$errors
);
}

$credentials = request(['email', 'password']);

if (!$token = auth("api_jwt")->attempt($credentials)) {
$errors = [
"wrong_credentials" => "The provided credentials don't match our records"
"wrong_credentials" => trans("auth_jwt.error.wrong_credentials")
];

return $this->sendResponse(
Constants::HTTP_UNAUTHORIZED,
"Wrong credentials",
trans("auth_jwt.error.wrong_credentials_msg"),
null,
$errors
);
}

return $this->sendResponse(
Constants::HTTP_SUCCESS,
"User logged in successfully",
trans("auth_jwt.success.login"),
$this->generateAccessTokenDetails($token)
);
}
Expand Down Expand Up @@ -441,7 +465,7 @@ public function login()
public function logout()
{
auth("api_jwt")->logout();
$message = "User logged out successfully";
$message = trans("auth_jwt.success.logout");
return $this->sendResponse(Constants::HTTP_SUCCESS, $message, null);
}

Expand Down Expand Up @@ -557,7 +581,7 @@ public function refresh()
{
$newToken = auth("api_jwt")->refresh();
$data = $this->generateAccessTokenDetails($newToken);
$message = "JWT token refresh successfully";
$message = trans("auth_jwt.success.refresh");

return $this->sendResponse(Constants::HTTP_SUCCESS, $message, $data);
}
Expand Down Expand Up @@ -669,7 +693,18 @@ public function refresh()

public function sendResetPasswordOTP(Request $request)
{
$this->isEmailValid($request);
$requestValidationRules = [
'email' => 'required|email'
];

if ($errors = $this->requestHasErrors($request, $requestValidationRules)) {
return $this->sendResponse(
Constants::HTTP_ERROR,
trans("common.error.generic"),
null,
$errors
);
}

$isUniqueToken = false;
$user = User::where('email', $request->get('email'))->first();
Expand All @@ -678,7 +713,7 @@ public function sendResetPasswordOTP(Request $request)
if ($user) {

/**
* Set all old OPT as expired for this user
* Set all old OTP as expired for this user
*/

ResetPasswordOTP::where('user_id', $user->id)
Expand Down Expand Up @@ -709,7 +744,7 @@ public function sendResetPasswordOTP(Request $request)
Mail::to($user)->send(new SendResetPasswordOTPMail($user, $resetPasswordOtpModel));

if (empty(Mail::failures())) {
$message = "OTP email sent successfully";
$message = trans("auth_jwt.success.otp_email");

return $this->sendResponse(Constants::HTTP_SUCCESS, $message);
}
Expand All @@ -718,7 +753,7 @@ public function sendResetPasswordOTP(Request $request)
}
}

$message = "Oops, something went wrong while trying to send your OTP";
$message = trans("auth_jwt.error.otp_email");

return $this->sendResponse(Constants::HTTP_ERROR, $message);
}
Expand Down Expand Up @@ -843,7 +878,16 @@ public function sendResetPasswordOTP(Request $request)
public function verifyOTP(Request $request)
{

$request->validate(['otp' => 'required']);
$requestValidationRules = ['otp' => 'required'];

if ($errors = $this->requestHasErrors($request, $requestValidationRules)) {
return $this->sendResponse(
Constants::HTTP_ERROR,
trans("common.error.generic"),
null,
$errors
);
}

$otp = $request->get("otp", null);

Expand Down Expand Up @@ -874,7 +918,7 @@ public function verifyOTP(Request $request)
if ($resetPasswordOTPVerificationModel->save()) {
$resetPasswordOTPModel->status = Constants::RESET_PASSWORD_OTP_ACTIVATED;
if ($resetPasswordOTPModel->save()) {
$message = "OTP verified successfully";
$message = trans("auth_jwt.success.otp_verification");
$data = [
"verification_token" => $uniqueOTPVerificationToken
];
Expand All @@ -885,15 +929,15 @@ public function verifyOTP(Request $request)
}
}
} else {
$message = "This OTP expired";
$message = trans("auth_jwt.error.otp_expired");
return $this->sendResponse(Constants::HTTP_ERROR, $message);
}
} else {
$message = "This OTP not valid";
$message = trans("auth_jwt.error.otp_invalid");
return $this->sendResponse(Constants::HTTP_ERROR, $message);
}

$message = "Oops, something went wrong";
$message = trans("common.error.generic");
return $this->sendResponse(Constants::HTTP_ERROR, $message);
}

Expand Down Expand Up @@ -1016,10 +1060,19 @@ public function verifyOTP(Request $request)

public function resetPassword(Request $request)
{
$request->validate([
$requestValidationRules = [
'verification_token' => 'required',
'password' => 'required|confirmed|min:8',
]);
];

if ($errors = $this->requestHasErrors($request, $requestValidationRules)) {
return $this->sendResponse(
Constants::HTTP_ERROR,
trans("common.error.generic"),
null,
$errors
);
}

$verificationToken = $request->get("verification_token", null);
$password = $request->get("password", null);
Expand All @@ -1036,15 +1089,15 @@ public function resetPassword(Request $request)
if ($user->save()) {
$resetPasswordOTPVerificationModel->status = Constants::RESET_PASSWORD_OTP_ACTIVATED;
if ($resetPasswordOTPVerificationModel->save()) {
$message = "Password reset successfully";
$message = trans("auth_jwt.success.reset_password");
return $this->sendResponse(Constants::HTTP_SUCCESS, $message);
}
}
}
}
}

$message = "Something went wrong while trying to reset your password";
$message = trans("auth_jwt.error.reset_password");
return $this->sendResponse(Constants::HTTP_ERROR, $message);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/V1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function __construct()

public function me()
{
$message = "User profile returned successfully";
$message = trans("common.success.generic");
$data = auth("api_jwt")->user();

return $this->sendResponse(Constants::HTTP_SUCCESS, $message, $data);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/V2/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class UserController extends UserControllerV1

public function me()
{
$message = "User profile returned successfully";
$message = trans("common.success.generic");
$data = auth("api_jwt")->user();

// Adding a new attribute in v2 that is not in V1
Expand Down
1 change: 1 addition & 0 deletions app/Http/Helpers/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ class Constants
const HTTP_SUCCESS = 200;
const HTTP_ERROR = 400;
const HTTP_UNAUTHORIZED = 401;
const HTTP_NOT_FOUND = 404;
const HTTP_FORBIDDEN = 403;
}
29 changes: 18 additions & 11 deletions app/Http/Helpers/Validators.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@

use Illuminate\Http\Request;
use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;

trait Validators
{

/**
* Validate the email for the given request.
*
* @param \Illuminate\Http\Request $request
* @return void
*/
public function isEmailValid(Request $request)
{
$request->validate(['email' => 'required|email']);
}

/**
* @param $otp
* @param $lifetime
Expand Down Expand Up @@ -50,4 +40,21 @@ public function isExpired($date, $lifetime)
? true
: false;
}

/**
* Generic validation error method
* @param $request
* @param $rules
* @return bool|\Illuminate\Support\MessageBag
*/
public function requestHasErrors($request, $rules)
{
$validator = Validator::make($request->all(), $rules);

if ($validator->fails()) {
return $validator->errors();
}

return false;
}
}
8 changes: 4 additions & 4 deletions app/Http/Middleware/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ public function handle($request, Closure $next)
return $next($request);
}

$message = "Invalid API key";
$errors = ["api_key" => "Invalid API key"];
$message = trans("common.error.api_key_invalid");
$errors = ["api_key" => trans("common.error.api_key_invalid")];

return $this->sendResponse(Constants::HTTP_UNAUTHORIZED, $message, null, $errors);
}

$message = "Missing API key header";
$errors = ["api_key" => "x-api-key header is missing"];
$message = trans("common.error.api_key_missing_msg");
$errors = ["api_key" => trans("common.error.api_key_missing_err")];

return $this->sendResponse(Constants::HTTP_UNAUTHORIZED, $message, null, $errors);
}
Expand Down
18 changes: 18 additions & 0 deletions resources/lang/en/auth_jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,23 @@
'password_reset_request_otp_mail_title' => 'Your OTP code',
'password_reset_request_otp_title' => 'Your OTP code',
'password_reset_request_otp_body' => ':otp is your OTP to reset your password.',
"success" => [
"register" => "User registered successfully",
"login" => "User logged in successfully",
"logout" => "User logged out successfully",
"refresh" => "JWT token refresh successfully",
"otp_email" => "OTP email sent successfully",
"otp_verification" => "OTP verified successfully",
"reset_password" => "Password reset successfully"
],
"error" => [
"wrong_credentials_msg" => "The provided credentials don't match our records",
"wrong_credentials" => "The provided credentials don't match our records",
"otp_email" => "Oops, something went wrong while trying to send your OTP",
"otp_expired" => "This OTP expired",
"otp_invalid" => "This OTP invalid",
"reset_password" => "Something went wrong while trying to reset your password"

]

];
14 changes: 14 additions & 0 deletions resources/lang/en/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
return [
"error" => [
"generic" => "Oops! something went wrong, please try again latter",
"api_key_missing_msg" => "Missing API key header",
"api_key_missing_err" => "x-api-key header is missing",
"api_key_invalid" => "Invalid API key",
"unauthenticated_msg" => "Unauthorized",
"unauthenticated_err" => "Unauthorized request"
],
"success" => [
"generic" => "Data returned successfully"
]
];

0 comments on commit b539d8d

Please sign in to comment.