Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AppRoutingModule } from './app-routing.module';

import { CoreModule } from './core/core.module';
import { LocalizationModule } from './localization/localization.module';
import { tokenInterceptorProviders } from './auth/interceptors/tokenInterceptor.provider';

registerLocaleData(en);

Expand All @@ -26,7 +27,7 @@ registerLocaleData(en);
CoreModule,
LocalizationModule,
],

providers: [tokenInterceptorProviders],
bootstrap: [AppComponent],
})
export class AppModule { }
32 changes: 32 additions & 0 deletions src/app/auth/interceptors/token.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { environment } from 'src/environments/environment';
import { LocalStorageService } from '../../core/services/localStorage.service';
import { StorageKeys } from '../../core/models/project-manager.model';

@Injectable()
export class TokenInterceptor implements HttpInterceptor {
constructor(private localStorageService: LocalStorageService) {}

intercept(request: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
const token = this.localStorageService.getFromLocalStorage(StorageKeys.Token);
if (!token || request.url.includes('sign')) {
const replacedUnAuthRequest = request.clone({
url: `${environment.PATH}:${environment.PORT}/${request.url}`,
});
return next.handle(replacedUnAuthRequest);
}

const replacedAuthRequest = request.clone({
url: `${environment.PATH}:${environment.PORT}${request.url}`,
headers: request.headers.set('Authorization', `Bearer ${token}`),
});
return next.handle(replacedAuthRequest);
}
}
6 changes: 6 additions & 0 deletions src/app/auth/interceptors/tokenInterceptor.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { TokenInterceptor } from './token.interceptor';

export const tokenInterceptorProviders = [
{ provide: HTTP_INTERCEPTORS, useClass: TokenInterceptor, multi: true },
];
64 changes: 64 additions & 0 deletions src/app/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import {
SignInDTO,
SignInResponse,
SignUpDTO,
SignUpResponse,
StorageKeys,
Routes,
} from '../models/project-manager.model';
import { LocalStorageService } from './localStorage.service';

@Injectable({
providedIn: 'root',
})

export class AuthService {
private isAuthorized$$ = new BehaviorSubject(false);

public isAuthorized$ = this.isAuthorized$$.asObservable();

redirectUrl: string | null = null;

constructor(
private localStorageService: LocalStorageService,
private http: HttpClient,
private router: Router,
) { }

signUp(newUser: SignUpDTO) {
const login$: Observable<SignUpResponse> = this.http.post<SignUpResponse>(`${Routes.SignUp}`, newUser);
login$.subscribe({
next: () => {
const user: SignInDTO = { login: newUser.login, password: newUser.password };
this.signIn(user);
},
error: () => this.isAuthorized$$.next(false),
});
return login$;
}

signIn(user: SignInDTO) {
const login$: Observable<SignInResponse> = this.http.post<SignInResponse>(`${Routes.SignIn}`, user);
login$.subscribe({
next: (result) => {
this.isAuthorized$$.next(true);
this.localStorageService.setInLocalStorage(StorageKeys.Token, result.token);
this.router.navigateByUrl('workspace');
},
error: () => this.isAuthorized$$.next(false),
});
return login$;
}

logout() {
this.isAuthorized$$.next(false);
}

isAuthorized() {
return this.isAuthorized$$.value;
}
}
152 changes: 152 additions & 0 deletions src/app/core/models/project-manager.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
export enum Routes {
SignIn = 'auth/signin',
SignUp = 'auth/signup',
AllUsers = 'users',
AllBoards = 'boards',
BoardsSet = 'boardsSet',
AllColumns = 'columns',
ColumnsSet = 'columnsSet',
AllTasks = 'tasks',
TasksSet = 'tasksSet',
File = 'file',
Points = 'points',
}
export enum StorageKeys {
Token = 'token',
}

export interface ErrorResponse {
statusCode: string,
message: string,
}

export interface SignUpDTO {
name: string,
login: string,
password: string,
}

export interface SignUpResponse {
name: string,
login: string,
_id: string,
}

export interface SignInDTO {
login: string,
password: string,
}

export interface SignInResponse {
token: string,
}

export interface BoardDTO {
title: string,
owner: string,
users: string[],
}

export interface BoardResponse {
_id: string,
title: string,
owner: string,
users: string[],
}

export interface ColumnDTO {
title: string;
order: number;
}

export interface ColumnResponse {
_id: string;
title: string;
order: number;
boardId: string;
}

export interface UpdateColumnDTO {
_id: string;
order: number;
}

export interface ColumnFromSetDTO {
title: string;
order: number;
boardId: string;
}

export interface TaskDTO {
_id: string;
title: string;
order: number;
description: string;
userId: string;
users: string[];
}

export interface TaskResponse {
_id: string;
title: string;
order: number;
boardId: string;
columnId: string;
description: string;
userId: string;
users: string[];
}

export interface UpdateTaskDTO {
title: string;
order: number;
description: string;
columnId: string;
userId: string;
users: string[];
}

export interface UpdateTaskPositionDTO {
title: string;
order: number;
columnId: string;
}

export interface FileDTO {
boardId: string;
taskId: string;
file: File;
}

export interface FileResponse {
_id: string;
name: string;
taskId: string;
boardId: string;
path: string;
}

export interface PointDTO {
title: string;
taskId: string;
boardId: string;
done: boolean;
}

export interface PointResponse {
_id: string;
title: string;
taskId: string;
boardId: string;
done: boolean;
}

export interface UpdatePointsDTO {
_id: string;
done: boolean;
}

export interface UpdatePointDTO {
title: string;
done: boolean;
}
Loading