Spaces:
Running
Running
Update flare-ui/src/app/components/login/login.component.ts
Browse files
flare-ui/src/app/components/login/login.component.ts
CHANGED
@@ -1,115 +1,128 @@
|
|
1 |
-
import { Component, inject } from '@angular/core';
|
2 |
-
import { CommonModule } from '@angular/common';
|
3 |
-
import { FormsModule } from '@angular/forms';
|
4 |
-
import { Router } from '@angular/router';
|
5 |
-
import {
|
6 |
-
|
7 |
-
@
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
[
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
}
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
}
|
|
|
1 |
+
import { Component, inject } from '@angular/core';
|
2 |
+
import { CommonModule } from '@angular/common';
|
3 |
+
import { FormsModule } from '@angular/forms';
|
4 |
+
import { Router } from '@angular/router';
|
5 |
+
import { MatCardModule } from '@angular/material/card';
|
6 |
+
import { MatFormFieldModule } from '@angular/material/form-field';
|
7 |
+
import { MatInputModule } from '@angular/material/input';
|
8 |
+
import { MatButtonModule } from '@angular/material/button';
|
9 |
+
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
|
10 |
+
import { AuthService } from '../../services/auth.service';
|
11 |
+
|
12 |
+
@Component({
|
13 |
+
selector: 'app-login',
|
14 |
+
standalone: true,
|
15 |
+
imports: [
|
16 |
+
CommonModule,
|
17 |
+
FormsModule,
|
18 |
+
MatCardModule,
|
19 |
+
MatFormFieldModule,
|
20 |
+
MatInputModule,
|
21 |
+
MatButtonModule,
|
22 |
+
MatProgressSpinnerModule
|
23 |
+
],
|
24 |
+
template: `
|
25 |
+
<div class="login-container">
|
26 |
+
<div class="login-card">
|
27 |
+
<h1>Flare Administration</h1>
|
28 |
+
<form (ngSubmit)="login()" #loginForm="ngForm">
|
29 |
+
<div class="form-group">
|
30 |
+
<label for="username">Username</label>
|
31 |
+
<input
|
32 |
+
type="text"
|
33 |
+
id="username"
|
34 |
+
name="username"
|
35 |
+
[(ngModel)]="username"
|
36 |
+
required
|
37 |
+
[disabled]="loading"
|
38 |
+
>
|
39 |
+
</div>
|
40 |
+
<div class="form-group">
|
41 |
+
<label for="password">Password</label>
|
42 |
+
<input
|
43 |
+
type="password"
|
44 |
+
id="password"
|
45 |
+
name="password"
|
46 |
+
[(ngModel)]="password"
|
47 |
+
required
|
48 |
+
[disabled]="loading"
|
49 |
+
>
|
50 |
+
</div>
|
51 |
+
@if (error) {
|
52 |
+
<div class="alert alert-danger">{{ error }}</div>
|
53 |
+
}
|
54 |
+
<button
|
55 |
+
type="submit"
|
56 |
+
class="btn btn-primary w-100"
|
57 |
+
[disabled]="loading || !loginForm.valid"
|
58 |
+
>
|
59 |
+
@if (loading) {
|
60 |
+
<span class="spinner"></span> Logging in...
|
61 |
+
} @else {
|
62 |
+
Login
|
63 |
+
}
|
64 |
+
</button>
|
65 |
+
</form>
|
66 |
+
</div>
|
67 |
+
</div>
|
68 |
+
`,
|
69 |
+
styles: [`
|
70 |
+
.login-container {
|
71 |
+
min-height: 100vh;
|
72 |
+
display: flex;
|
73 |
+
align-items: center;
|
74 |
+
justify-content: center;
|
75 |
+
background-color: #f5f5f5;
|
76 |
+
}
|
77 |
+
|
78 |
+
.login-card {
|
79 |
+
background: white;
|
80 |
+
padding: 2rem;
|
81 |
+
border-radius: 8px;
|
82 |
+
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
83 |
+
width: 100%;
|
84 |
+
max-width: 400px;
|
85 |
+
|
86 |
+
h1 {
|
87 |
+
text-align: center;
|
88 |
+
margin-bottom: 2rem;
|
89 |
+
color: #333;
|
90 |
+
font-size: 1.5rem;
|
91 |
+
}
|
92 |
+
}
|
93 |
+
|
94 |
+
.w-100 {
|
95 |
+
width: 100%;
|
96 |
+
}
|
97 |
+
|
98 |
+
button {
|
99 |
+
display: flex;
|
100 |
+
align-items: center;
|
101 |
+
justify-content: center;
|
102 |
+
gap: 0.5rem;
|
103 |
+
}
|
104 |
+
`]
|
105 |
+
})
|
106 |
+
export class LoginComponent {
|
107 |
+
private authService = inject(AuthService);
|
108 |
+
private router = inject(Router);
|
109 |
+
|
110 |
+
username = '';
|
111 |
+
password = '';
|
112 |
+
loading = false;
|
113 |
+
error = '';
|
114 |
+
|
115 |
+
async login() {
|
116 |
+
this.loading = true;
|
117 |
+
this.error = '';
|
118 |
+
|
119 |
+
try {
|
120 |
+
await this.authService.login(this.username, this.password).toPromise();
|
121 |
+
this.router.navigate(['/']);
|
122 |
+
} catch (err: any) {
|
123 |
+
this.error = err.error?.detail || 'Invalid credentials';
|
124 |
+
} finally {
|
125 |
+
this.loading = false;
|
126 |
+
}
|
127 |
+
}
|
128 |
}
|