PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Password Protect

Solved
dave-armstrong

Jun 6th, 2020 04:39 PM

I'm buiding a design portfolio and showcasing case studies. Some of which are protected under a NDA - i have permision to show the work to future clients but not advertise so I'm going to put them behind a password. I'm currently using voyager as a blog and content loader but I dont want the visitor to login, just use a password that i will supply.

So basically, I need some routes to be protected with out using Auth? Doable?

cookie

Jun 6th, 2020 08:41 PM

Simply use one of these packages:

  • https://github.com/spatie/laravel-littlegatekeeper
  • https://github.com/elic-dev/laravel-site-protection

;-)

Report
1
dave-armstrong

Jun 8th, 2020 01:04 AM

Hey, thanks for the suggestions but I can't get either to work... head meet desk!!!

tnylea

Jun 8th, 2020 03:15 AM

Yeah man, that's totally doable,

Do you want these routes that are not protected with authentication to still use the admin interface?

Anything inside of your routes/web.php that are outside of the Voyager routes:

Route::group(['prefix' => 'admin'], function () {
    Voyager::routes();
});

Will be accessible without authentication. Let me know if that helps or feel free to share some code samples and I can help further ;)

Report
1
dave-armstrong

Jun 8th, 2020 03:32 AM

So as I only want a password that I supply to be used (and not an email address) I was wondering if I just set up a user and password in voyager and have that stored in the controller (or somewhere) then have a middlewere on my ProjectsController@show.

This is my routes for the case studies

Route::get('case-studies', 'ProjectsController@index')->name('case-studies.index');
Route::get('case-studies/{project}', 'ProjectsController@show')->name('case-studies.show');

Route::group(['prefix' => 'admin'], function () {
    Voyager::routes();
});

My case-studies controller currently looks like this:

use App\Project;
use Illuminate\Http\Request;

class ProjectsController extends Controller
{
    // Get Case Studies page (/case-studies)
    public function index() {
        $projects = Project::all();
        return view('case-studies.index', ['projects' => $projects]);
    }

    // Get Case Study (/case-studies/slug)
    public function show($slug) {
        $project = Project::where('slug', $slug)->firstOrFail();
        return view('case-studies.show', ['project' => $project]);
    }

}

I don't really mind where the user puts the password to be honest but the only thing with using the voyager login is it shows the email address as well. Possibly a simple pop-up on the show routes would be better?

I'm still at an early stage of learning so any guidance on what I should be doing would be greatly appreciated.

tnylea

Jun 8th, 2020 05:08 PM

Best Answer

Here's probably the easiest way to do this. You could create a new user in Voyager, say that you created a user with an email [email protected], with password as password123.

You could then create a view with a form and have that email be a hidden field like this:

<form action="POST" method="/login">
    @csrf
    <input type="hidden" name="email" value="[email protected]">
    <input type="password" name="password">
    <input type="submit" value="login">
</form>

After the user has logged in, you could then hardcode that email inside of your ProjectsController like so:

use App\Project;
use Illuminate\Http\Request;

class ProjectsController extends Controller
{
    // Get Case Studies page (/case-studies)
    public function index() {
        $projects = Project::all();
        return view('case-studies.index', ['projects' => $projects]);
    }

    // Get Case Study (/case-studies/slug)
    public function show($slug) {
        if(auth()->guest() || (!auth()->guest() && auth()->user()->email != '[email protected]')){
            return redirect('/');
        }
        $project = Project::where('slug', $slug)->firstOrFail();
        return view('case-studies.show', ['project' => $project]);
    }

}

A better way might be to create a new row in the projects table called project_email, and then you can add the email address you want in that column for that project (ex. [email protected]), and you could rewrite your controller like so:

use App\Project;
use Illuminate\Http\Request;

class ProjectsController extends Controller
{
    // Get Case Studies page (/case-studies)
    public function index() {
        $projects = Project::all();
        return view('case-studies.index', ['projects' => $projects]);
    }

    // Get Case Study (/case-studies/slug)
    public function show($slug) {
        $project = Project::where('slug', $slug)->firstOrFail();
        if(auth()->guest() || (!auth()->guest() && auth()->user()->email != $project->project_email)){
            return redirect('/');
        }
        return view('case-studies.show', ['project' => $project]);
    }

}

And in this case, only the email address you want to allow access to that project will be allowed to view that page.

Hope that helps :)

Report
1
dave-armstrong

Jun 10th, 2020 07:26 PM

This is working great, thanks so much. One last thing before I finish up - once the user has signed in, whats the best practice to re-direct them to:

Route::get('case-studies', 'ProjectsController@index')->name('case-studies.index');

The user within the database has a role id of 2 - standard Voyager user. Currently when the new form submits, it redirects to /

cookie

Jun 10th, 2020 07:36 PM

You can change the redirectTo Var in your LoginController or create a method named redirectTo to override it.

protected $redirectTo = '/case-studies/index;
public function redirectTo()
{
    return redirect()->route('case-studies.index');
}
Report
2