let token = localStorage.getItem('Token');
if(token){
return this.authService.verifyToken(token).map(result => result['token'] === token);
}else{
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}
verifyToken(token: string): Observable<{}>{
let urlVerify = 'api-token-verify'
let headers = new Headers({'Content-Type':'application/json'});
let options = new RequestOptions({headers: headers});
return this.http.post(this.backendUrl+'api-token-verify/', {token}, options)
.map(res => res.json().data)
.map(result => result['token'] === token)
.catch(this.handleError);
}
handleError(error: any){
console.log('Произошла ошибка: ',error.json()['non_field_errors'][0]);
return Observable.throw(error.json()['non_field_errors'][0]);
}
let result_guard;
function() {
promise.then(function(result) {
result_guard = true;
}
if(result_guard){
return true;
}
return false;
}
canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
if(localStorage.getItem('Token')){
let token = localStorage.getItem('Token');
this.authService.verifyToken(token).subscribe(
data => {
// Я проверил, нахожусь я здесь, если всё ок. Проверял через console.log()
// Тоесть мне возвращает true.
return true;
},
error => {
for(let i=0;i<error.length;i++){
console.log(error[i]);
}
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}
);
}else{
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url }});
return false;
}
}
from rest_framework.views import exception_handler
from django.http import Http404
def custom_exception_handler(exc, context):
# Call REST framework's default exception handler first,
# to get the standard error response.
response = exception_handler(exc, context)
if isinstance(exc, Http404):
custom_response_data = {
'detail': 'Этого объекта не существует.' # custom exception message
}
response.data = custom_response_data # set the custom response data on response object
return response