0
私はIonic 2アプリで簡単なログインを開発中です。Ionic 2ログインコンポーネントと認証サービス
ログインビューをレンダリングした後、認証サービスにメールとパスワードを送信し、トークンを保存してログインコンポーネントにtrueまたはfalseを返すログインコンポーネントがあります。
マイログインコンポーネント:export class LoginPage {
email: string;
password: string;
constructor(public navCtrl: NavController, public navParams: NavParams, public authentifiction: Authentification) {}
login() {
this.authentifiction.login(this.email, this.password);
}
loginSuccess() {
console.log('Success');
}
loginError() {
console.log('Failed');
}
}
そして、私の認証サービス:
export class Authentification {
public authorized: boolean;
constructor(public http: Http, private alertCtrl: AlertController, public storage: Storage) {}
login(email, password) {
var params = 'email=' + email + '&password=' + password;
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
return this.http.post('URL', params, {
headers: headers})
.map(res => res.json())
.subscribe(
data => this.loginSuccess(data.auth_token),
error => this.loginError()
);
}
loginSuccess(auth_token) {
this.storage.set('auth_token', auth_token);
???
}
loginError() {
???
}
}
どのように私は、ログインが成功したかどう真または偽の認証サービスのリターンは応じて持つことができますか?
この全体の観測可能なことは、httpリクエストがすぐに応答しないので、あなたは、直接ブール値を返しません...まだ
観測の一種と理解しやすい説明のためにすべての感謝の最初の。私はあなたのコードを実装しましたが、私の認証サービスで私は得ます: 'Observable'を見つけることができません。他のライブラリをインポートする必要がありますか? – almo
はい、 'rxjs'のimport {Observable}を使うことができます。 – kdev
また、ログイン時に必要な応答のタイプ(trueまたはfalseなど)を追加する場合は、このHTTP POST呼び出しを実行しているサーバーのPOSTサービスで実装できます。 –