add user register/login form and authentication
This commit is contained in:
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 @@ use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
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<int, string>
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* The primary key associated with the users table.
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'rz_username';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
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<int, string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
'hashed_password',
|
||||
//'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
@ -38,7 +49,13 @@ class User extends Authenticatable
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
protected $casts = [
|
||||
'email_verified_at' => 'datetime',
|
||||
];
|
||||
//protected $casts = [
|
||||
// 'email_verified_at' => 'datetime',
|
||||
//];
|
||||
|
||||
//override variable 'password'
|
||||
public function getAuthPassword()
|
||||
{
|
||||
return $this->hashed_password;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user