Last active
August 29, 2019 16:35
-
-
Save misteryomi/c65384b9c06826cd832e19852ac127d5 to your computer and use it in GitHub Desktop.
React Lumen user authentication
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 Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Hash; | |
use App\User; | |
use Validator; | |
class UsersController extends Controller | |
{ | |
public function register(Request $request) | |
{ | |
$validator = Validator::make($request->all(), [ | |
'name' => 'required', | |
'email' => 'required|email', | |
'password' => 'required' | |
]); | |
if($validator->fails()){ | |
return response(['message' => 'Validation errors', 'errors' => $validator->errors(), 'status' => false], 422); | |
} | |
$input = $request->all(); | |
$input['password'] = Hash::make($input['password']); | |
$user = User::create($input); | |
/**Take note of this: Your user authentication access token is generated here **/ | |
$data['token'] = $user->createToken('MyApp')->accessToken; | |
$data['name'] = $user->name; | |
return response(['data' => $data, 'message' => 'Account created successfully!', 'status' => true]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment