added auth.service & config HttpClient
This commit is contained in:
parent
94b096e6c0
commit
53c2caa45e
5 changed files with 31 additions and 4 deletions
|
|
@ -4,4 +4,4 @@ from .models import CustomUser
|
||||||
class UserSerializer(serializers.ModelSerializer):
|
class UserSerializer(serializers.ModelSerializer):
|
||||||
class Meta:
|
class Meta:
|
||||||
model = CustomUser
|
model = CustomUser
|
||||||
fields = ["id", "username", "first_name", "last_name", "email"]
|
fields = ["id", "username", "email"]
|
||||||
|
|
@ -27,6 +27,9 @@ DEBUG = True
|
||||||
|
|
||||||
ALLOWED_HOSTS = []
|
ALLOWED_HOSTS = []
|
||||||
|
|
||||||
|
CORS_ALLOWED_ORIGINS = [
|
||||||
|
'http://localhost:4200'
|
||||||
|
]
|
||||||
|
|
||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
|
|
@ -38,6 +41,7 @@ INSTALLED_APPS = [
|
||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
|
'corsheaders',
|
||||||
'users',
|
'users',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
@ -52,6 +56,8 @@ MIDDLEWARE = [
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
|
'corsheaders.middleware.CorsMiddleware',
|
||||||
|
'django.middleware.common.CommonMiddleware'
|
||||||
]
|
]
|
||||||
|
|
||||||
ROOT_URLCONF = 'videoflix.urls'
|
ROOT_URLCONF = 'videoflix.urls'
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
import { ApplicationConfig } from '@angular/core';
|
import { ApplicationConfig } from '@angular/core';
|
||||||
import { provideRouter } from '@angular/router';
|
import { provideRouter } from '@angular/router';
|
||||||
|
|
||||||
import { routes } from './app.routes';
|
import { routes } from './app.routes';
|
||||||
|
import { provideHttpClient } from '@angular/common/http';
|
||||||
|
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [provideRouter(routes)]
|
providers: [provideRouter(routes), provideHttpClient()],
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,6 @@ export class AuthComponent {
|
||||||
authData = {
|
authData = {
|
||||||
mail: '',
|
mail: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
constructor(private router: Router) {}
|
constructor(private router: Router) {}
|
||||||
|
|
||||||
isUserEmailValid(emailValue: string) {
|
isUserEmailValid(emailValue: string) {
|
||||||
|
|
|
||||||
22
frontend/src/app/services/auth.service.ts
Normal file
22
frontend/src/app/services/auth.service.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class AuthService {
|
||||||
|
constructor(private http: HttpClient) {}
|
||||||
|
|
||||||
|
getAllUsers(): Observable<any> {
|
||||||
|
const headers = this.getAuthHeaders();
|
||||||
|
return this.http.get('http://127.0.0.1:8000/users/', { headers });
|
||||||
|
}
|
||||||
|
|
||||||
|
private getAuthHeaders(): HttpHeaders {
|
||||||
|
const authToken = localStorage.getItem('authToken');
|
||||||
|
return new HttpHeaders({
|
||||||
|
Authorization: `token ${authToken}`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue