Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add form request for credentials #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Role;
use App\Models\User;
use App\Http\Requests\CredentialsRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\Exceptions\MissingAbilityException;
Expand All @@ -21,25 +22,19 @@ public function index() {
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Http\Requests\CredentialsRequest $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
$creds = $request->validate([
'email' => 'required|email',
'password' => 'required',
'name' => 'nullable|string',
]);

$user = User::where('email', $creds['email'])->first();
public function store(CredentialsRequest $request) {
$user = User::where('email', $request['email'])->first();
if ($user) {
return response(['error' => 1, 'message' => 'user already exists'], 409);
}

$user = User::create([
'email' => $creds['email'],
'password' => Hash::make($creds['password']),
'name' => $creds['name'],
'email' => $request['email'],
'password' => Hash::make($request['password']),
'name' => $request['name'],
]);

$defaultRoleSlug = config('hydra.default_user_role_slug', 'user');
Expand All @@ -51,16 +46,11 @@ public function store(Request $request) {
/**
* Authenticate an user and dispatch token.
*
* @param \Illuminate\Http\Request $request
* @param \App\Http\Requests\CredentialsRequest $request
* @return \Illuminate\Http\Response
*/
public function login(Request $request) {
$creds = $request->validate([
'email' => 'required|email',
'password' => 'required',
]);

$user = User::where('email', $creds['email'])->first();
public function login(CredentialsRequest $request) {
$user = User::where('email', $request['email'])->first();
if (! $user || ! Hash::check($request->password, $user->password)) {
return response(['error' => 1, 'message' => 'invalid credentials'], 401);
}
Expand Down
32 changes: 32 additions & 0 deletions app/Http/Requests/CredentialsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CredentialsRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'email' => 'required|email',
'password' => 'required',
'name' => 'nullable|string'
];
}
}