2017-09-04 2 views
0

쿠키로 응답 할 토큰을 생성하는 코드는 다음과 같습니다.Axios를 사용하여 쿠키 토큰을 보내는 방법은 무엇입니까?

public function authenticate(Request $request) 
{ 
    // grab credentials from the request 

    $credentials = ['email'=>$request->header('email'),'password'=>$request->header('password')]; 

    try { 
     // attempt to verify the credentials and create a token for the user 
     if (! $token = JWTAuth::attempt($credentials)) { 
      return response()->json(['error' => 'invalid_credentials'], 401); 
     } 
    } catch (JWTException $e) { 
     // something went wrong whilst attempting to encode the token 
     return response()->json(['error' => 'could_not_create_token'], 500); 
    } 

    return response($token)->cookie(
     'token',$token, 60 
    ); 

} 

그리고 저는 이것을 처리하기 위해 액시오를 사용하고 있습니다. 내 코드는 다음과 같습니다.

axios({ 
     method: 'post', 
     url: 'http://test.dev/authenticate', 
     headers : { 
     'email':'[email protected]', 
     'password':'secret' 
     }, 
     json:true, 
    }) 
axios({ 
     method: 'get', 
     url: 'http://test.dev/api/users', 
     json:true, 
     withCredentials: true 
    }); 

이제 사용자 목록을 검색하는 토큰을 보내는 방법에 문제가 있습니다. withCredentials를 추가하려고 시도했습니다. 사실이지만 운이 없습니다.

token_not_provided 응답 오류가 발생했습니다.

질문 :

  1. 어떻게 응답에서 쿠키 토큰의 값을 얻을?
  2. 권한 보유자 {토큰}을 사용하여 액시 보를 사용하여 토큰을 보내는 방법은 무엇입니까?

미리 감사드립니다.

+1

코드 자체를 업로드하는 데 도움을 얻으려면 코드 사진을 업로드하지 마십시오! 아무도 쉽게 복사 할 수있는 코드를 다시 입력 할 시간이 없습니다. – milo526

답변

0

당신은 당신의 Axios의 헤더에 토큰을 추가해야

headers: { 
    'Authorization' : 'Bearer ' + token 
} 

은 당신이 저장 확인하여 token 다시 초기 인증에서 그것을 받으면.

+0

안녕하세요, 답변 해 주셔서 감사합니다. 토큰을 저장한다는 것은 무엇을 의미합니까? 이미 쿠키 응답으로 설정했습니다. 또한 response.headers ('Set-Cookie')를 사용하여 토큰 값을 얻으려고했지만 결과는 정의되지 않았습니다. – POGI

관련 문제