Last active
November 8, 2019 16:17
-
-
Save vitorvargasdev/427ad216cd1921bd0f96b3fa51f30ba7 to your computer and use it in GitHub Desktop.
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\Validator; | |
use \App\User; | |
class UserController extends Controller | |
{ | |
public function login(Request $request) | |
{ | |
$data = $request->all(); | |
$validacao = Validator::make($data, [ | |
'email' => ['required', 'string', 'email', 'max:255'], | |
'password' => ['required', 'string'], | |
]); | |
if ($validacao->fails()) { | |
return $validacao->errors(); | |
} | |
if (Auth::attempt(['email' => $data['email'], 'password' => $data['password']])) { | |
$user = Auth::user(); | |
$user->token = $user->createToken($user->email)->accessToken; | |
return $user; | |
}else{ | |
return ['status'=>0]; | |
} | |
} | |
public function register(Request $request) | |
{ | |
$data = $request->all(); | |
$validacao = Validator::make($data, [ | |
'name' => ['required', 'string', 'max:255'], | |
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'], | |
'password' => ['required', 'string', 'min:8', 'confirmed'], | |
]); | |
if ($validacao->fails()) { | |
return $validacao->errors(); | |
} | |
$user = User::create([ | |
'name' => $data['name'], | |
'email' => $data['email'], | |
'password' => Hash::make($data['password']), | |
]); | |
$user->token = $user->createToken($user->email)->accessToken; | |
return $user; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment