Coaster CMS Blog

Securing Files with Coaster CMS

With the latest version of Coaster CMS you can now block visitors from accessing certain content such as PDFs, images and documents unless they are logged in. Users who try to access any of this content before they are logged in, will be redirected to a login page where if they don’t already have an account can choose to register accordingly. Once logged in, all of these files will become available to the user.

Setting up a secure directory

Securing files within Coaster CMS is easy. Once you are logged into your website yourwebsite.com/admin navigate to system settings and scroll to the bottom of the page. You should be able to see a setting named “Secure Upload Folders,” enter a directory name of your choice.

This directory will now be visible when you access your file manager. Any files placed within this new directory will only be accessible to logged in users. If you wish to have a members only area, where any visitor to your site can create an account, follow the steps below.

Creating a user registration form

You can piggyback off of Coaster’s own user model to register and authenticate front-end users. First things first, you will need to create a very short model that extends the Coaster CMS user model. Create a file named User.php within the app/Models directory. If the models directory doesn’t already exist, create one. Once this new file has been created, enter the code below:

The next step involves adding some user login and user registration methods to the app/Http/Controllers/Auth/AuthController.php file.

Don’t worry about any of the views referenced within the above code, we will move on to creating them shortly. At the bottom of this file there is a create method, before we continue we must set the role_id of new users to that of something that has frontend access only.

It is vitally important that you add this line, as otherwise you leave the potential for anyone to have admin access to the backend of your site.

Finally we must make some changes/additions to the routes.php file within the app/Http directory.

The first part of the code registers all of the different routes relevant to user registration and login. The final part of the code ensures that any users who visit a downloads page are redirected to a login page if they are not already logged in. You can adapt this code to your liking, and require authentication for all sorts of different URLs.

Middleware

You’ll have seen the ‘auth’ middleware group in the routes (see Laravel’s middleware documentation for more information)…

There are a few changes you must make to the Authenticate.php file found in app/Http/Middleware. The below code ensures that any AJAX requests receive a 200 status code on protected pages if the user is logged in and authenticated.

All that is required now is a user authentication view and a user registration view. In the case of this tutorial we have placed these views within resources/views/auth. Create a login.blade.php file and a register.blade.php (if you’d prefer not to use the Blade templating engine, omit the .blade extension). I won’t go into too much detail involving the creation of forms as this has already been covered within our developer documentation.

Conclusion

We hope this post has helped those of you who were looking to add front-end user authentication to their website.

Please note that any files stored within a secured directory will be inaccessible to bots and crawlers such as Google, and will therefore not appear within search results.