link2877 link2878 link2879 link2880 link2881 link2882 link2883 link2884 link2885 link2886 link2887 link2888 link2889 link2890 link2891 link2892 link2893 link2894 link2895 link2896 link2897 link2898 link2899 link2900 link2901 link2902 link2903 link2904 link2905 link2906 link2907 link2908 link2909 link2910 link2911 link2912 link2913 link2914 link2915 link2916 link2917 link2918 link2919 link2920 link2921 link2922 link2923 link2924 link2925 link2926 link2927 link2928 link2929 link2930 link2931 link2932 link2933 link2934 link2935 link2936 link2937 link2938 link2939 link2940 link2941 link2942 link2943 link2944 link2945 link2946 link2947 link2948 link2949 link2950 link2951 link2952 link2953 link2954 link2955 link2956 link2957 link2958 link2959 link2960 link2961 link2962 link2963 link2964 link2965 link2966 link2967 link2968 link2969 link2970 link2971 link2972 link2973 link2974 link2975 link2976 link2977 link2978 link2979 link2980 link2981 link2982 link2983 link2984 link2985 link2986 link2987 link2988 link2989 link2990 link2991 link2992 link2993 link2994 link2995 link2996 link2997 link2998 link2999 link3000 link3001 link3002 link3003 link3004 link3005 link3006 link3007 link3008 link3009 link3010 link3011 link3012 link3013

[Vue.js] authorization token for user performing auth needed operation - Laravel Passport Subscribe to RSS

First, when not sure authorization token is the real problem or not. when using passport and giving token for the auth. There is no problem with that part. User can register, login or logout. Also logged in user seeing pages only auth user can see. But when it comes to like or dislike an article. This error popping up. Route [login] not defined. it seems like in my controller below. this part is not working properly. Auth::user()->id

public function postLikeArticle( $id ){
$article = Article::where(‘id’, ‘=’, $id)->first();

if( !$article->likes->contains( Auth::user()->id ) ){

$article->likes()->attach( Auth::user()->id, [
‘created_at’ => date(‘Y-m-d H:i:s’),
‘updated_at’ => date(‘Y-m-d H:i:s’)
] );
}

return response()->json( [‘article_liked’ => true], 201 );
}

When I like the post when having 401 (Unauthorized) and my request header is looks like below.

No authorization token in the request header. And this makes me think that when doing something wrong at login part in my vuex. Which is like below.

export const articles = {

state: {
token: localStorage.getItem(‘access_token’) || null,
},
getters: {
loggedIn(state){
return state.token !== null;
}
},
mutations: {

fetchToken(state, token) {
state.token = token
},

},

actions: {
fetchToken(context, credentials){

return new Promise((resolve, reject) => {
axios.post(“/api/v1/login”, {
username:credentials.username,
password:credentials.password,
})
.then(response => {
const token = response.data.access_token
localStorage.setItem(‘access_token’, token)
context.commit(“fetchToken”, token)

resolve(response)

axios.defaults.headers.common[‘Authorization’] = ‘Bearer ‘ + context.state.token

})
.catch(error => {
console.log(error)
reject(error)
})
})
},
},
}

when not sure what when doing wrong. but it seems Auth:: is doesn’t know user logged in… When I logged in at first authorization token is there. But when I refresh the page it’s gone.

Solution :

Also logged in user seeing pages only auth user can see.

How are you doing this? If the user can see what auth user can see, which means you are making GET request with auth token right? If you’re using a passport, you should put the token into the Authorization header.

axios.defaults.headers.common.Authorization = `Bearer ${token}`;

Use this to put the token into the all axios request after you have login, then you should be good to go.