0
我正在开发我的Ionic 2应用程序中的简单登录。Ionic 2登录组件和身份验证服务
我有我的渲染login视图,然后发送电子邮件和密码的身份验证服务,将验证,保存令牌,并返回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() {
???
}
}
我怎么能有权威性的服务回报true或false取决于如果登录成功与否?
这整个观察到的东西还是有点混乱,我...
第一。我实现了你的代码,但是在我的auth服务中,我得到:无法找到'Observable'。我是否需要导入其他库? – almo
是的,你可以使用'rxjs'中的import {Observable}; – kdev
此外,我想补充说,你想要的响应类型,如登录时的true或false,你可以在你的服务器的POST服务上实现它,你正在做这个HTTP POST调用。 –