import Service from 'radio.service';
const AuthService = Service.extend({
start() {
this.user = new User();
return this.user.fetch();
},
requests: {
isAuthenticated: 'isAuthenticated',
authenticate: 'authenticate'
},
isAuthenticated() {
return this.user.get('isAuthenticated');
},
authenticate() {
this.user.authenticate();
},
onError(err) {
console.log('Err!', err);
}
});
const authService = new AuthService();
const Page = View.extend({
render() {
authService.request('isAuthenticated').then(isAuthenticated => {
if (isAuthenticated) {
this.$el.html('Welcome!');
return;
}
this.$el.html('Permission denied.')
return authService.request('authenticate').then(() => this.render());
}).catch(err => {
this.$el.html('Oh no!');
});
}
});
const Page = View.extend({
render() {
Promise.resolve()
.then(() => {
if (!authService.isStarted) {
return authService.start().catch(err => {
authService.onError(err);
throw err;
}))
}
})
.then(() => authService.isAuthenticated().catch(err => {
authService.onError(err);
throw err;
})))
.then(isAuthenticated => {
if (isAuthenticated) {
this.$el.html('Welcome!');
return;
}
this.$el.html('Permission denied.')
return Promise.resolve()
.then(() => authService.authenticate().catch(err => {
authService.onError(err);
throw err;
}))
.then(() => this.render());
}
}).catch(err => {
this.$el.html('Oh no!');
});
}
});