webapp/app/Http/Controllers/UsersController.php

33 lines
946 B
PHP

<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
use Laravel\Lumen\Routing\Controller as BaseController;
class UsersController extends BaseController
{
public function create(Request $request)
{
try {
$data = $this->validate($request, [
'email' => 'required|email|unique:users'
]);
$token = bin2hex(random_bytes(72));
$user = new User(['email' => $data['email']]);
$user->token = $token;
$user->save();
return response()->json(['token' => $token, 'email' => $data['email']]);
} catch (ValidationException $e) {
return response()->json(['error' => $e->errors()], 422);
} catch (\Exception $e) {
return response()->json(['error' => $e->getMessage()], 422);
}
}
}