I have one VueJs project as front-end and one Laravel project as back-end using for making api and server.
I work with jwt in laravel, it returns jwt token that contains user info.
Problem:
I need to store this token in cookie with HTTPOnly flag but I don't know where and how set this cookie!? In server side (Laravel)?In Client side (VueJs)?
Codes:
My AuthController in Laravel:
public function login()
{
$credentials = request(['email', 'password']);
if (! $token = auth()->attempt($credentials)) {
return response()->json(['error' => 'Unauthorized'], 401);
}
return $this->respondWithToken($token);
}
protected function respondWithToken($token)
{
return response()->json([
'access_token' => $token,
'token_type' => 'bearer',
'expires_in' => auth()->factory()->getTTL() * 60
]);
}
it returns:
{
"access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC92dWV4LmNhY3R1c3dlYi5pclwvYXBpXC9sb2dpbiIsImlhdCI6MTU3MzkxNDA1NSwiZXhwIjoxNTczODQ3NTQ2YWEiLCJ1c2VyIjp7ImlkIjozLCJuYW1lIjoiYWxpIiwiZW1haWwiOiJhQGEuY29tIiwicm9sZSI6IjIiLCJlbWFpbF92ZXJpZmllZF9hdCI6bnVsbCwiY3JlYXRlZF9hdCI6IjIwMTktMTEtMTQgMDg6MDU6NDUiLCJ1cGRhdGVkX2F0IjoiMjAxOS0xMS0xNCAwODowNTo0NSJ9fQ.Ow785CLmaAckZR9iowVkEMX6AxBQw7JSklt3Vp1btAcG4",
"token_type": "bearer",
"expires_in": 3600
}
And in my Vuex store actions:
const actions = {
login({commit}, user) {
return new Promise((resolve, reject) => {
commit('auth_request');
Axios.post(`${api_base_url}/login`, user)
.then(resp => {
const token = resp.data.access_token;
commit('auth_success', token, user);
resolve(resp)
})
.catch(err => {
commit('auth_error');
reject(err)
})
})
},
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire