laravel / sanctum

Laravel Sanctum provides a featherweight authentication system for SPAs and simple APIs.
https://laravel.com/docs/sanctum
MIT License
2.76k stars 296 forks source link

after Login with santcum go to Home #133

Closed jackpit93 closed 4 years ago

jackpit93 commented 4 years ago

Description:

this is my vue login file:

<template>
  <div class="home">
    <form action="#" method="POST">
        <div><input type="email" name="email" placeholder="email"></div>
        <div><input type="password" name="password" placeholder="password"></div>
        <div><button type="submit" @click.prevent="login">login</button></div>

    </form>
  </div>
</template>

<script>

import axios from 'axios'

axios.defaults.withCredentials = true;
axios.defaults.baseURL ='http://localhost'
export default {
  name: 'Home',

  methods:{
    login(){
        axios.get('/sanctum/csrf-cookie').then(response => {
           axios.post('/login',{
            email:'admin@admin.com',
            password:'123456'
          })
          .then(response=>{
            console.log(response)
          })
        });
    }
  }

}
</script>

after login request with axios laravel go to home url and i have error in console image

I'm sure the password and username are correct. Shouldn't html be returned to me when I use axios. image

driesvints commented 4 years ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.

jackpit93 commented 4 years ago

please see this : https://stackoverflow.com/questions/tagged/laravel-sanctum

Most users have the same problem and no one has given the right answer.It is a good idea to provide a solution to this problem instead of sending a duplicate message.

harrysbaraini commented 4 years ago

It's hard to provide a solution for your error just by looking your vue component. A lot of things may be going on, like cors, Auth middleware, content negotiation.

About redirection to /home, ensure you're sending proper Accept and ContentType headers (application/json).

About 401, I had a similar issue, but I eventually found out that I wasn't using auth guards in the right way.

Dartui commented 4 years ago

"Problem" is in RedirectIfAuthenticated middleware, which is aliased to guest. Base LoginController is protected by guest middleware, so if you once logged in you will hit this part of code every time you try to log in again:

        if (Auth::guard($guard)->check()) {
            return redirect(RouteServiceProvider::HOME);
        }
Asepdadan commented 3 years ago

in local work, but production use shared hosting not work, where that's wrong'

{ "message": "Unauthenticated." }

sausin commented 5 months ago

The following updated code inside the RedirectIfAuthenticated controller works:

if (Auth::guard($guard)->check()) {
    if ($request->wantsJson()) {
        return response()->json(['result' => 'success'], 200);
    }

    return redirect(RouteServiceProvider::HOME);
 }