davo Posted August 4, 2014 Share Posted August 4, 2014 I'm trying to get the login form on my site to redirect a user if they have the role "agent". I'm using the following code and i thought using 'has role' I would be able to conditionally redirect a user but it's always choosing the else option at the moment. Are there any glaring mistakes in my code? <?php if($user->isLoggedin()) $session->redirect('/portal/agent-portal/'); 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); $u->tmp_pass = ''; $u->save(); // now redirect to the profile edit page if ($user->hasRole("agent")) { $session->redirect('/portal/agent-portal/');}else{ $session->redirect('/portal/profile/'); } } } // present the login form $headline = $input->post->username ? "Login failed" : "Please login"; $page->body = " <h2>$headline</h2> <form role='form' action='./' method='post'> <div class='form-group'> <label for='username'>Username </label> <input type='text' class='form-control' style='border-radius:0px' id='username' placeholder='username' name='username'> </div> <div class='form-group'> <label for='pass'>Password</label> <input type='password' class='form-control' style='border-radius:0px' id='pass' placeholder='Password' name='pass'> </div> <button type='submit' class='btn btn-sm btn-default'>Sign in</button> </form> <p><a href='/reset-pass/'>Forgot your password?</a></p> "; $page->editor = "Login to view content editor"; include("./main.php"); // main markup template Link to comment Share on other sites More sharing options...
adrian Posted August 4, 2014 Share Posted August 4, 2014 Hi davo, Do you want $user->hasRole or $u->hasRole ? It looks like you are redirecting to the agent-portal page if the user is logged in already and the hasRole check is for the user you are logging in via the posted username and password, which is for $u 1 Link to comment Share on other sites More sharing options...
Pete Posted August 4, 2014 Share Posted August 4, 2014 You could also change $u back to $user at the end of all that code above // present the login form and simply move the first line in your code to just after the closing of the if statement since you don't seem bothered about checking the role on line 1 if they're already logged in? But I think adrian has identified the problem: $u != $user 2 Link to comment Share on other sites More sharing options...
davo Posted August 4, 2014 Author Share Posted August 4, 2014 Thank you both. I'm still fair new to the api I sometimes take the examples I see 'literally'. 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now