lonnieezell / myth-auth

One-stop Auth package for CodeIgniter 4
MIT License
625 stars 207 forks source link

Login issue after registration with success notification #598

Closed theblackzarc closed 11 months ago

theblackzarc commented 1 year ago

Hai guys,

I would need some help for this issue i'm facing with. oh also i've search the issue open or closed but nothing the same as my issue.

here is my step that will show the error:

  1. register a new user
  2. after success registration it will be redirected to login page with a success registration notification
  3. then try to login on the login page with a success registration notification
  4. instead of home or http://localhost:8080 but it will redirect to http://localhost:8080/dist/js/adminlte.min.js

How is it possible? and how to fix it? i've check the attemptRegister() and i think it will not route to http://localhost:8080/dist/js/adminlte.min.js

issue_redirect

but if you've done with registration and then refresh the login page (the notification will be gone) it will not redirect to http://localhost:8080/dist/js/adminlte.min.js but go to home or http://localhost:8080

Codeiginter 4 version 4.3.6 Myth/Auth version 1.2.1 PHP Version 7.4.33

Many thanks, Jonathan

manageruz commented 1 year ago

Hi Jonathan. It's strange that kind of behavior. Have you modified the default code of MythAuth? I mean AuthController.php and view files. And please show me your config value of myth's $landingRoute variable, then $baseURL and $indexPage variables values.

manageruz commented 1 year ago

And if you made modifications, please provide all of them.

theblackzarc commented 1 year ago

Hai manageruz,

Editing AuthController.php is a no and for viewing i just rename it to login_old.php and register_old.php

i did this coz i just want to use my login and register template, but all the coding inside was same as the origin. see the picture i just send.

views

and here is my code inside my login template.

<?= $this->extend('auth/templates/layout'); ?>
<?= $this->section('content'); ?>
<title>LMS | SIGN IN</title>

<body class="hold-transition login-page">
    <div class="login-box">
        <div class="card card-outline card-primary">
            <div class="card-header text-center">
                <img src="assets/img/logo.png" alt="" style="width: 60%;">
            </div>
            <div class="card-body">
                <p class="login-box-msg">Sign in to start your session</p>

                <?= view('Myth\Auth\Views\_message_block') ?>

                <form action="<?= route_to('login') ?>" method="post">
                    <?= csrf_field() ?>
                    <?php if ($config->validFields === ['email']) : ?>

                        <div class="input-group mb-3">
                            <input type="email" class="form-control <?php if (session('errors.login')) : ?>is-invalid<?php endif ?>" name="login" placeholder="<?= lang('Auth.email') ?>">
                            <div class="input-group-append">
                                <div class="input-group-text">
                                    <span class="fas fa-envelope"></span>
                                </div>
                            </div>
                            <div class="invalid-feedback">
                                <?= session('errors.login') ?>
                            </div>
                        </div>

                    <?php else : ?>

                        <div class="input-group mb-3">
                            <input type="text" class="form-control <?php if (session('errors.login')) : ?>is-invalid<?php endif ?>" name="login" placeholder="<?= lang('Auth.emailOrUsername') ?>">
                            <div class="input-group-append">
                                <div class="input-group-text">
                                    <span class="fas fa-envelope"></span>
                                </div>
                            </div>
                            <div class="invalid-feedback">
                                <?= session('errors.login') ?>
                            </div>
                        </div>

                    <?php endif; ?>

                    <div class="input-group mb-3">
                        <input type="password" name="password" class="form-control <?php if (session('errors.password')) : ?>is-invalid<?php endif ?>" placeholder="<?= lang('Auth.password') ?>">
                        <div class="input-group-append">
                            <div class="input-group-text">
                                <span class="fas fa-lock"></span>
                            </div>
                        </div>
                        <div class="invalid-feedback">
                            <?= session('errors.password') ?>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-8">
                            <?php if ($config->allowRemembering) : ?>
                                <div class="icheck-primary">
                                    <input type="checkbox" name="remember" class="form-check-input" <?php if (old('remember')) : ?> checked <?php endif ?>>
                                    <label for="form-check-label">
                                        <?= lang('Auth.rememberMe'); ?>
                                    </label>
                                </div>
                            <?php endif; ?>
                        </div>
                        <div class="col-4">
                            <button type="submit" class="btn btn-primary btn-block"><?= lang('Auth.loginAction') ?></button>
                        </div>

                    </div>
                </form>
                <?php if ($config->activeResetter) : ?>
                    <p class="mb-1">
                        <a href="<?= route_to('forgot') ?>"><?= lang('Auth.forgotYourPassword'); ?></a>
                    </p>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- jQuery -->
    <script src="<?= base_url(); ?>assets/plugins/jquery/jquery.min.js"></script>
    <!-- Bootstrap 4 -->
    <script src="<?= base_url(); ?>assets/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
    <!-- AdminLTE App -->
    <script src="<?= base_url(); ?>assets/dist/js/adminlte.min.js"></script>
</body>

<?= $this->endSection(); ?>

please let me know if i did something i'm not supposed.

Thank You manageruz.

Regards, Jonathan

manageruz commented 1 year ago

You forget to show your config value of myth's $landingRoute variable, then $baseURL and $indexPage variables values.

theblackzarc commented 1 year ago

owh sorry, i forgot..

here is the code

