Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JwtInterceptor not work #765

Closed
matiasAS opened this issue Jan 29, 2023 · 2 comments
Closed

JwtInterceptor not work #765

matiasAS opened this issue Jan 29, 2023 · 2 comments
Labels
more info needed This issue is waiting for more information in order to progress

Comments

@matiasAS
Copy link

Description

Not attached token to request:
"his library does not have any functionality for (or opinion about) implementing user authentication and retrieving JWTs to begin with. Those details will vary depending on your setup, but in most cases, you will use a regular HTTP request to authenticate your users and then save their JWTs in local storage or in a cookie if successful."

code example: request to api in django localhost,
return this.http.post(http://localhost:8000/wells/api/v1/get_all_wells_by_user/, {},options)

result: response http 401

app.module.ts:
import {NgModule} from '@angular/core';
import {BrowserModule} from '@angular/platform-browser';
import {RouteReuseStrategy} from '@angular/router';

import {IonicModule, IonicRouteStrategy} from '@ionic/angular';

import {AppComponent} from './app.component';
import {AppRoutingModule} from './app-routing.module';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {HttpClientModule, HTTP_INTERCEPTORS} from "@angular/common/http";
import {JwtModule, JwtInterceptor, JWT_OPTIONS} from "@auth0/angular-jwt";

export function tokenGetter() {
return localStorage.getItem("access_token");
}

@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
IonicModule.forRoot(),
AppRoutingModule,
BrowserAnimationsModule,
JwtModule.forRoot({
config: {
tokenGetter: tokenGetter,
allowedDomains: ["http://localhost:8000/*"],
disallowedRoutes: ["http://example.com/examplebadroute/"],
},
}),
HttpClientModule
],
providers: [
{provide: RouteReuseStrategy, useClass: IonicRouteStrategy},
{provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true},
],
bootstrap: [
AppComponent
],
})
export class AppModule {
}

Environment

Please provide the following:
version of library: "@auth0/angular-jwt": "^5.1.2",

Framework and more data:
FRONTEND
Ionic:
Ionic CLI : 6.20.8
Ionic Framework : @ionic/angular 6.5.1
@angular-devkit/build-angular : 15.1.3
@angular-devkit/schematics : 15.1.3
@angular/cli : 15.1.3
@ionic/angular-toolkit : 6.1.0

Capacitor:
Capacitor CLI : 4.6.2
@capacitor/android : not installed
@capacitor/core : 4.6.2
@capacitor/ios : not installed

Utility:
cordova-res : not installed globally
native-run : 1.7.1

System:
NodeJS : v19.5.0
npm : 9.4.0
OS : Linux 5.15

BACKEND

Django 3.2.16
Configurations - settings

REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': (
#'rest_framework.permissions.IsAuthenticated',
),
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework_simplejwt.authentication.JWTTokenUserAuthentication',
#'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.SessionAuthentication',
'rest_framework.authentication.BasicAuthentication',
),
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',

),

}
SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(days=1),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
}

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework_jwt',
'rest_framework',
'rest_framework.authtoken',
'ohm2_handlers_light',
'corsheaders'
]

@api_view(['POST'])
#@requires_scope('wells:get_all_wells_by_user')
#@authentication_classes((TokenAuthentication,))
@permission_classes((IsAuthenticated,))
def get_all_wells_by_user(request):
print(request)
#print(request.headers)
res, error = dispatcher.get_all_wells_by_user(request, request.data)
if error:
return JsonResponse({"error": error.regroup()})
return JsonResponse(res)

@frederikprijck
Copy link
Member

Hello,

I tried reproducing this but I am unable to.
Would you be able to reproduce it in our sample application that we have inside our repository?

Also, please format your code so it is easier for anyone to understand.

Thanks.

@frederikprijck frederikprijck added the more info needed This issue is waiting for more information in order to progress label Jan 30, 2023
@frederikprijck
Copy link
Member

Closing due to inactivity. Happy to reopen as needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more info needed This issue is waiting for more information in order to progress
Projects
None yet
Development

No branches or pull requests

2 participants