Add frontend.
This commit is contained in:
2
frontend/src/app/app.component.html
Normal file
2
frontend/src/app/app.component.html
Normal file
@@ -0,0 +1,2 @@
|
||||
<app-header></app-header>
|
||||
<router-outlet></router-outlet>
|
||||
8
frontend/src/app/app.component.scss
Normal file
8
frontend/src/app/app.component.scss
Normal file
@@ -0,0 +1,8 @@
|
||||
:host {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
app-header {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
29
frontend/src/app/app.component.spec.ts
Normal file
29
frontend/src/app/app.component.spec.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { AppComponent } from './app.component';
|
||||
|
||||
describe('AppComponent', () => {
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [AppComponent],
|
||||
}).compileComponents();
|
||||
});
|
||||
|
||||
it('should create the app', () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app).toBeTruthy();
|
||||
});
|
||||
|
||||
it(`should have the 'codiki-ng' title`, () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
const app = fixture.componentInstance;
|
||||
expect(app.title).toEqual('codiki-ng');
|
||||
});
|
||||
|
||||
it('should render title', () => {
|
||||
const fixture = TestBed.createComponent(AppComponent);
|
||||
fixture.detectChanges();
|
||||
const compiled = fixture.nativeElement as HTMLElement;
|
||||
expect(compiled.querySelector('h1')?.textContent).toContain('Hello, codiki-ng');
|
||||
});
|
||||
});
|
||||
19
frontend/src/app/app.component.ts
Normal file
19
frontend/src/app/app.component.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component } from '@angular/core';
|
||||
import { RouterOutlet } from '@angular/router';
|
||||
import { HeaderComponent } from './components/header/header.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
standalone: true,
|
||||
imports: [
|
||||
CommonModule,
|
||||
RouterOutlet,
|
||||
HeaderComponent
|
||||
],
|
||||
templateUrl: './app.component.html',
|
||||
styleUrl: './app.component.scss'
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'codiki-ng';
|
||||
}
|
||||
14
frontend/src/app/app.config.ts
Normal file
14
frontend/src/app/app.config.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
import { provideRouter } from '@angular/router';
|
||||
|
||||
import { routes } from './app.routes';
|
||||
import { provideAnimationsAsync } from '@angular/platform-browser/animations/async';
|
||||
import { provideHttpClient } from '@angular/common/http';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [
|
||||
provideRouter(routes),
|
||||
provideAnimationsAsync(),
|
||||
provideHttpClient()
|
||||
]
|
||||
};
|
||||
6
frontend/src/app/app.routes.ts
Normal file
6
frontend/src/app/app.routes.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { Routes } from '@angular/router';
|
||||
|
||||
export const routes: Routes = [
|
||||
{ path: 'login', loadComponent: () => import('./pages/login/login.component').then(module => module.LoginComponent) },
|
||||
{ path: '**', loadComponent: () => import('./pages/home/home.component').then(module => module.HomeComponent) }
|
||||
];
|
||||
16
frontend/src/app/components/header/header.component.html
Normal file
16
frontend/src/app/components/header/header.component.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<div>
|
||||
<button type="button">
|
||||
<mat-icon>menu</mat-icon>
|
||||
</button>
|
||||
<img src="assets/images/codiki.png" alt="logo"/>
|
||||
<span class="title">Codiki</span>
|
||||
</div>
|
||||
<div>
|
||||
<input name="search-query" placeholder="Search something..." />
|
||||
<button type="button">
|
||||
<mat-icon>search</mat-icon>
|
||||
</button>
|
||||
</div>
|
||||
<div>
|
||||
<a [routerLink]="['/login']">Login</a>
|
||||
</div>
|
||||
82
frontend/src/app/components/header/header.component.scss
Normal file
82
frontend/src/app/components/header/header.component.scss
Normal file
@@ -0,0 +1,82 @@
|
||||
$headerHeight: 3.5em;
|
||||
|
||||
:host {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: space-between;
|
||||
background-color: #3f51b5;
|
||||
color: white;
|
||||
position: relative;
|
||||
border: 1px solid black;
|
||||
height: $headerHeight;
|
||||
|
||||
div {
|
||||
border: 1px solid black;
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
justify-content: center;
|
||||
height: $headerHeight;
|
||||
|
||||
&:nth-child(1) {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
align-items: center;
|
||||
gap: 1em;
|
||||
padding: 0 1em;
|
||||
|
||||
img {
|
||||
$imageSize: 2em;
|
||||
width: $imageSize;
|
||||
height: $imageSize;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 1.5em;
|
||||
}
|
||||
}
|
||||
|
||||
&:nth-child(2) {
|
||||
flex: 1;
|
||||
$borderRadiusValue: 10em;
|
||||
|
||||
input {
|
||||
flex: 1;
|
||||
width: 60%;
|
||||
max-width: 50em;
|
||||
border-radius: $borderRadiusValue 0 0 $borderRadiusValue;
|
||||
background-color: white;
|
||||
border: solid 1px #ccc;
|
||||
margin: .5em 0;
|
||||
padding: .2em .5em;
|
||||
}
|
||||
|
||||
button {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
border-radius: 0 $borderRadiusValue $borderRadiusValue 0;
|
||||
background-color: white;
|
||||
border: solid 1px #ccc;
|
||||
margin: .5em 0;
|
||||
|
||||
&:hover {
|
||||
background-color: #eee;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&:nth-child(3) {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
right: 0;
|
||||
|
||||
a {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
align-items: center;
|
||||
min-width: 5em;
|
||||
color: white;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
13
frontend/src/app/components/header/header.component.ts
Normal file
13
frontend/src/app/components/header/header.component.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { RouterModule } from '@angular/router';
|
||||
|
||||
@Component({
|
||||
selector: 'app-header',
|
||||
standalone: true,
|
||||
imports: [MatButtonModule, MatIconModule, RouterModule],
|
||||
templateUrl: './header.component.html',
|
||||
styleUrl: './header.component.scss',
|
||||
})
|
||||
export class HeaderComponent {}
|
||||
4
frontend/src/app/core/model/FormError.ts
Normal file
4
frontend/src/app/core/model/FormError.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface FormError {
|
||||
fieldName: string;
|
||||
errorMessage: string;
|
||||
}
|
||||
7
frontend/src/app/core/model/User.ts
Normal file
7
frontend/src/app/core/model/User.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface User {
|
||||
id: string;
|
||||
email: string;
|
||||
pseudo: string;
|
||||
photoId?: string;
|
||||
roles: string[];
|
||||
}
|
||||
10
frontend/src/app/core/rest-services/model/login.model.ts
Normal file
10
frontend/src/app/core/rest-services/model/login.model.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export interface LoginRequest {
|
||||
email?: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
export interface LoginResponse {
|
||||
tokenType: string,
|
||||
accessToken: string,
|
||||
refreshToken: string
|
||||
}
|
||||
15
frontend/src/app/core/rest-services/user.rest-service.ts
Normal file
15
frontend/src/app/core/rest-services/user.rest-service.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { HttpClient } from "@angular/common/http";
|
||||
import { Injectable, inject } from "@angular/core";
|
||||
import { LoginRequest, LoginResponse } from "./model/login.model";
|
||||
import { lastValueFrom } from "rxjs";
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserRestService {
|
||||
private httpClient = inject(HttpClient);
|
||||
|
||||
login(request: LoginRequest): Promise<LoginResponse> {
|
||||
return lastValueFrom(this.httpClient.post<LoginResponse>('/api/users/login', request));
|
||||
}
|
||||
}
|
||||
73
frontend/src/app/core/service/authentication.service.ts
Normal file
73
frontend/src/app/core/service/authentication.service.ts
Normal file
@@ -0,0 +1,73 @@
|
||||
import { Injectable } from "@angular/core";
|
||||
import { User } from "../model/User";
|
||||
|
||||
const JWT_PARAM = 'jwt';
|
||||
|
||||
interface UserDetails {
|
||||
sub: string;
|
||||
exp: number;
|
||||
email: string;
|
||||
pseudo: string;
|
||||
roles: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthenticationService {
|
||||
authenticate(token: string): void {
|
||||
localStorage.setItem(JWT_PARAM, token);
|
||||
}
|
||||
|
||||
unauthenticate(): void {
|
||||
localStorage.removeItem(JWT_PARAM);
|
||||
}
|
||||
|
||||
isAuthenticated(): boolean {
|
||||
let result = false;
|
||||
|
||||
const userDetails = this.extractUserDetails();
|
||||
|
||||
if (userDetails) {
|
||||
const authenticationExpiration = new Date(userDetails.exp * 1000);
|
||||
result = authenticationExpiration > new Date();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private extractUserFromLocalStorage(): User | undefined {
|
||||
let result: User | undefined = undefined;
|
||||
|
||||
const userDetails = this.extractUserDetails();
|
||||
if (userDetails) {
|
||||
const user = this.convertToUser(userDetails);
|
||||
result = user;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private extractUserDetails(): UserDetails | undefined {
|
||||
let result: UserDetails | undefined = undefined;
|
||||
|
||||
const token = localStorage.getItem(JWT_PARAM);
|
||||
|
||||
const tokenParts = token?.split('.');
|
||||
if (tokenParts?.length === 3 && tokenParts[1].length) {
|
||||
const userDetails: UserDetails = JSON.parse(tokenParts[1]);
|
||||
result = userDetails;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private convertToUser(userDetails: UserDetails): User {
|
||||
return {
|
||||
id: userDetails.sub,
|
||||
email: userDetails.email,
|
||||
pseudo: userDetails.pseudo,
|
||||
roles: userDetails.roles.split(',')
|
||||
};
|
||||
}
|
||||
}
|
||||
5
frontend/src/app/core/utils/ObjectUtils.ts
Normal file
5
frontend/src/app/core/utils/ObjectUtils.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export function copy<T>(object: T): T {
|
||||
return JSON.parse(
|
||||
JSON.stringify(object)
|
||||
);
|
||||
}
|
||||
1
frontend/src/app/pages/home/home.component.html
Normal file
1
frontend/src/app/pages/home/home.component.html
Normal file
@@ -0,0 +1 @@
|
||||
<p>home works!</p>
|
||||
0
frontend/src/app/pages/home/home.component.scss
Normal file
0
frontend/src/app/pages/home/home.component.scss
Normal file
23
frontend/src/app/pages/home/home.component.spec.ts
Normal file
23
frontend/src/app/pages/home/home.component.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { HomeComponent } from './home.component';
|
||||
|
||||
describe('HomeComponent', () => {
|
||||
let component: HomeComponent;
|
||||
let fixture: ComponentFixture<HomeComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [HomeComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(HomeComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
12
frontend/src/app/pages/home/home.component.ts
Normal file
12
frontend/src/app/pages/home/home.component.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Component } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-home',
|
||||
standalone: true,
|
||||
imports: [],
|
||||
templateUrl: './home.component.html',
|
||||
styleUrl: './home.component.scss'
|
||||
})
|
||||
export class HomeComponent {
|
||||
|
||||
}
|
||||
16
frontend/src/app/pages/login/login.component.html
Normal file
16
frontend/src/app/pages/login/login.component.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<form [formGroup]="loginForm" (submit)="performLogin()" ngNativeValidate>
|
||||
<h1>Login</h1>
|
||||
<div>
|
||||
<label for="email">
|
||||
Email address
|
||||
</label>
|
||||
<input type="email" formControlName="email" required />
|
||||
</div>
|
||||
<div>
|
||||
<label for="password">
|
||||
Password
|
||||
</label>
|
||||
<input type="password" formControlName="password" required />
|
||||
</div>
|
||||
<button type="submit">Send</button>
|
||||
</form>
|
||||
0
frontend/src/app/pages/login/login.component.scss
Normal file
0
frontend/src/app/pages/login/login.component.scss
Normal file
23
frontend/src/app/pages/login/login.component.spec.ts
Normal file
23
frontend/src/app/pages/login/login.component.spec.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
|
||||
import { LoginComponent } from './login.component';
|
||||
|
||||
describe('LoginComponent', () => {
|
||||
let component: LoginComponent;
|
||||
let fixture: ComponentFixture<LoginComponent>;
|
||||
|
||||
beforeEach(async () => {
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [LoginComponent]
|
||||
})
|
||||
.compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(LoginComponent);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
});
|
||||
63
frontend/src/app/pages/login/login.component.ts
Normal file
63
frontend/src/app/pages/login/login.component.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Component, OnDestroy, OnInit, inject } from '@angular/core';
|
||||
import { FormBuilder, FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
||||
import { MatSnackBarModule } from '@angular/material/snack-bar';
|
||||
import { Subscription, debounceTime, map } from 'rxjs';
|
||||
import { LoginService } from './login.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
standalone: true,
|
||||
templateUrl: './login.component.html',
|
||||
styleUrl: './login.component.scss',
|
||||
imports: [ ReactiveFormsModule ],
|
||||
providers: [LoginService, MatSnackBarModule]
|
||||
})
|
||||
export class LoginComponent implements OnInit, OnDestroy {
|
||||
private loginService = inject(LoginService);
|
||||
private formBuilder = inject(FormBuilder);
|
||||
private subscriptions: Subscription[] = [];
|
||||
emailValue: string | undefined;
|
||||
loginForm: FormGroup = this.formBuilder.group({
|
||||
email: new FormControl<string | undefined>('', [Validators.required, Validators.email]),
|
||||
password: new FormControl<string | undefined>('', [Validators.required])
|
||||
});
|
||||
|
||||
ngOnInit(): void {
|
||||
const emailSubscription = this.loginForm.controls['email'].valueChanges
|
||||
.pipe(
|
||||
debounceTime(300),
|
||||
map(value => value?.length ? value as string : '')
|
||||
)
|
||||
.subscribe(email => {
|
||||
this.loginService.editEmail(email);
|
||||
});
|
||||
this.subscriptions.push(emailSubscription);
|
||||
|
||||
const passwordSubscription = this.loginForm.controls['password'].valueChanges
|
||||
.pipe(
|
||||
debounceTime(300),
|
||||
map(value => value?.length ? value as string : '')
|
||||
)
|
||||
.subscribe(password => {
|
||||
this.loginService.editPassword(password);
|
||||
});
|
||||
this.subscriptions.push(passwordSubscription)
|
||||
|
||||
const stateSubscription = this.loginService.state$
|
||||
.subscribe(state => {
|
||||
this.loginForm.controls['email'].setValue(state.request.email, { emitEvent: false });
|
||||
this.loginForm.controls['password'].setValue(state.request.password, { emitEvent: false });
|
||||
});
|
||||
this.subscriptions.push(stateSubscription);
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.subscriptions.forEach(subscription => subscription.unsubscribe());
|
||||
}
|
||||
|
||||
performLogin(): void {
|
||||
if (this.loginForm.valid) {
|
||||
this.loginService.performLogin();
|
||||
}
|
||||
}
|
||||
}
|
||||
76
frontend/src/app/pages/login/login.service.ts
Normal file
76
frontend/src/app/pages/login/login.service.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
import { Injectable, inject } from "@angular/core";
|
||||
import { BehaviorSubject, Observable } from "rxjs";
|
||||
import { copy } from "../../core/utils/ObjectUtils";
|
||||
import { FormError } from "../../core/model/FormError";
|
||||
import { UserRestService } from "../../core/rest-services/user.rest-service";
|
||||
import { LoginRequest } from "../../core/rest-services/model/login.model";
|
||||
import { AuthenticationService } from "../../core/service/authentication.service";
|
||||
import { MatSnackBar } from "@angular/material/snack-bar";
|
||||
import { Router } from "@angular/router";
|
||||
|
||||
export interface LoginState {
|
||||
request: LoginRequest;
|
||||
errors: FormError[]
|
||||
}
|
||||
|
||||
const DEFAULT_STATE: LoginState = {
|
||||
request: {
|
||||
email: undefined,
|
||||
password: undefined
|
||||
},
|
||||
errors: []
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class LoginService {
|
||||
private stateSubject = new BehaviorSubject<LoginState>(copy(DEFAULT_STATE));
|
||||
private userRestService = inject(UserRestService);
|
||||
private authenticationService = inject(AuthenticationService);
|
||||
private snackBar = inject(MatSnackBar);
|
||||
private router = inject(Router);
|
||||
|
||||
get state$(): Observable<LoginState> {
|
||||
return this.stateSubject.asObservable();
|
||||
}
|
||||
|
||||
private get state(): LoginState {
|
||||
return this.stateSubject.value;
|
||||
}
|
||||
|
||||
private save(newState: LoginState): void {
|
||||
this.stateSubject.next(newState);
|
||||
}
|
||||
|
||||
editEmail(newEmail: string): void {
|
||||
const state = this.state;
|
||||
|
||||
state.request.email = newEmail;
|
||||
|
||||
this.save(state);
|
||||
}
|
||||
|
||||
editPassword(newPassword: string): void {
|
||||
const state = this.state;
|
||||
|
||||
state.request.password = newPassword;
|
||||
|
||||
this.save(state);
|
||||
}
|
||||
|
||||
performLogin(): void {
|
||||
const state = this.state;
|
||||
|
||||
// Check state is valid
|
||||
|
||||
this.userRestService.login(state.request)
|
||||
.then(response => {
|
||||
this.authenticationService.authenticate(response.accessToken);
|
||||
this.snackBar.open('Authentication succeeded!', 'Close', { duration: 5000 });
|
||||
this.router.navigate(['/home']);
|
||||
})
|
||||
.catch(error => {
|
||||
console.error(error)
|
||||
this.snackBar.open('Authentication failed.', 'Close', { duration: 5000 });
|
||||
});
|
||||
}
|
||||
}
|
||||
0
frontend/src/assets/.gitkeep
Normal file
0
frontend/src/assets/.gitkeep
Normal file
BIN
frontend/src/assets/images/codiki.png
Normal file
BIN
frontend/src/assets/images/codiki.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
BIN
frontend/src/assets/images/favicon.ico
Normal file
BIN
frontend/src/assets/images/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 17 KiB |
BIN
frontend/src/favicon.ico
Normal file
BIN
frontend/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
15
frontend/src/index.html
Normal file
15
frontend/src/index.html
Normal file
@@ -0,0 +1,15 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>CodikiNg</title>
|
||||
<base href="/">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<link rel="icon" type="image/x-icon" href="assets/images/favicon.ico">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap" rel="stylesheet">
|
||||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
|
||||
</head>
|
||||
<body class="mat-typography">
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
6
frontend/src/main.ts
Normal file
6
frontend/src/main.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { appConfig } from './app/app.config';
|
||||
import { AppComponent } from './app/app.component';
|
||||
|
||||
bootstrapApplication(AppComponent, appConfig)
|
||||
.catch((err) => console.error(err));
|
||||
4
frontend/src/styles.scss
Normal file
4
frontend/src/styles.scss
Normal file
@@ -0,0 +1,4 @@
|
||||
/* You can add global styles to this file, and also import other style files */
|
||||
|
||||
html, body { height: 100%; }
|
||||
body { margin: 0; font-family: Roboto, "Helvetica Neue", sans-serif; }
|
||||
Reference in New Issue
Block a user