Jump to content

provide Login Link that points to home page?


Joe
 Share

Recommended Posts

With a login link I am either sent to the Admin > Pages view ({$config->urls->admin}) or to the edit view of the page the login link is on ({$config->urls->admin}page/edit/?id={$page->id}) after logging in.

I would like to set up a login link that points to the home page instead (the public home page, but as a logged-in user). Possibly also a login link on other pages that sends you to the page itself rather than to its edit view.

I havent´t been able to come up with of a way of setting up such a link yet. Does anyone know offhand?

Thank you!
 

Edited by Joe
Link to comment
Share on other sites

This is mostly stolen from Ryan: https://processwire.com/talk/topic/1716-integrating-a-member-visitor-login-form/?p=15919

But it adds the functionality to redirect back to the page the user was on when they started the login:

$requested_pid = (int) $input->get->pid;
$t = $pages->get($requested_pid)->path;

if($user->isLoggedin()){
  if($requested_pid == ''){
   $session->redirect('/');
  }
  else{
   $session->redirect($t);
  }
}

if($input->post->username && $input->post->pass) {
  $username = $sanitizer->username($input->post->username);
  $pass = $input->post->pass;
  $u = $users->get($username);
  if($u->id && $u->tmp_pass && $u->tmp_pass === $pass) {
    // user logging in with tmp_pass, so change it to be their real pass
    $u->of(false);
    $u->pass = $u->tmp_pass;
    $u->save();
    $u->of(true);
  }
  $u = $session->login($username, $pass);
  if($u) {
    // user is logged in, get rid of tmp_pass
    $u->of(false);
    $tmp_pass = $u->tmp_pass;
    $u->tmp_pass = '';
    $u->save();

    // now redirect to the profile edit page
    if($tmp_pass == $pass){
      $session->redirect('/profile/');
    }
    else{
      $session->redirect($t);
    }
  }
}

// present the login form
$headline = $input->post->username ? "Login failed" : "Please login";
echo "
  <h2>$headline</h2>
  <form action='./?pid=".$requested_pid."' method='post'>
  <p>
  <label>Username <input type='text' name='username'></label>
  <label>Password <input type='password' name='pass'></label>
  </p>
  <input class='bluebutton' type='submit' name='login' value='Login'>
  </form><hr /><br />
  <p><a href='/reset-pass/'>Forgot Password?</a></p>
";

Keep in mind that this also includes "forgot password" functionality, but doesn't include all the code for that - take a look at that post of Ryan's to get the rest.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...