Drupal 8 Hook example: hook_user_login

Redirect when user log in first time

  1. function custom_module_user_login($account) {
  2.  
  3. $last_access = \Drupal::currentUser()->getLastAccessedTime();
  4. $config = \Drupal::config('fa_config_form.settings');
  5.  
  6. if (!$last_access) {
  7. $url = $config->get('first_login_redirect');
  8. if ($url) {
  9. $response = new RedirectResponse($url);
  10. $response->send();
  11. return;
  12. }
  13. }
  14. else {
  15. $url = $config->get('regular_login_redirect');
  16. if ($url) {
  17. $response = new RedirectResponse($url);
  18. $response->send();
  19. return;
  20. }
  21. }
  22. }