Jump to content

How to redirect back to home of current language after login


adrianmak
 Share

Recommended Posts

Here is a sample page tree and my site is a multi language website with default English and two other languages let say japanese and korean

language path suffix of each are

/    english

/jpn   japanese

/kor  korean
 

Home
  +-----about
  +-----product
  +-----contact
  +-----members-area  
  +-----login (1001)
  +-----logout (1002)

members-area is restricted for users with registered role.

In access tab of members-area template, page will redirect to 1001 page id (login form) for guest

After user log in, it will redirect back to home

My problem is

if I am at other two langauges  to login, it will redirect to default English home. I suppose redirect back to japanese home or korean home

For instance,

when access members area in japanese ,

http://mydomain.com/jpn/members-area   

it will redirect to login page    

http://mydomain.com/jpn/login

After user logged in, pw redirect to (default English home) instead of http://mydomain.com/jpn (japanses Home)

This is my login.php template

<?php
$content = "";

if ($user->isLoggedin()) {
        $session->redirect($pages->get("template=home")->url);
}

if ($input->post->user && $input->post->pass) {
    $username = $sanitizer->username($input->post->user);
    $password = $input->post->pass;

    try {
        if ($session->login($username, $password)) {
            $session->redirect($pages->get("template=home")->url);  // redirect to home after user login
        } else {
            $error = "Wrong username or password. Login failed.";
        }
    } catch (Exception $e) {
        $error = $e->getMessage();
    }
}

$content .= "
        <div class='login-box'>
        <div class='panel panel-default'>
            <div class='panel-heading'>
                <h3 class='panel-title'><span class='lock-icon'></span><strong>Member Log-in</strong></h3>
            </div>
            <div class='panel-body'>
                <form role='form' action='./' method='post'>
                    <div class='message-error'>$error</div>
                    <div class='form-group'>
                        <label for='user'>Username</label>
                        <input type='text' name='user' id='user' class='form-control' placeholder='Username' />
                    </div>
                    <div class='form-group'>
                        <label for='pass'>Password</label>
                        <input type='password' name='pass' id='pass' class='form-control' placeholder='Password' />
                    </div>
                    <button type='submit' class='btn btn-sm btn-primary'>Login</button>
                </form>
            </div>
        </div>
        </div>
";
Link to comment
Share on other sites

if I am at other two langauges  to login, it will redirect to default English home. [...]

This is only true if language of current user (set in backend) is default.

// define language var before login take current language of page
$language = $user->language

// redirect to this language after login
$session->redirect($pages->get("template=home")->localUrl($language));

Small hints :)

1) Read the docs https://processwire.com/api/multi-language-support/multi-language-urls/

2) use http://example.org/jpn/login instead of http://mydomain.com/jpn/login

  • Like 3
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...