저는 Angular 2 (JS의 큰 전문가는 아님)와 Rails 4.2 사이의 통합을 작성하고 있습니다. 레일 측면에서 나는 인증을 위해 장치를 사용하고있다. 문제는 사용자 로그인을 요청할 때 응답으로 Set-Cookie
헤더가 반환됩니다. 그러나 어떤 인증 요구 리소스를 요청하려고 할 때 Cookie
헤더의 값은 "Set-Cookie=null"
입니다 (값은 Set-Cookie
과 같습니까?). 여기에 당신이 일을 끝낼 방법은 다음과 같습니다각도 2 쿠키 인증
session.service.ts
@Injectable()
export class SessionService{
private headers = new Headers({'Content-Type': 'application/json', 'Accept': 'application/json'})
private createSessionUrl = 'http://localhost:3002/users/sign_in';
constructor(
private http: Http
) { }
create(email: string, password: string): Promise<User>{
return this.http.post(this.createSessionUrl, JSON.stringify({user:{email: email, password: password}}),
{ headers: this.headers })
.toPromise()
.then(response => response.json() as User)
.catch(this.handleError)
}
private handleError(error: any): Promise<any> {
return Promise.reject(error.message || error);
}
}
statistics.service.ts
import { Injectable } from '@angular/core';
import { Headers, Http, RequestOptions } from '@angular/http';
import { Statistics } from '../models/statistics.models'
import 'rxjs/add/operator/toPromise';
@Injectable()
export class StatisticsService{
private headers = new Headers({'Content-Type': 'application/json', 'Accept': 'application/json'})
private statisticsUrl = 'http://localhost:3002/statistics';
constructor(
private http: Http
) {}
show(): Promise<Statistics>{
let options = new RequestOptions({ headers: this.headers, withCredentials: true });
return this.http.get(this.statisticsUrl, options)
.toPromise()
.then(response => response.json() as Statistics)
.catch(this.handleError)
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
지금까지 내가 아는 한,
let options = new RequestOptions({ headers: this.headers, withCredentials: true });
요청에 적절한 쿠키를 포함해야합니다.
추가 정보가 필요한 경우 의견을 적어주십시오.