add user register/login form and authentication
This commit is contained in:
parent
13f933e0b5
commit
4752b23f35
28
ChatGPT/HTML Registration Form
Normal file
28
ChatGPT/HTML Registration Form
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
reate me a simple register form in html with these inputs: rz_username, full_name, organisation_unit, password and password confirmation
|
||||||
|
|
||||||
|
Sure, here's a simple registration form in HTML with the inputs you specified:
|
||||||
|
|
||||||
|
html
|
||||||
|
|
||||||
|
<form>
|
||||||
|
<label for="rz_username">Username:</label>
|
||||||
|
<input type="text" id="rz_username" name="rz_username" required><br><br>
|
||||||
|
|
||||||
|
<label for="full_name">Full Name:</label>
|
||||||
|
<input type="text" id="full_name" name="full_name" required><br><br>
|
||||||
|
|
||||||
|
<label for="organisation_unit">Organisation Unit:</label>
|
||||||
|
<input type="text" id="organisation_unit" name="organisation_unit" required><br><br>
|
||||||
|
|
||||||
|
<label for="password">Password:</label>
|
||||||
|
<input type="password" id="password" name="password" required><br><br>
|
||||||
|
|
||||||
|
<label for="password_confirm">Confirm Password:</label>
|
||||||
|
<input type="password" id="password_confirm" name="password_confirm" required><br><br>
|
||||||
|
|
||||||
|
<input type="submit" value="Register">
|
||||||
|
</form>
|
||||||
|
|
||||||
|
In this form, each input has a label to describe it and a unique id attribute for accessibility and styling purposes. Each input also has a name attribute, which is what will be sent to the server when the form is submitted.
|
||||||
|
|
||||||
|
Note that the password and password_confirm inputs are of type password, which will hide the text the user enters. This is important for security reasons. The required attribute is added to each input to ensure that the user fills out all the required fields before submitting the form.
|
53
device-app/app/Http/Controllers/UserController.php
Normal file
53
device-app/app/Http/Controllers/UserController.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Validation\Rule;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class UserController extends Controller
|
||||||
|
{
|
||||||
|
public function create()
|
||||||
|
{
|
||||||
|
return view('users.register');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function store(Request $request)
|
||||||
|
{
|
||||||
|
$formFields = $request->validate([
|
||||||
|
'rz_username' => ['required', Rule::unique('users', 'rz_username')],
|
||||||
|
'full_name' => ['required', 'min:3'],
|
||||||
|
'organisation_unit' => 'required',
|
||||||
|
'password' => 'required|min:6'
|
||||||
|
//'password' => 'required|confirmed|min:6'
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Hash Password
|
||||||
|
$formFields['hashed_password'] = bcrypt($formFields['password']);
|
||||||
|
|
||||||
|
$user = User::create($formFields);
|
||||||
|
|
||||||
|
auth()->login($user);
|
||||||
|
return redirect('/home');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function login()
|
||||||
|
{
|
||||||
|
return view('users.login');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function authenticate(Request $request)
|
||||||
|
{
|
||||||
|
$formFields = $request->validate([
|
||||||
|
'rz_username' => 'required',
|
||||||
|
'password' => 'required'
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (auth()->attempt($formFields)) {
|
||||||
|
$request->session()->regenerate();
|
||||||
|
|
||||||
|
return redirect('/home');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,18 +7,21 @@
|
|||||||
|
|
||||||
class Device extends Model
|
class Device extends Model
|
||||||
{
|
{
|
||||||
|
use HasFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Timestamps are disabled.
|
* The primary key associated with the devices table.
|
||||||
* @var boolean
|
|
||||||
*/
|
|
||||||
public $timestamps = false;
|
|
||||||
/**
|
|
||||||
* The primary key associated with the table Devices.
|
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $primaryKey = 'device_id';
|
protected $primaryKey = 'device_id';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array<int, string>
|
||||||
|
*/
|
||||||
protected $fillable = ['device_id', 'title', 'device_type', 'description', 'accessories', 'rz_username_buyer', 'serial_number', 'image_url'];
|
protected $fillable = ['device_id', 'title', 'device_type', 'description', 'accessories', 'rz_username_buyer', 'serial_number', 'image_url'];
|
||||||
|
|
||||||
use HasFactory;
|
//Timestamps are disabled.
|
||||||
|
public $timestamps = false;
|
||||||
}
|
}
|
@ -12,25 +12,36 @@ class User extends Authenticatable
|
|||||||
{
|
{
|
||||||
use HasApiTokens, HasFactory, Notifiable;
|
use HasApiTokens, HasFactory, Notifiable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The primary key associated with the users table.
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $primaryKey = 'rz_username';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that are mass assignable.
|
* The attributes that are mass assignable.
|
||||||
*
|
*
|
||||||
* @var array<int, string>
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
protected $fillable = [
|
protected $fillable = [
|
||||||
'name',
|
'rz_username',
|
||||||
'email',
|
'full_name',
|
||||||
'password',
|
'organisation_unit',
|
||||||
|
'has_admin_privileges',
|
||||||
|
'hashed_password'
|
||||||
];
|
];
|
||||||
|
|
||||||
|
//Timestamps are disabled.
|
||||||
|
public $timestamps = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that should be hidden for serialization.
|
* The attributes that should be hidden for serialization.
|
||||||
*
|
*
|
||||||
* @var array<int, string>
|
* @var array<int, string>
|
||||||
*/
|
*/
|
||||||
protected $hidden = [
|
protected $hidden = [
|
||||||
'password',
|
'hashed_password',
|
||||||
'remember_token',
|
//'remember_token',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,7 +49,13 @@ class User extends Authenticatable
|
|||||||
*
|
*
|
||||||
* @var array<string, string>
|
* @var array<string, string>
|
||||||
*/
|
*/
|
||||||
protected $casts = [
|
//protected $casts = [
|
||||||
'email_verified_at' => 'datetime',
|
// 'email_verified_at' => 'datetime',
|
||||||
];
|
//];
|
||||||
|
|
||||||
|
//override variable 'password'
|
||||||
|
public function getAuthPassword()
|
||||||
|
{
|
||||||
|
return $this->hashed_password;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ public function up(): void
|
|||||||
$table->string('rz_username')->unique();
|
$table->string('rz_username')->unique();
|
||||||
$table->string('full_name');
|
$table->string('full_name');
|
||||||
$table->string('organisation_unit');
|
$table->string('organisation_unit');
|
||||||
$table->boolean('has_admin_privileges');
|
$table->boolean('has_admin_privileges')->default(false);
|
||||||
$table->string('hashed_password');
|
$table->string('hashed_password');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -24,32 +24,21 @@ public function run(): void
|
|||||||
|
|
||||||
Device::factory(2)->create();
|
Device::factory(2)->create();
|
||||||
|
|
||||||
//Device::factory()->create([
|
User::create([
|
||||||
// 'device_id' => '1',
|
|
||||||
// 'title' => 'Test Device One',
|
|
||||||
// 'device_type' => 'type1',
|
|
||||||
// 'description' => 'des',
|
|
||||||
// 'accessories' => 'acc',
|
|
||||||
// 'rz_username_buyer' => 'rzb',
|
|
||||||
// 'serial_number' => '123',
|
|
||||||
// 'image_url' => 'www.url.de'
|
|
||||||
//]);
|
|
||||||
|
|
||||||
//User::create([
|
'rz_username' => 'admin',
|
||||||
// [
|
'full_name' => 'Admin',
|
||||||
// 'rz_username' => 'admin',
|
'organisation_unit' => '11111111',
|
||||||
// 'full_name' => 'Admin',
|
'has_admin_privileges' => true,
|
||||||
// 'organisation_unit' => '11111111',
|
'hashed_password' => bcrypt('vollgeheim')
|
||||||
// 'has_admin_privileges' => true,
|
]);
|
||||||
// 'hashed_password' => 'vollgeheim'
|
|
||||||
// ],
|
User::create([
|
||||||
// [
|
'rz_username' => 'user',
|
||||||
// 'rz_username' => 'user',
|
'full_name' => 'User',
|
||||||
// 'full_name' => 'User',
|
'organisation_unit' => '66666666',
|
||||||
// 'organisation_unit' => '66666666',
|
'has_admin_privileges' => false,
|
||||||
// 'has_admin_privileges' => false,
|
'hashed_password' => bcrypt('test123')
|
||||||
// 'hashed_password' => 'test123'
|
]);
|
||||||
// ]
|
|
||||||
//]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,10 @@
|
|||||||
<body>
|
<body>
|
||||||
<nav>
|
<nav>
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="/">Home</a></li>
|
<li><a href="/home">Home</a></li>
|
||||||
|
<li><a href="/register">Register</a></li>
|
||||||
|
<li><a href="/login">Login</a></li>
|
||||||
|
<li>Logout</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
<main>
|
<main>
|
||||||
|
25
device-app/resources/views/users/login.blade.php
Normal file
25
device-app/resources/views/users/login.blade.php
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
@extends('layout')
|
||||||
|
|
||||||
|
@section('content')
|
||||||
|
<h1>Login Form</h1>
|
||||||
|
<form method="POST" action="/users/authenticate">
|
||||||
|
@csrf
|
||||||
|
<label for="rz_username">RZ-Username:</label>
|
||||||
|
<input type="text" id="rz_username" name="rz_username" value="{{ old('device_id') }}" required>
|
||||||
|
@error('rz_username')
|
||||||
|
<p>{{ $message }}</p>
|
||||||
|
@enderror
|
||||||
|
<br />
|
||||||
|
<label for="hashed_password">Password:</label>
|
||||||
|
<input type="password" id="hashed_password" name="password" required>
|
||||||
|
@error('hashed_password')
|
||||||
|
<p>{{ $message }}</p>
|
||||||
|
@enderror
|
||||||
|
<br />
|
||||||
|
<input type="submit" value="Login">
|
||||||
|
<div>
|
||||||
|
<p>Don't have an account?</p>
|
||||||
|
<a href="/register">Register</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
@endsection
|
43
device-app/resources/views/users/register.blade.php
Normal file
43
device-app/resources/views/users/register.blade.php
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
@extends('layout')
|
||||||
|
|
||||||
|
@section('content')
|
||||||
|
<h1>Registration Form</h1>
|
||||||
|
<form method="POST" action="/users">
|
||||||
|
@csrf
|
||||||
|
<label for="rz_username">RZ-Username:</label>
|
||||||
|
<input type="text" id="rz_username" name="rz_username" value="{{old('device_id')}}" required>
|
||||||
|
@error('rz_username')
|
||||||
|
<p>{{$message}}</p>
|
||||||
|
@enderror
|
||||||
|
<br/>
|
||||||
|
<label for="full_name">Full Name:</label>
|
||||||
|
<input type="text" id="full_name" name="full_name" value="{{old('device_id')}}" required>
|
||||||
|
@error('full_name')
|
||||||
|
<p>{{$message}}</p>
|
||||||
|
@enderror
|
||||||
|
<br/>
|
||||||
|
<label for="organisation_unit">Organisation Unit:</label>
|
||||||
|
<input type="text" id="organisation_unit" name="organisation_unit" value="{{old('device_id')}}" required>
|
||||||
|
@error('organisation_unit')
|
||||||
|
<p>{{$message}}</p>
|
||||||
|
@enderror
|
||||||
|
<br/>
|
||||||
|
<label for="password">Password:</label>
|
||||||
|
<input type="password" id="password" name="password" required>
|
||||||
|
@error('password')
|
||||||
|
<p>{{$message}}</p>
|
||||||
|
@enderror
|
||||||
|
<br/>
|
||||||
|
{{-- <label for="password_confirm">Confirm Password:</label>
|
||||||
|
<input type="password" id="password_confirmation" name="password_confirm" required><
|
||||||
|
@error('password_confirmation')
|
||||||
|
<p>{{$message}}</p>
|
||||||
|
@enderror
|
||||||
|
<br/> --}}
|
||||||
|
<input type="submit" value="Register">
|
||||||
|
<div>
|
||||||
|
<p>Already have an account?</p>
|
||||||
|
<a href="/login">Login</a>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
@endsection
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\DeviceController;
|
use App\Http\Controllers\DeviceController;
|
||||||
|
use App\Http\Controllers\UserController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
use App\Models\Device;
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
@ -15,27 +15,30 @@
|
|||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::get('/', function () {
|
Route::get('/home', function () {
|
||||||
return view('welcome');
|
return view('welcome');
|
||||||
});
|
});
|
||||||
|
|
||||||
// index - show all devices
|
// index - show all devices
|
||||||
Route::get('/devices', [DeviceController::class, 'index']);
|
Route::get('/devices', [DeviceController::class, 'index']);
|
||||||
|
// create - show device create form
|
||||||
// create - show create form
|
|
||||||
Route::get('/devices/create', [DeviceController::class, 'create']);
|
Route::get('/devices/create', [DeviceController::class, 'create']);
|
||||||
|
|
||||||
// store - store new device
|
// store - store new device
|
||||||
Route::post('/devices', [DeviceController::class, 'store']);
|
Route::post('/devices', [DeviceController::class, 'store']);
|
||||||
|
|
||||||
// edit - show edit form
|
// edit - show edit form
|
||||||
Route::get('devices/{device}/edit', [DeviceController::class, 'edit']);
|
Route::get('devices/{device}/edit', [DeviceController::class, 'edit']);
|
||||||
|
|
||||||
// update - update device
|
// update - update device
|
||||||
Route::put('devices/{device}', [DeviceController::class, 'update']);
|
Route::put('devices/{device}', [DeviceController::class, 'update']);
|
||||||
|
|
||||||
// destroy - delete device
|
// destroy - delete device
|
||||||
Route::delete('devices/{device}', [DeviceController::class, 'destroy']);
|
Route::delete('devices/{device}', [DeviceController::class, 'destroy']);
|
||||||
|
|
||||||
// show - show sigle device
|
// show - show sigle device
|
||||||
Route::get('/devices/{device}', [DeviceController::class, 'show']);
|
Route::get('/devices/{device}', [DeviceController::class, 'show']);
|
||||||
|
|
||||||
|
// create - show register form
|
||||||
|
Route::get('/register', [UserController::class, 'create']);
|
||||||
|
// store - store new user
|
||||||
|
Route::post('/users', [UserController::class, 'store']);
|
||||||
|
// login - show user login form
|
||||||
|
Route::get('/login', [UserController::class, 'login']);
|
||||||
|
// authenticate - log in user
|
||||||
|
Route::post('/users/authenticate', [UserController::class, 'authenticate']);
|
||||||
|
Loading…
Reference in New Issue
Block a user