0

I am using angular 5 on the front end. With an HTTP Interceptor which if the client is logged in, sends a token along with any http request made to my server. I am able to log into my system. So the JWT scheme is working correctly.

The interceptor is as so: I console log the token to make sure it is being added, and it is working correctly in my logs.

import { Injectable } from '@angular/core';
import {
  HttpRequest,
  HttpHandler,
  HttpEvent,
  HttpInterceptor
} from '@angular/common/http';
import {UserAuthorizationService} from "../userservice/userauthorizationservice/userauthorizationservice";
import { Observable } from 'rxjs/Observable';

@Injectable()
export class TokenInterceptor implements HttpInterceptor{
  constructor(private tokenservice: UserAuthorizationService){}

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>>{
    let token = this.tokenservice.checklocalfortoken();
    if(token == null){
      token = this.tokenservice.checksessionfortoken();
    }
    if(token != null){
      console.log('here is the token being sent');
      console.log(token);
      request = request.clone({
        setHeaders: {
          Authorization: 'JWT ' + token
        }
      });
    }
    return next.handle(request);


  }

}

The token scheme works I have tested it.

The settings are properly set. Pretty much everything that needs to be done in this guide is done:

http://getblimp.github.io/django-rest-framework-jwt/

moving on to the actual view:

class CreateSuitsuser(APIView):
    permission_classes = (Issuitssuperuser,)

    def post(self, request, *args, **kwargs):
        serialized = CreateSuitsUserSerializer(data=request.data)
        if serialized.is_valid(raise_exception=True):
            data = serialized.data
        ...

the permission class:

class Issuitssuperuser(BasePermission):

    def has_permission(self, request, view):
        if request.user.issuitssuperuser:
            return True
        return False

the error:

if request.user['issuitssuperuser']: TypeError: 'AnonymousUser' object has no attribute 'getitem' [10/May/2018 20:36:51] "OPTIONS /api/user/suits/ HTTP/1.1" 500 21855 Performing system checks...

but for why? This isn't cool. What am I doing wrong that is not making this cool? I would like it to be cool.

SK. Fazlee Rabby
  • 344
  • 4
  • 14

1 Answers1

0

I ended up writing my own permissions for APIVIEWS and using the django permissions for generics

just easier and im sick of it :

class SuitsPermissions():

    def superuser(self, user):
        if user.issuitssuperuser:
            return True
        return False

    def admin(self, user):
        if user.issuitsadministrator:
            return True
        return False