69 lines
1.9 KiB
PHP
69 lines
1.9 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Http\Controllers;
|
||
|
|
||
|
use App\Models\Privilege;
|
||
|
use App\Models\User;
|
||
|
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
|
||
|
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||
|
use Illuminate\Foundation\Validation\ValidatesRequests;
|
||
|
use Illuminate\Http\Request;
|
||
|
use Illuminate\Routing\Controller as BaseController;
|
||
|
use Illuminate\Support\Facades\Auth;
|
||
|
use Illuminate\Support\Facades\Hash;
|
||
|
|
||
|
class PublicController extends BaseController
|
||
|
{
|
||
|
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
|
||
|
|
||
|
public function loginView() {
|
||
|
if(Auth::check()) {
|
||
|
return redirect("/dashboard");
|
||
|
}
|
||
|
return view("public.login");
|
||
|
}
|
||
|
|
||
|
public function registerView(Request $request) {
|
||
|
return view("public.register");
|
||
|
}
|
||
|
|
||
|
public function register(Request $request) {
|
||
|
$validatedData = $request->validate([
|
||
|
'email' => ['required', 'email:rfc,dns'],
|
||
|
'password' => ['required'],
|
||
|
'password2' => ['required', 'same:password']
|
||
|
]);
|
||
|
|
||
|
//How Many user already exists, check to make the first user admin
|
||
|
$userCount = User::query()->count('id');
|
||
|
|
||
|
$user = new User();
|
||
|
$user->email = $validatedData["email"];
|
||
|
$user->password = Hash::make($validatedData["password"]);
|
||
|
$user->saveOrFail();
|
||
|
|
||
|
$privilege = new Privilege();
|
||
|
$privilege->user_id = $user->id;
|
||
|
if($userCount == 0) {
|
||
|
$privilege->admin = true;
|
||
|
$privilege->createVPN = true;
|
||
|
}
|
||
|
$privilege->saveOrFail();
|
||
|
|
||
|
return redirect("/login");
|
||
|
}
|
||
|
|
||
|
public function login(Request $request) {
|
||
|
$validatedData = $request->validate([
|
||
|
'email' => ['required', 'email:rfc'],
|
||
|
'password' => ['required']
|
||
|
]);
|
||
|
|
||
|
if(Auth::attempt($validatedData)) {
|
||
|
return redirect("/dashboard");
|
||
|
}
|
||
|
|
||
|
return view("public.login", ["msg"=> "Login failed"]);
|
||
|
}
|
||
|
}
|