refs #365 added interceptor

pull/2/head
Alvaro Puente Mella 2024-05-22 16:27:18 +02:00
parent c42a61bf3d
commit eb19de4c4f
5 changed files with 47 additions and 9 deletions

View File

@ -10,7 +10,8 @@ import { HeaderComponent } from './components/layout/header/header.component';
import { SidebarComponent } from './components/layout/sidebar/sidebar.component'; import { SidebarComponent } from './components/layout/sidebar/sidebar.component';
import { LoginComponent } from './components/login/login.component'; import { LoginComponent } from './components/login/login.component';
import { FormsModule } from '@angular/forms'; import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http'; import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { CustomInterceptor } from './services/custom.interceptor';
@NgModule({ @NgModule({
declarations: [ declarations: [
@ -29,7 +30,13 @@ import { HttpClientModule } from '@angular/common/http';
FormsModule, FormsModule,
HttpClientModule HttpClientModule
], ],
providers: [], providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: CustomInterceptor,
multi:true
}
],
bootstrap: [AppComponent] bootstrap: [AppComponent]
}) })
export class AppModule { } export class AppModule { }

View File

@ -0,0 +1,15 @@
:host{
width: 100%;
display: grid;
grid-template-areas:
"sidebar header"
"sidebar content"
"sidebar footer";
grid-template-columns: 120px 1fr;
height: 100%;
}
.content-wrapper{
display: block;
background-color: gold;
grid-area: content;
}

View File

@ -1 +1,7 @@
<p>main-layout works!</p> <app-sidebar></app-sidebar>
<app-header></app-header>
<app-footer></app-footer>
<div class="content-wrapper">
<div class="content">
<router-outlet />
</div>

View File

@ -3,11 +3,11 @@
<form> <form>
<div> <div>
<label for="username">Username:</label> <label for="username">Username:</label>
<input [(ngModel)]="loginObj.EmailId" type="text" id="username" name="EmailId" required> <input [(ngModel)]="loginObj.username" type="text" id="username" name="username" required>
</div> </div>
<div> <div>
<label for="password">Password:</label> <label for="password">Password:</label>
<input (ngModel)="loginObj.Password" type="password" id="password" name="Password" required> <input [(ngModel)]="loginObj.password" type="password" id="password" name="password" required>
</div> </div>
<button (click)="onLogin()">Login</button> <button (click)="onLogin()">Login</button>
</form> </form>

View File

@ -11,11 +11,21 @@ import { Router } from '@angular/router';
}) })
export class LoginComponent { export class LoginComponent {
loginObj: any = { loginObj: any = {
"EmailId": "", "username": "",
"Password": "" "password": ""
}; };
constructor(private http: HttpClient) { } constructor(private http: HttpClient, private router: Router) { }
onLogin() { onLogin() {
this.http.post('https://freeapi.gerasim.in/api/User/Login', this.loginObj).subscribe((res: any) => {}) this.http.post('http://127.0.0.1:8080/auth/login', this.loginObj).subscribe((res: any) => {
if(res.token){
localStorage.setItem('loginToken', res.token);
localStorage.setItem('refreshToken', res.refreshToken);
this.router.navigateByUrl('/dashboard');
}
else(
alert("invalid credentials")
)
})
} }
} }