Skip to content

Instantly share code, notes, and snippets.

@tiagoboeing
Last active October 16, 2018 01:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiagoboeing/eafe457640b9732cf547e5e21fbf1246 to your computer and use it in GitHub Desktop.
Save tiagoboeing/eafe457640b9732cf547e5e21fbf1246 to your computer and use it in GitHub Desktop.
Login request Laravel with JWT
// cadastrar usuário
public function register(Request $request){
$json = $request->getContent();
$params = json_decode($json);
// instancia usuário
$user = new User();
$user->nome = $params->nome;
$user->sobrenome = $params->sobrenome;
$user->email = $params->email;
// criptografa senha
$user->senha = hash('sha256', $params->senha);
// verifica se email e senha foram informados
if((isset($user->email) && !is_null($user->email)) && (isset($user->senha) && !is_null($user->senha))){
// busca contas duplicadas
$isset_user = User::where('email', '=', $params->email)->first();
if(count($isset_user) > 0){
$data = array(
'status' => 'error',
'code' => 400,
'message' => 'Usuário duplicado'
);
} else {
$user->save();
$data = array(
'status' => 'success',
'code' => 200,
'message' => 'Conta criada'
);
}
}
return response()->json($data, $data['code']);
}
public function login(Request $request){
$jwtAuth = new JwtAuth();
//Receber POST
$json = $request->input('json', null);
$params = json_decode($json);
$email = (!is_null($json) && isset($request->email)) ? $request->email : null;
$password = (!is_null($json) && isset($request->password)) ? $request->password : null;
$getToken = (!is_null($json) && isset($request->gettoken)) ? $request->gettoken : null;
//Criptografar a senha
$pwd = hash('sha256', $password);
if(!is_null($email) && !is_null($password) && ($getToken == null || $getToken == 'false')){
$signup = $jwtAuth->signup($email, $pwd);
}elseif($getToken != null){
$signup = $jwtAuth->signup($email, $pwd, $getToken);
}else{
$signup = array(
'status' => 'error',
'code' => 400,
'message' => 'Envie seus dados por POST'
);
}
return response()->json($signup, $signup['code']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment