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

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                      Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript