Skip to content

Instantly share code, notes, and snippets.

@misteryomi
Last active August 29, 2019 16:35
Show Gist options
  • Save misteryomi/c65384b9c06826cd832e19852ac127d5 to your computer and use it in GitHub Desktop.
Save misteryomi/c65384b9c06826cd832e19852ac127d5 to your computer and use it in GitHub Desktop.
React Lumen user authentication
<?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