Created
February 2, 2022 12:14
-
-
Save Ghostscypher/604dadd7bf512bf9f001c236085a8f82 to your computer and use it in GitHub Desktop.
Oauth2-Server app AuthController
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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