0% found this document useful (0 votes)
15 views6 pages

Recaptcha Guide

sdfasdf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Recaptcha Guide

sdfasdf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Create "php artisan make:view layout/assets".

and link bootstrap cdn script

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@yield('title')</title>
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet" integrity="sha384-
QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
crossorigin="anonymous">
</head>
<body>

<div class="container">
@yield('content')
</div>

<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
crossorigin="anonymous"></script>
</body>
</html>

2. Create registration/login page. "php artisan make:view user/registration" "php


artisan make:view user/registration"

REGISTRAION PAGE

@extends('layout.asset')
@section('title', 'Registration Page')

@section('content')

@error('mail')
<div class="text-danger">{{$message}}</div>
@enderror

<form action="{{ route('user.store') }}" method="POST">


@csrf
<div class="row mt-4">
<div class="col-sm-4"></div>
<div class="col-sm-4">
<br>
<br>
<div class="card p-3">
<h2>Create Account</h2>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" name="email" id="email"
value="{{ old('email') }}" placeholder="[email protected]">
@error('email')
<div class="text-danger">{{ $message }} </div>
@enderror
</div>
<div class="mb-3">
<label for="name" class="form-label">Name</label>
<input type="name" class="form-control" name="name" id="name"
value="{{ old('name') }}" placeholder="name">
@error('name')
<div class="text-danger">{{ $message }} </div>
@enderror
</div>
<div class="mb-3">
<label for="contact" class="form-label">Contact</label>
<input type="text" class="form-control" name="contact" id="contact"
value="{{ old('contact') }}" placeholder="09123456489">
@error('contact')
<div class="text-danger">{{ $message }} </div>
@enderror
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password"
id="password" placeholder="********">
@error('password')
<div class="text-danger">{{ $message }} </div>
@enderror
</div>

<div class="mb-3">
{!!NoCaptcha::renderJs()!!}
{!!NoCaptcha::display()!!}

@error('g-recaptcha-response')
<div class="text-danger">{{ $message }}
@enderror
<br>
<input type="submit" class="form-control btn btn-primary"
id="password" value="Create Account"></form>
</div>
<i style="text-align: right">Already have an account? <a
href="{{ route('login') }}">Login here</a></i>
</div>
</div>
<div class="col-sm-4">

</div>
</div>

LOGIN PAGE

@extends('layout.asset')
@section('title', 'Login Page')

@section('content')

@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
@if(session('error'))
<div class="alert alert-danger">
{{ session('error') }}
</div>
@endif
<div class="row mt-4">
<div class="col-sm-4">

</div>
<div class="col-sm-4">
<br>
<br>
<div class="card p-3">
<div class="mb-3">
<form action="{{ ('loginprocess') }}" method="POST">
<h2>Login</h2>
@csrf
<label for="email" class="form-lable">Email address</label>
<input type="email" class="form-control" name="email"
id="email" placeholder="[email protected]" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" name="password"
id="password" placeholder="********" required>
@error('password')
<div class="text-danger">{{ $message }} </div>
@enderror
</div>

<div class="mb-3">
{!!NoCaptcha::renderJs()!!}
{!!NoCaptcha::display()!!}
@if($errors->has('g-recaptcha-response'))
<div class="alert alert-danger">
{{ $errors->first('g-recaptcha-response') }}
</div>
@endif
<br>
<input type="submit" class="form-control btn btn-primary"
id="login" value="Login">
</form>
</div>
<i style="text-align: right">Don't have an account?<a
href="{{ route('register') }}"> Create account</a></i>
</div>
</div>
<div class="col-sm-4">

</div>
</div>
@endsection

@endsection

3. Mange routes
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;

Route::get('/' ,[AuthController::class, 'register'])->name('register');


Route::get('/login' ,[AuthController::class, 'login'])->name('login');

Route::post('/createaccount', [AuthController::class, 'user_store'])-


>name('user.store');
Route::post('/loginprocess', [AuthController::class, 'userlogin'])-
>name('loginprocess');

Route::get('/logout' ,[AuthController::class, 'logoutuser'])->name('logout');

Route::middleware(['auth'])->group(function(){

Route::get('/userdashboard', [AuthController::class, 'dashboard'])-


>name('userdashboard');
});

Route::middleware(['auth', 'roles:admin'])->group(function(){

Route::get('/admindashboard', [AuthController::class, 'admindashboard'])-


>name('admindashboard');
});

4. Create database
5. add users in the migrations
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('contact');
$table->string('password');
$table->timestamps();
});
6. Add roles to users table. "php artisan make:migration add_roles_to_user_table"
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddRolesToUserTable extends Migration


{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('roles')->default('user')->after('password');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('roles');
});
}
};

7. Migrate it. "php artisan migrate"


8. Create dashboard for user and admin. "php artisan make:view user/dashboard" "php
artisan make:view admin/dashboard"
9. Create middleware. "php artisan make:middleware CheckAdmin"
<?php

namespace App\Http\Middleware;

use Illuminate\Support\Facades\Auth;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class CheckAdmin
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\
HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, $role): Response
{
if(!Auth::check()){
return redirect()->route('login');
}

$user = Auth::user();
if($user && $user->roles === $role){
return $next($request);
}
return redirect('/login')->with('error', 'Access Denied Admins only');
}
}

10. On models/user. add fillables


protected $fillable = [
'name',
'email',
'password',
'contact',
'roles',
];
11. create your recaptcha in this link
https://www.google.com/recaptcha/admin/create and generate sitekey and secretkey

12. Install the recaptcha package in your larvel. "composer require anhskohbo/no-
captcha"

13. Copy this and paste it in your env file


NOCAPTCHA_SITEKEY=
NOCAPTCHA_SECRET=

You might also like