Laravel login - Fail condition isn't workingWhy does AuthorizeAttribute redirect to the login page for authentication and authorization failures?Reference - What does this error mean in PHP?Laravel - Manual user loginLogin Auth in laravel customsize (SOLUTION)Laravel Login & Registration with Error CodeCan I log a user in with post request rather than through Laravel login page by defaultHow do I log in a user in Laravel?How do I manually authenticate user in Laravel 5.4Laravel 5.5 basic auth login exceptionLaravel redirect failed login: Method loginUsername does not exist

Did Joe Biden "stop a prosecution" into his son in Ukraine? And did he brag about stopping the prosecution?

How to prove (A v B), (A → C), (B → D) therefore (C v D)

Does the DOJ's declining to investigate the Trump-Zelensky call ruin the basis for impeachment?

Is power set functor determined by its image on objects?

Could Boris Johnson face criminal charges for illegally proroguing Parliament?

Can I voluntarily exit from the US after a 20 year overstay, or could I be detained at the airport?

How to define the line style in advance of plot function

Found a minor bug, affecting 1% of users. What should QA do?

Where does the image of a data connector as a sharp metal spike originate from?

Can 35 mm film which went through a washing machine still be developed?

How come the Russian cognate for the Czech word "čerstvý" (fresh) means entirely the opposite thing (stale)?

How to "Start as close to the end as possible", and why to do so?

How to tension rope between two trees?

Are there any tricks to pushing a grand piano?

Using 4K Skyrim Textures when running 1920 x 1080 display resolution?

Why do many websites hide input when entering a OTP

Is there any problem with students seeing faculty naked in university gym?

How to be productive while waiting for meetings to start

Redirect output on-the-fly - looks not possible in Linux, why?

As an interviewer, how to conduct interviews with candidates you already know will be rejected?

C - Learning Linked Lists, Pointer Manipulation - Store some ints, print and free memory

Anonymous reviewer disclosed his identity. Should I thank him by name?

Generating Roman numerals with dice

The decoration of language in proposal writing



Laravel login - Fail condition isn't working


Why does AuthorizeAttribute redirect to the login page for authentication and authorization failures?Reference - What does this error mean in PHP?Laravel - Manual user loginLogin Auth in laravel customsize (SOLUTION)Laravel Login & Registration with Error CodeCan I log a user in with post request rather than through Laravel login page by defaultHow do I log in a user in Laravel?How do I manually authenticate user in Laravel 5.4Laravel 5.5 basic auth login exceptionLaravel redirect failed login: Method loginUsername does not exist






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0















I'm creating a login api in Laravel.



I have a login method



public function authenticate(Request $request)

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials))
return response()->json(auth()->user());


return Response::json(['message' => 'Failed to authenticate'], 401);



If the login fails it doesn't reach the 401 error. Any reason why?










share|improve this question


























  • What version of Laravel are you using?

    – Rwd
    Mar 28 at 21:19











  • @RossWilson 5.8

    – user11161143
    Mar 28 at 21:19











  • You should show us what are you getting instead of 401 error, and how are you getting it

    – Snickfire
    Mar 28 at 21:26











  • Did you import the Response facade? After a successful attempt, you use the response() helper instead.

    – Joel Hinz
    Mar 28 at 21:33











  • use try ... catch for catch error if anything missing/wrong in your code

    – Ajay
    Mar 30 at 10:51


















0















I'm creating a login api in Laravel.



I have a login method



public function authenticate(Request $request)

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials))
return response()->json(auth()->user());


return Response::json(['message' => 'Failed to authenticate'], 401);



If the login fails it doesn't reach the 401 error. Any reason why?










share|improve this question


























  • What version of Laravel are you using?

    – Rwd
    Mar 28 at 21:19











  • @RossWilson 5.8

    – user11161143
    Mar 28 at 21:19











  • You should show us what are you getting instead of 401 error, and how are you getting it

    – Snickfire
    Mar 28 at 21:26











  • Did you import the Response facade? After a successful attempt, you use the response() helper instead.

    – Joel Hinz
    Mar 28 at 21:33











  • use try ... catch for catch error if anything missing/wrong in your code

    – Ajay
    Mar 30 at 10:51














0












0








0








I'm creating a login api in Laravel.



I have a login method



public function authenticate(Request $request)

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials))
return response()->json(auth()->user());


return Response::json(['message' => 'Failed to authenticate'], 401);



If the login fails it doesn't reach the 401 error. Any reason why?










share|improve this question
















I'm creating a login api in Laravel.



I have a login method



public function authenticate(Request $request)

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials))
return response()->json(auth()->user());


return Response::json(['message' => 'Failed to authenticate'], 401);



If the login fails it doesn't reach the 401 error. Any reason why?







php laravel authentication laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 21:19

























asked Mar 28 at 21:11







user11161143






















  • What version of Laravel are you using?

    – Rwd
    Mar 28 at 21:19











  • @RossWilson 5.8

    – user11161143
    Mar 28 at 21:19











  • You should show us what are you getting instead of 401 error, and how are you getting it

    – Snickfire
    Mar 28 at 21:26











  • Did you import the Response facade? After a successful attempt, you use the response() helper instead.

    – Joel Hinz
    Mar 28 at 21:33











  • use try ... catch for catch error if anything missing/wrong in your code

    – Ajay
    Mar 30 at 10:51


















  • What version of Laravel are you using?

    – Rwd
    Mar 28 at 21:19











  • @RossWilson 5.8

    – user11161143
    Mar 28 at 21:19











  • You should show us what are you getting instead of 401 error, and how are you getting it

    – Snickfire
    Mar 28 at 21:26











  • Did you import the Response facade? After a successful attempt, you use the response() helper instead.

    – Joel Hinz
    Mar 28 at 21:33











  • use try ... catch for catch error if anything missing/wrong in your code

    – Ajay
    Mar 30 at 10:51

















