| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Carbon;
- use Firebase\JWT\JWT;
- use Firebase\JWT\Key;
- class LoginController extends Controller{
- private $responseController;
- private $encryptionController;
- private $secretKey = "ydl27x22cNsNY0z6o3Fr6XZoUvsX0QMZx6MaiwN+KCnM6APS4Xbb7GDfudOYD5uD/r8TzQElh4d4HIal5Os0XA==";
- public function __construct(
- $responseController = new ResponseController(),
- $encryptionController = new EncryptionController(),
- ){
- $this->responseController = $responseController;
- $this->encryptionController = $encryptionController;
- }
- public function login(Request $request){
- $validator = Validator::make($request->all(), [
- 'email' => 'required|string|email',
- 'password' => 'required|string'
- ]);
- if($validator->fails()){
- return $this->responseController->makeResponse(
- true,
- "Se encontraron uno o más errores.",
- $this->responseController->makeErrors(
- $validator->errors()->messages()
- ),
- 401
- );
- }
- $login = $request->all();
- $usr = DB::table('samusua')->where('USUA_EMAI', '=', $login['email'])->first();
- if(is_null($usr)){
- return $this->responseController->makeResponse(true, "El correo electrónico no está registrado.", [], 404);
- }
- $contra = $usr->USUA_CONT;
- if(!Hash::check($login['password'], $contra)){
- return $this->responseController->makeResponse(true, "La contraseña es incorrecta.", [], 401);
- }
- $now = Carbon::now('America/Mexico_city');
- $iat = $now->timestamp;
- $cad = $now->addDay()->timestamp;
- $payload = [
- "iss" => $login['email'],
- "aud" => "dominio.syp.mx",
- "iat" => $iat,
- "cad" => $cad
- ];
- $token = JWT::encode($payload, $this->secretKey, 'EdDSA');
- return $this->responseController->makeResponse(false, "EXITO.", [
- "IDUSUARIO" => $this->encryptionController->encrypt($usr->USUA_IDUS),
- "NOMBREUSUARIO" => $this->encryptionController->encrypt($usr->USUA_NOMB),
- "CORREO" => $this->encryptionController->encrypt($usr->USUA_EMAI),
- "TOKEN" => $token,
- ]);
- }
- }
|