From 4752b23f359079f5123e5b51f59cf089fb7fdb72 Mon Sep 17 00:00:00 2001 From: TimmensOne Date: Mon, 27 Mar 2023 15:29:49 +0200 Subject: [PATCH] add user register/login form and authentication --- ChatGPT/HTML Registration Form | 28 ++++++++++ .../app/Http/Controllers/UserController.php | 53 +++++++++++++++++++ device-app/app/Models/Device.php | 17 +++--- device-app/app/Models/User.php | 33 +++++++++--- .../2014_10_12_000000_create_users_table.php | 2 +- .../database/seeders/DatabaseSeeder.php | 41 ++++++-------- device-app/resources/views/layout.blade.php | 5 +- .../resources/views/users/login.blade.php | 25 +++++++++ .../resources/views/users/register.blade.php | 43 +++++++++++++++ device-app/routes/web.php | 21 ++++---- 10 files changed, 216 insertions(+), 52 deletions(-) create mode 100644 ChatGPT/HTML Registration Form create mode 100644 device-app/app/Http/Controllers/UserController.php create mode 100644 device-app/resources/views/users/login.blade.php create mode 100644 device-app/resources/views/users/register.blade.php diff --git a/ChatGPT/HTML Registration Form b/ChatGPT/HTML Registration Form new file mode 100644 index 0000000..570c0d4 --- /dev/null +++ b/ChatGPT/HTML Registration Form @@ -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 + +
+ +

+ + +

+ + +

+ + +

+ + +

+ + +
+ +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. \ No newline at end of file diff --git a/device-app/app/Http/Controllers/UserController.php b/device-app/app/Http/Controllers/UserController.php new file mode 100644 index 0000000..c8495f0 --- /dev/null +++ b/device-app/app/Http/Controllers/UserController.php @@ -0,0 +1,53 @@ +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'); + } + } +} diff --git a/device-app/app/Models/Device.php b/device-app/app/Models/Device.php index 627f121..bb38700 100644 --- a/device-app/app/Models/Device.php +++ b/device-app/app/Models/Device.php @@ -7,18 +7,21 @@ class Device extends Model { + use HasFactory; + /** - * Timestamps are disabled. - * @var boolean - */ - public $timestamps = false; - /** - * The primary key associated with the table Devices. + * The primary key associated with the devices table. * @var string */ protected $primaryKey = 'device_id'; + /** + * The attributes that are mass assignable. + * + * @var array + */ protected $fillable = ['device_id', 'title', 'device_type', 'description', 'accessories', 'rz_username_buyer', 'serial_number', 'image_url']; - use HasFactory; + //Timestamps are disabled. + public $timestamps = false; } \ No newline at end of file diff --git a/device-app/app/Models/User.php b/device-app/app/Models/User.php index 23b4063..d191d05 100644 --- a/device-app/app/Models/User.php +++ b/device-app/app/Models/User.php @@ -12,25 +12,36 @@ class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable; + /** + * The primary key associated with the users table. + * @var string + */ + protected $primaryKey = 'rz_username'; + /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ - 'name', - 'email', - 'password', + 'rz_username', + 'full_name', + 'organisation_unit', + 'has_admin_privileges', + 'hashed_password' ]; + //Timestamps are disabled. + public $timestamps = false; + /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ - 'password', - 'remember_token', + 'hashed_password', + //'remember_token', ]; /** @@ -38,7 +49,13 @@ class User extends Authenticatable * * @var array */ - protected $casts = [ - 'email_verified_at' => 'datetime', - ]; + //protected $casts = [ + // 'email_verified_at' => 'datetime', + //]; + + //override variable 'password' + public function getAuthPassword() + { + return $this->hashed_password; + } } diff --git a/device-app/database/migrations/2014_10_12_000000_create_users_table.php b/device-app/database/migrations/2014_10_12_000000_create_users_table.php index 9406518..5731486 100644 --- a/device-app/database/migrations/2014_10_12_000000_create_users_table.php +++ b/device-app/database/migrations/2014_10_12_000000_create_users_table.php @@ -15,7 +15,7 @@ public function up(): void $table->string('rz_username')->unique(); $table->string('full_name'); $table->string('organisation_unit'); - $table->boolean('has_admin_privileges'); + $table->boolean('has_admin_privileges')->default(false); $table->string('hashed_password'); }); } diff --git a/device-app/database/seeders/DatabaseSeeder.php b/device-app/database/seeders/DatabaseSeeder.php index 346f4c1..db15d0b 100644 --- a/device-app/database/seeders/DatabaseSeeder.php +++ b/device-app/database/seeders/DatabaseSeeder.php @@ -24,32 +24,21 @@ public function run(): void Device::factory(2)->create(); - //Device::factory()->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([ - //User::create([ - // [ - // 'rz_username' => 'admin', - // 'full_name' => 'Admin', - // 'organisation_unit' => '11111111', - // 'has_admin_privileges' => true, - // 'hashed_password' => 'vollgeheim' - // ], - // [ - // 'rz_username' => 'user', - // 'full_name' => 'User', - // 'organisation_unit' => '66666666', - // 'has_admin_privileges' => false, - // 'hashed_password' => 'test123' - // ] - //]); + 'rz_username' => 'admin', + 'full_name' => 'Admin', + 'organisation_unit' => '11111111', + 'has_admin_privileges' => true, + 'hashed_password' => bcrypt('vollgeheim') + ]); + + User::create([ + 'rz_username' => 'user', + 'full_name' => 'User', + 'organisation_unit' => '66666666', + 'has_admin_privileges' => false, + 'hashed_password' => bcrypt('test123') + ]); } } diff --git a/device-app/resources/views/layout.blade.php b/device-app/resources/views/layout.blade.php index 630b1dc..001735a 100644 --- a/device-app/resources/views/layout.blade.php +++ b/device-app/resources/views/layout.blade.php @@ -8,7 +8,10 @@
diff --git a/device-app/resources/views/users/login.blade.php b/device-app/resources/views/users/login.blade.php new file mode 100644 index 0000000..01572d3 --- /dev/null +++ b/device-app/resources/views/users/login.blade.php @@ -0,0 +1,25 @@ +@extends('layout') + +@section('content') +

Login Form

+
+ @csrf + + + @error('rz_username') +

{{ $message }}

+ @enderror +
+ + + @error('hashed_password') +

{{ $message }}

+ @enderror +
+ +
+

Don't have an account?

+ Register +
+
+@endsection diff --git a/device-app/resources/views/users/register.blade.php b/device-app/resources/views/users/register.blade.php new file mode 100644 index 0000000..e407240 --- /dev/null +++ b/device-app/resources/views/users/register.blade.php @@ -0,0 +1,43 @@ +@extends('layout') + +@section('content') +

Registration Form

+
+ @csrf + + + @error('rz_username') +

{{$message}}

+ @enderror +
+ + + @error('full_name') +

{{$message}}

+ @enderror +
+ + + @error('organisation_unit') +

{{$message}}

+ @enderror +
+ + + @error('password') +

{{$message}}

+ @enderror +
+ {{-- + < + @error('password_confirmation') +

{{$message}}

+ @enderror +
--}} + +
+

Already have an account?

+ Login +
+
+@endsection diff --git a/device-app/routes/web.php b/device-app/routes/web.php index 1d564ad..b76dcd0 100644 --- a/device-app/routes/web.php +++ b/device-app/routes/web.php @@ -1,8 +1,8 @@