What version of Laravel are you using?

– Rwd
Mar 28 at 21:19





What version of Laravel are you using?

– Rwd
Mar 28 at 21:19













@RossWilson 5.8

– user11161143
Mar 28 at 21:19





@RossWilson 5.8

– user11161143
Mar 28 at 21:19













You should show us what are you getting instead of 401 error, and how are you getting it

– Snickfire
Mar 28 at 21:26





You should show us what are you getting instead of 401 error, and how are you getting it

– Snickfire
Mar 28 at 21:26













Did you import the Response facade? After a successful attempt, you use the response() helper instead.

– Joel Hinz
Mar 28 at 21:33





Did you import the Response facade? After a successful attempt, you use the response() helper instead.

– Joel Hinz
Mar 28 at 21:33













use try ... catch for catch error if anything missing/wrong in your code

– Ajay
Mar 30 at 10:51






use try ... catch for catch error if anything missing/wrong in your code

– Ajay
Mar 30 at 10:51













2 Answers
2






active

oldest

votes


















0
















Try



public function authenticate(Request $request)

$credentials = $request->only('email', 'password');

if (Auth::attempt($credentials))
return response()->json(auth()->user());

else
return Response::json(['message' => 'Failed to authenticate'], 401);




I would also dd() your auth() and make sure things are being pass as intended. Are you passing your CSRF token?



Also do you have your Response loaded using Use ../../Response; at the top of the controller?






share|improve this answer


































    0
















    As @kray said, use dd() or echo"";print_r($credentials) to see if data is passed correctly. If not , then may be you haven't include csrf_field() in your form.






    share|improve this answer


























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );














      draft saved

      draft discarded
















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406898%2flaravel-login-fail-condition-isnt-working%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown
























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0
















      Try



      public function authenticate(Request $request)

      $credentials = $request->only('email', 'password');

      if (Auth::attempt($credentials))
      return response()->json(auth()->user());

      else
      return Response::json(['message' => 'Failed to authenticate'], 401);




      I would also dd() your auth() and make sure things are being pass as intended. Are you passing your CSRF token?



      Also do you have your Response loaded using Use ../../Response; at the top of the controller?






      share|improve this answer































        0
















        Try



        public function authenticate(Request $request)

        $credentials = $request->only('email', 'password');

        if (Auth::attempt($credentials))
        return response()->json(auth()->user());

        else
        return Response::json(['message' => 'Failed to authenticate'], 401);




        I would also dd() your auth() and make sure things are being pass as intended. Are you passing your CSRF token?



        Also do you have your Response loaded using Use ../../Response; at the top of the controller?






        share|improve this answer





























          0














          0










          0









          Try



          public function authenticate(Request $request)

          $credentials = $request->only('email', 'password');

          if (Auth::attempt($credentials))
          return response()->json(auth()->user());

          else
          return Response::json(['message' => 'Failed to authenticate'], 401);




          I would also dd() your auth() and make sure things are being pass as intended. Are you passing your CSRF token?



          Also do you have your Response loaded using Use ../../Response; at the top of the controller?






          share|improve this answer















          Try



          public function authenticate(Request $request)

          $credentials = $request->only('email', 'password');

          if (Auth::attempt($credentials))
          return response()->json(auth()->user());

          else
          return Response::json(['message' => 'Failed to authenticate'], 401);




          I would also dd() your auth() and make sure things are being pass as intended. Are you passing your CSRF token?



          Also do you have your Response loaded using Use ../../Response; at the top of the controller?







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 29 at 0:21

























          answered Mar 29 at 0:15









          KrayKray

          4389 silver badges21 bronze badges




          4389 silver badges21 bronze badges


























              0
















              As @kray said, use dd() or echo"";print_r($credentials) to see if data is passed correctly. If not , then may be you haven't include csrf_field() in your form.






              share|improve this answer





























                0
















                As @kray said, use dd() or echo"";print_r($credentials) to see if data is passed correctly. If not , then may be you haven't include csrf_field() in your form.






                share|improve this answer



























                  0














                  0










                  0









                  As @kray said, use dd() or echo"";print_r($credentials) to see if data is passed correctly. If not , then may be you haven't include csrf_field() in your form.






                  share|improve this answer













                  As @kray said, use dd() or echo"";print_r($credentials) to see if data is passed correctly. If not , then may be you haven't include csrf_field() in your form.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 30 at 10:11









                  iamawesomeiamawesome

                  1476 bronze badges




                  1476 bronze badges































                      draft saved

                      draft discarded















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406898%2flaravel-login-fail-condition-isnt-working%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      위키백과:대문 둘러보기 메뉴기부 안내모바일판 대문크리에이티브 커먼즈 저작자표시-동일조건변경허락 3.0CebuanoDeutschEnglishEspañolFrançaisItaliano日本語NederlandsPolskiPortuguêsРусскийSvenskaTiếng ViệtWinaray中文العربيةCatalàفارسیSrpskiУкраїнськаБългарскиНохчийнČeštinaDanskEsperantoEuskaraSuomiעבריתMagyarՀայերենBahasa IndonesiaҚазақшаBaso MinangkabauBahasa MelayuBân-lâm-gúNorskRomânăSrpskohrvatskiSlovenčinaTürkçe

                      용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh