kuvia/app/Http/Controllers/AccountController.php

73 lines
2.1 KiB
PHP
Raw Permalink Normal View History

2021-01-12 14:49:45 +00:00
<?php
namespace App\Http\Controllers;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
class AccountController extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function registerView() {
2021-01-12 19:56:51 +00:00
if(!env('REGISTER_ENABLED')){
return abort(401);
}
2021-01-12 14:49:45 +00:00
return view("account.register");
}
public function register(Request $request) {
2021-01-12 19:56:51 +00:00
if(!env('REGISTER_ENABLED')){
return abort(401);
}
2021-01-12 14:49:45 +00:00
$validated = $request->validate([
'username' => 'required|unique:users|unique:tenants,url|regex:/^[a-z0-9]{8,30}$/i',
'email' => 'required|unique:users|email:rfc,dns',
'password' => 'required|min:8|confirmed',
]);
$user = new User();
$user->password = Hash::make($validated["password"]);
$user->username = $validated["username"];
$user->email = $validated["email"];
$user->saveOrFail();
$tenant = new Tenant();
$tenant->name = $validated["username"];
$tenant->url = $validated["username"];
$tenant->template = "default";
$tenant->owner = $user->id;
$tenant->saveOrFail();
2021-01-20 18:51:12 +00:00
Auth::attempt(["username" => $user->username, "password" => $validated["password"]]);
return redirect("/d");
2021-01-12 14:49:45 +00:00
}
public function loginView() {
2021-01-20 18:51:12 +00:00
if(Auth::check()) {
return redirect("/d");
}
2021-01-12 14:49:45 +00:00
return view("account.login");
}
public function login(Request $request) {
$credentials = $request->only('username', 'password');
if (Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect("/d");
}
return back()->withErrors([
'username' => 'The provided credentials do not match our records.',
]);
}
}