public $landingRoute = '/';
    public $reservedRoutes = [
        'login'                   => 'login',
        'logout'                  => 'logout',
        'register'                => 'register',
        'activate-account'        => 'activate-account',
        'resend-activate-account' => 'resend-activate-account',
        'forgot'                  => 'forgot',
        'reset-password'          => 'reset-password',
    ];
public string $baseURL = 'http://sub.domain.com/';
public string $indexPage = '';

here it is manageruz. sorry i'm really forget about this. Thank You.

Regards, Jonathan

manageruz commented 11 months ago

Hey Jonathan. I can't reproduce your error. It should be some kind of web server settings error. What's your webserver?

manageruz commented 11 months ago

public string $baseURL = 'http://sub.domain.com/'; is this is your real url?

theblackzarc commented 11 months ago

Hey Jonathan. I can't reproduce your error. It should be some kind of web server settings error. What's your webserver?

Hi manageruz, I'm using XAMPP and also Live Server with CPanel in it. both of them make the same problem but it is okay since we use it as internal web app.

public string $baseURL = 'http://sub.domain.com/'; is this is your real url?

nope.. it just some example.. but it is true I'm using it in sub domain not the domain primary.

Thank You manageruz for responding, it just annoying that I must click back at the browser then everything went normal.

if You want this to close so be it, since its not very important issue like others.

Thank You so much for Your time manageruz 🙏🏻

regards, Jonathan

manageruz commented 11 months ago

It's interesting for me too. But as i said before i can't reproduce your error. Try to check your .htaccess file settings. It would be nice if you show it. It also might be a subdomain issue.

manageruz commented 11 months ago

And which is your success registration notification ?

  1. Please confirm your account by clicking the activation link in the email we have sent.
  2. Welcome aboard! Please login with your new credentials.
manageruz commented 11 months ago

In the AuthController.php file before the code below (it's inside attemptLogin() method) dump the session('redirect_url') and site_url($this->config->landingRoute) variables and show me it's values please after successful registration message without refreshing the page:

$redirectURL = session('redirect_url') ?? site_url($this->config->landingRoute); 
unset($_SESSION['redirect_url']);
theblackzarc commented 11 months ago

Sorry manageruz for very late respond, had to travel outside.

It's interesting for me too. But as i said before i can't reproduce your error. Try to check your .htaccess file settings. It would be nice if you show it. It also might be a subdomain issue.

here is my .htaccess (default/never change)

# Disable directory browsing
Options -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    # RewriteBase /

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
    ServerSignature Off
# Disable server signature end

And which is your success registration notification ?

  1. Please confirm your account by clicking the activation link in the email we have sent.
  2. Welcome aboard! Please login with your new credentials.

No. 2

In the AuthController.php file before the code below (it's inside attemptLogin() method) dump the session('redirect_url') and site_url($this->config->landingRoute) variables and show me it's values please after successful registration message without refreshing the page:

$redirectURL = session('redirect_url') ?? site_url($this->config->landingRoute); 
unset($_SESSION['redirect_url']);
    public function attemptLogin()
    {
        $rules = [
            'login'    => 'required',
            'password' => 'required',
        ];
        if ($this->config->validFields === ['email']) {
            $rules['login'] .= '|valid_email';
        }

        if (!$this->validate($rules)) {
            return redirect()->back()->withInput()->with('errors', $this->validator->getErrors());
        }

        $login    = $this->request->getPost('login');
        $password = $this->request->getPost('password');
        $remember = (bool) $this->request->getPost('remember');

        // Determine credential type
        $type = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'username';

        // Try to log them in...
        if (!$this->auth->attempt([$type => $login, 'password' => $password], $remember)) {
            return redirect()->back()->withInput()->with('error', $this->auth->error() ?? lang('Auth.badAttempt'));
        }

        // Is the user being forced to reset their password?
        if ($this->auth->user()->force_pass_reset === true) {
            return redirect()->to(route_to('reset-password') . '?token=' . $this->auth->user()->reset_hash)->withCookies();
        }

        $redirectURL = session('redirect_url') ?? site_url('/');  <--- this part is not the same as You mention manageruz
        $redirectURL = session('redirect_url') ?? site_url($this->config->landingRoute); <--- this is from You

        unset($_SESSION['redirect_url']); <--- uncomment

        return redirect()->to($redirectURL)->withCookies()->with('message', lang('Auth.loginSuccess'));
    }

so I will comment it for now and try the result.. and here is the result (attached)

before-register

after-register

before changing attemptLogin()

        $redirectURL = session('redirect_url') ?? site_url('/');
        unset($_SESSION['redirect_url']);
        return redirect()->to($redirectURL)->withCookies()->with('message', lang('Auth.loginSuccess'));

redirection-failed

after first changing attemptLogin()

        // $redirectURL = session('redirect_url') ?? site_url('/');
        unset($_SESSION['redirect_url']);
        // return redirect()->to($redirectURL)->withCookies()->with('message', lang('Auth.loginSuccess'));

redirection-failed-login

after second changing attemptLogin()

        $redirectURL = '';
        unset($_SESSION['redirect_url']);
        return redirect()->to($redirectURL)->withCookies()->with('message', lang('Auth.loginSuccess'));

success-redirection

and it works, many thanks manageruz for staying with me all times and days.

Thank You so much manageruz

Sorry for the edit.. just to make it clear though 😄