Tank Auth: automatic login on email verification

2.6k Views Asked by At

I am using Tank Auth, and require my users validate their email addresses. Upon validation, users are still not logged in. I would like users to be automatically logged in when activating their account.

In my previous version (home-grown auth) I wrote a login function that didn't require a password, that was only callable in that situation. However, I can't see a way of doing that in Tank Auth. One solution would be to cache passwords until activation, but I really don't want to do that...

2

There are 2 best solutions below

1
On BEST ANSWER

You can just bypass your login system.

Store either thier email address OR alias in a cookie.

when they activate grab the cookie, search(verify they exist) for the user, then setup a normal login session.

A login is just a set of session rules.

inside your activation script/email process, append

$this->_set_ac_cookie({email});

-

protected function _set_ac_cookie({email})
{
    return $this->input->set_cookie(array(
        'name'  =>  'ac_cookie_'.{email},
        'expire'    =>  '7200', //You may mirror when/if your activation key expires
        'domain'    =>  'yourdomain.com',
        'path'  =>  '/'
    ));
}

check cookie exists

protected function _before_activation({email})
{
    return $this->input->cookie('ac_cookie_'.{email})) ? TRUE : FALSE;
    //make sure {email} has a UNIQUE db constraint
}

when the user clicks the activation link

if($this->_before_activation({email}))
{
        //check user exists AND activation status === false (0)
        //grab the user details from db based on {email}, set activation status to true (1)

        //setup login session
        $this->session->set_userdata(array(
            'logged_in' =>  (int)1,
            'user_id'   =>  {user->id},
            'alias' =>  {user->alias}
        ));

        redirect('dashboard');
}
else
{
    //sorry!
}
1
On

If you take a look at the code inside tank auth that handles the email activation, you'll see that it explicitly logs the user out on email verification.

https://github.com/ilkon/Tank-Auth/blob/master/application/controllers/auth.php

Line 243.

You could just comment out $this->tank_auth->logout(); on line 244 and it should work as you want it to, but this would be bad practice.