Skip to content

Commit

Permalink
Sign in with Google Validation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
NuwanJ committed Sep 10, 2024
1 parent 3590f29 commit cbfdc35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Domains\Auth\Events\User\UserLoggedIn;
use App\Domains\Auth\Services\UserService;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Validator;
use App\Rules\ValidateAsInternalEmail;

/**
* Class SocialController.
Expand All @@ -30,7 +32,30 @@ public function redirect($provider)
*/
public function callback($provider, UserService $userService)
{
$user = $userService->registerProvider(Socialite::driver($provider)->user(), $provider);
// Validate for internal user
$info = Socialite::driver($provider)->user();
$validator = Validator::make(
['email' => $info->email, 'name' => $info->name],
['email' => ['required', 'email', new ValidateAsInternalEmail()], 'name' => ['required']]
);

if ($validator->fails()) {
$errorMessage = "";
$errors = $validator->errors();

foreach ($errors->messages() as $key => $messages) {
if (is_array($messages)) {
foreach ($messages as $message) {
$errorMessage .= $message . ' ';
}
} else {
$errorMessage .= $messages . ' ';
}
}
return redirect()->route('frontend.auth.login')->withFlashDanger(trim($errorMessage));
}

$user = $userService->registerProvider($info, $provider);

if (!$user->isActive()) {
auth()->logout();
Expand All @@ -43,4 +68,4 @@ public function callback($provider, UserService $userService)

return redirect()->route(homeRoute());
}
}
}
2 changes: 1 addition & 1 deletion app/Rules/ValidateAsInternalEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ public function passes($attribute, $value)
*/
public function message()
{
return "Only Department of Computer Engineering students/staff are allowed to register by themself.";
return "Only Department of Computer Engineering students/staff are allowed to register by themselves.";
}
}

0 comments on commit cbfdc35

Please sign in to comment.