How can I redirect the user to home page if he successfully logged in? Also, how can I prevent the user from going back to login after they logged-in and how to prevent the user from going to home page without login, if they access the home page they will be redirected to login page?
I'm using both angular and laravel material starter but I'm still not yet quite familiar
I have this sample code
class LoginFormController {
constructor($auth, ToastService) {
'ngInject';
this.$auth = $auth;
this.ToastService = ToastService;
}
$onInit(){
this.username = '';
this.password = '';
}
login() {
let user = {
username: this.username,
password: this.password
};
this.$auth.login(user)
.then((response) => {
this.$auth.setToken(response.data);
this.ToastService.show('Logged in successfully.');
})
.catch(this.failedLogin.bind(this));
}
failedLogin(response) {
if (response.status === 422) {
for (let error in response.data.errors) {
return this.ToastService.error(response.data.errors[error][0]);
}
}
this.ToastService.error(response.statusText);
}
}
export const LoginFormComponent = {
templateUrl: './views/app/components/login-form/login-form.component.html',
controller: LoginFormController,
controllerAs: 'vm',
bindings: {}
}
Aucun commentaire:
Enregistrer un commentaire