Compare commits
4 commits
f5ebe90b60
...
7ce68e39c2
Author | SHA1 | Date | |
---|---|---|---|
7ce68e39c2 |
|||
4c9a23c547 |
|||
a009acb4e3 |
|||
496cafd32a |
8 changed files with 53 additions and 9 deletions
BIN
bun.lockb
Executable file
BIN
bun.lockb
Executable file
Binary file not shown.
|
@ -8,7 +8,7 @@ import { httpInterceptor } from './service/http.interceptor';
|
||||||
export const appConfig: ApplicationConfig = {
|
export const appConfig: ApplicationConfig = {
|
||||||
providers: [
|
providers: [
|
||||||
provideZoneChangeDetection({ eventCoalescing: true }),
|
provideZoneChangeDetection({ eventCoalescing: true }),
|
||||||
provideRouter(routes, withDebugTracing()),
|
provideRouter(routes),
|
||||||
provideHttpClient(withInterceptors([httpInterceptor])),
|
provideHttpClient(withInterceptors([httpInterceptor])),
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Routes } from '@angular/router';
|
import { Routes } from '@angular/router';
|
||||||
import { authGuard } from './service/auth.guard';
|
import { authGuard } from './service/auth.guard';
|
||||||
|
import NotFoundComponent from './component/not-found/not-found.component';
|
||||||
|
|
||||||
export const routes: Routes = [
|
export const routes: Routes = [
|
||||||
{
|
{
|
||||||
|
@ -32,4 +33,12 @@ export const routes: Routes = [
|
||||||
],
|
],
|
||||||
canActivate: [authGuard],
|
canActivate: [authGuard],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
path: '404',
|
||||||
|
component: NotFoundComponent,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
path: '**',
|
||||||
|
redirectTo: '404',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Component, inject } from '@angular/core';
|
import { Component, inject, OnInit } from '@angular/core';
|
||||||
import {
|
import {
|
||||||
FormBuilder,
|
FormBuilder,
|
||||||
FormGroup,
|
FormGroup,
|
||||||
|
@ -7,23 +7,30 @@ import {
|
||||||
} from '@angular/forms';
|
} from '@angular/forms';
|
||||||
import AuthService from '../../../service/auth.service';
|
import AuthService from '../../../service/auth.service';
|
||||||
import { AuthResponse, Token } from '../../../models';
|
import { AuthResponse, Token } from '../../../models';
|
||||||
import { Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-login',
|
selector: 'app-login',
|
||||||
imports: [ReactiveFormsModule],
|
imports: [ReactiveFormsModule],
|
||||||
templateUrl: './login.component.html',
|
templateUrl: './login.component.html',
|
||||||
})
|
})
|
||||||
export default class LoginComponent {
|
export default class LoginComponent implements OnInit {
|
||||||
fb: FormBuilder = inject(FormBuilder);
|
fb: FormBuilder = inject(FormBuilder);
|
||||||
authService: AuthService = inject(AuthService);
|
authService: AuthService = inject(AuthService);
|
||||||
router: Router = inject(Router);
|
router: Router = inject(Router);
|
||||||
|
route: ActivatedRoute = inject(ActivatedRoute);
|
||||||
|
|
||||||
form: FormGroup = this.fb.group({
|
form: FormGroup = this.fb.group({
|
||||||
username: [null, [Validators.required]],
|
username: [null, [Validators.required]],
|
||||||
password: [null, [Validators.required]],
|
password: [null, [Validators.required]],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
returnUrl: string | undefined;
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/dashboard';
|
||||||
|
}
|
||||||
|
|
||||||
login() {
|
login() {
|
||||||
if (this.form.invalid) {
|
if (this.form.invalid) {
|
||||||
console.log(this.form.errors);
|
console.log(this.form.errors);
|
||||||
|
@ -34,7 +41,7 @@ export default class LoginComponent {
|
||||||
localStorage.setItem('access_token', r.accessToken);
|
localStorage.setItem('access_token', r.accessToken);
|
||||||
localStorage.setItem('refresh_token', r.refreshToken);
|
localStorage.setItem('refresh_token', r.refreshToken);
|
||||||
|
|
||||||
this.router.navigate(['dashboard']);
|
this.router.navigate([this.returnUrl]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
8
src/app/component/not-found/not-found.component.ts
Normal file
8
src/app/component/not-found/not-found.component.ts
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
import { Component } from '@angular/core';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-not-found',
|
||||||
|
template: '<h1>404 Not Found</h1>',
|
||||||
|
standalone: true,
|
||||||
|
})
|
||||||
|
export default class NotFoundComponent {}
|
|
@ -1,8 +1,19 @@
|
||||||
import { Component } from '@angular/core';
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
import { Component, inject, OnInit } from '@angular/core';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-users',
|
selector: 'app-users',
|
||||||
imports: [],
|
imports: [],
|
||||||
templateUrl: './users.component.html',
|
templateUrl: './users.component.html',
|
||||||
})
|
})
|
||||||
export default class UsersComponent {}
|
export default class UsersComponent implements OnInit {
|
||||||
|
private client = inject(HttpClient);
|
||||||
|
ngOnInit() {
|
||||||
|
console.log('AHHHH');
|
||||||
|
console.log('AHHHHH');
|
||||||
|
|
||||||
|
this.client.get('https://dummyjson.com/users', {}).subscribe((response) => {
|
||||||
|
console.log(response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { CanActivateFn } from '@angular/router';
|
import { CanActivateFn, Router } from '@angular/router';
|
||||||
import { inject } from '@angular/core';
|
import { inject } from '@angular/core';
|
||||||
import {
|
import {
|
||||||
HttpClient,
|
HttpClient,
|
||||||
|
@ -8,7 +8,9 @@ import {
|
||||||
import { catchError, map, of } from 'rxjs';
|
import { catchError, map, of } from 'rxjs';
|
||||||
import { User } from '../models';
|
import { User } from '../models';
|
||||||
|
|
||||||
export const authGuard: CanActivateFn = () => {
|
export const authGuard: CanActivateFn = (route, state) => {
|
||||||
|
const router = inject(Router);
|
||||||
|
|
||||||
return inject(HttpClient)
|
return inject(HttpClient)
|
||||||
.get<User>('https://dummyjson.com/auth/me', { observe: 'response' })
|
.get<User>('https://dummyjson.com/auth/me', { observe: 'response' })
|
||||||
.pipe(
|
.pipe(
|
||||||
|
@ -18,6 +20,9 @@ export const authGuard: CanActivateFn = () => {
|
||||||
}),
|
}),
|
||||||
catchError((err: HttpErrorResponse) => {
|
catchError((err: HttpErrorResponse) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
router.navigate(['/auth/login'], {
|
||||||
|
queryParams: { returnUrl: state.url },
|
||||||
|
});
|
||||||
return of(false);
|
return of(false);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
|
@ -8,6 +8,10 @@ export const httpInterceptor: HttpInterceptorFn = (
|
||||||
req: HttpRequest<unknown>,
|
req: HttpRequest<unknown>,
|
||||||
next: HttpHandlerFn,
|
next: HttpHandlerFn,
|
||||||
) => {
|
) => {
|
||||||
|
if (req.url.includes('/auth/login') || req.url.includes('/auth/register')) {
|
||||||
|
return next(req);
|
||||||
|
}
|
||||||
|
|
||||||
return next(
|
return next(
|
||||||
req.clone({
|
req.clone({
|
||||||
setHeaders: {
|
setHeaders: {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue