-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
social-login-redirect: Redirection added for social auth users #306
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
*/ | ||
|
||
use Drupal\Core\Routing\RouteMatchInterface; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
||
/** | ||
* Implements hook_help(). | ||
|
@@ -22,3 +23,52 @@ function ct_user_help($route_name, RouteMatchInterface $route_match) { | |
default: | ||
} | ||
} | ||
|
||
/** | ||
* Implements hook_form_alter(). | ||
*/ | ||
function ct_user_form_alter(&$form, $form_state, $form_id) { | ||
// Check if the form is the user login form. | ||
if ($form_id == 'user_login_form') { | ||
// Add a custom validation function to the form. | ||
$form['#validate'][] = 'ct_user_user_login_form_validate'; | ||
} | ||
} | ||
|
||
/** | ||
* Custom validation function for the user login form. | ||
*/ | ||
function ct_user_user_login_form_validate(&$form, $form_state) { | ||
// Get the name value from the form. | ||
$name = $form_state->getValue('name'); | ||
|
||
// Get the id of the user. | ||
$ids = \Drupal::entityQuery('user') | ||
->condition('name', $name) | ||
->range(0, 1) | ||
->accessCheck(FALSE) | ||
->execute(); | ||
|
||
// Check if there is a User ID available. | ||
if (empty($ids)) { | ||
return; | ||
} | ||
else { | ||
// Check if there is a social auth profile. | ||
$socialIds = \Drupal::entityQuery("social_auth") | ||
->accessCheck(FALSE) | ||
->condition("user_id", reset($ids)) | ||
->execute(); | ||
|
||
// If the user has no social auth profile, allow the login to continue. | ||
if (empty($socialIds)) { | ||
return; | ||
} | ||
else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. This |
||
// If the user has social auth profile, redirect them to /user/login/google. | ||
$response = new RedirectResponse('/user/login/google'); | ||
$response->send(); | ||
user_logout(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to do this? If the form validation fails, the user will not log in, so there is nothing to logout. Speaking of which, you should fail the validation here even if we are sending a redirect response. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need an
else
block here. If the condition above is true, it will return. Otherwise, it will execute this block anyway. This is one of the ways we can avoidelse
blocks. I recommend you read https://medium.com/swlh/return-early-pattern-3d18a41bba8