Skip to content

Instantly share code, notes, and snippets.

@Ghostscypher
Created February 2, 2022 12:14
Show Gist options
  • Save Ghostscypher/604dadd7bf512bf9f001c236085a8f82 to your computer and use it in GitHub Desktop.
Save Ghostscypher/604dadd7bf512bf9f001c236085a8f82 to your computer and use it in GitHub Desktop.
Oauth2-Server app AuthController
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class AuthController extends Controller
{
public function login(Request $request){
$validated = $request->validate([
'email' => ['bail', 'required', 'exists:users,email'],
'password' => ['bail', 'required', 'string'],
]);
if(auth()->attempt($validated)){
return redirect()->intended();
}
return back()->withErrors(['error' => 'Invalid username or password']);
}
public function register(Request $request){
$validated = $request->validate([
'email' => ['bail', 'required', 'email', 'unique:users,email'],
'name' => ['bail', 'required', 'unique:users,name'],
'password' => ['bail', 'required', 'string', 'confirmed'],
]);
User::create([
'email' => $validated['email'],
'name' => $validated['name'],
'email_validated_at' => now(),
'password' => Hash::make($validated['password']),
]);
return redirect('login')->withErrors(['success' => 'Successfully registered.']);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment