Welcome Guest, Not a member yet? Register   Sign In
Shield Authentication
#1
Tongue 

Hi all,
I am trying to learn CI4 Shield authentication.
What i am trying to achieve with shield and Ci4
A private, login only (no registration needed), web app for myself where i can create some tools to help me.
The issue I'm facing:
So i installed a new ci4 project that was all okay, configured it all good, connected it to db then installed shield all good.
Tested registration and logging in, that also worked all fine.
The issue is when i created a new view file to redirect logged in users to, the routing works, after you log in it redirect you to the page but if you log out and go to the url of the page you can still access the page!

I have Ci4 in developer mode and its saying there is no session logged in.

Please find below the code ive changed.
Home.php
Code:
<?php

namespace App\Controllers;

class Home extends BaseController
{
    public function index(): string
    {
        return view('welcome_message');
    }

    public function afterLogin()
    {
        $user = \auth()->user();
        echo ($user);
    }
}



This is my auth.php routes i set up
Code:
public array $redirects = [
        'register'          => '/main-view',
        'login'            => '/main-view',
        'logout'            => 'login',
        'force_reset'      => '/',
        'permission_denied' => '/',
        'group_denied'      => '/',
    ];

Next this is my routes.php file
Code:
<?php

use CodeIgniter\Router\RouteCollection;

/**
* @var RouteCollection $routes
*/
$routes->get('/', 'Home::index');
$routes->get('/main-view', 'Home::afterLogin');
$routes->setAutoRoute(true);
service('auth')->routes($routes);

And this is my view file :
Code:
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Welcome to CodeIgniter 4!</title>
    <meta name="description" content="The small framework with powerful features">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="shortcut icon" type="image/png" href="/favicon.ico">

</head>

<body>


    <h1>LOGGED IN</h1>


</body>

</html>

Lastly this is my app.php where i've set up my local host base url

Code:
public string $baseURL = 'http://127.0.0.1/bluepanel-main/project-root/public/';



I have been reading the shield docs and watching some YouTube videos but i haven't found the solution yet.
Thank you for taking the time to look into this, it might be a obvious silly question however i am looking to learn this framework and i am fairly new to php.
Reply
#2

You need to protect pages that you want by the Controller Filter.
See https://shield.codeigniter.com/quick_sta...ting-pages
Reply
#3
Wink 

Hi Kenjis,
Thank you  very much for the reply, i've read the documentation but i'm a bit lost on how to configure the filters, i think ive gone about it the wrong way.
This is what i am trying to achieve:
No public / home page, when land on website redirect to login page. -> Once login have a logout  button at the top and a home page where only logged in users see.
If possible to protect every page under like a group? Like anything in home page (/home/index or /home/contact), to be session protected.
Have you got an example of filter configurations of something i'm trying to achieve. Again sorry about the hand holding questions, im struggling to find examples for ci4 and ive never worked with shield.

Many thanks
Reply
#4

It seems you want to protect all pages (except login/register page).
So the following example seems to be okay:
https://shield.codeigniter.com/reference...-all-pages
Reply
#5

That has worked wonders, thank you!

Also just a quick question on Ci4, if i wanted to create a platform where people sign in and have different content based on what they have saved in their acc, is that a possibility within CI4?
For example new user -> logs in -> check their notes.
other separate user logs in -> checks his already created notes different notes.

Many thanks,
Reply
#6

Yes, it is possible, if you write code for it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB