Commit bc83c79b authored by tinhbe's avatar tinhbe

complate login

parent 1bfb63bb
...@@ -16,6 +16,7 @@ import { CommonModule } from '@angular/common'; ...@@ -16,6 +16,7 @@ import { CommonModule } from '@angular/common';
], ],
template: ` <nz-content> template: ` <nz-content>
<div> <div>
admin
<router-outlet></router-outlet> <router-outlet></router-outlet>
</div> </div>
</nz-content>`, </nz-content>`,
......
<div> <nz-layout>
<p class="tw-text-white tw-bg-black tw-p-5">Welcome to the home page!</p> <nz-sider nzCollapsible nzWidth="200px">
</div> <div class="logo"></div>
\ No newline at end of file <ul nz-menu nzTheme="dark" nzMode="inline">
<li nz-menu-item>
<span nz-icon nzType="pie-chart"></span>
<span>Option 1</span>
</li>
<li nz-menu-item>
<span nz-icon nzType="desktop"></span>
<span>Option 2</span>
</li>
<li nz-submenu nzTitle="User" nzIcon="user">
<ul>
<li nz-menu-item>Tom</li>
<li nz-menu-item>Bill</li>
<li nz-menu-item>Alex</li>
</ul>
</li>
<li nz-submenu nzTitle="Team" nzIcon="team">
<ul>
<li nz-menu-item>Team 1</li>
<li nz-menu-item>Team 2</li>
</ul>
</li>
<li nz-menu-item>
<span nz-icon nzType="file"></span>
<span>File</span>
</li>
</ul>
</nz-sider>
<nz-layout>
<nz-header></nz-header>
<nz-content>
<nz-breadcrumb>
<nz-breadcrumb-item>User</nz-breadcrumb-item>
<nz-breadcrumb-item>Bill</nz-breadcrumb-item>
</nz-breadcrumb>
<div class="inner-content">Bill is a cat.</div>
</nz-content>
<nz-footer>Ant Design ©2020 Implement By Angular</nz-footer>
</nz-layout>
</nz-layout>
.logo {
height: 32px;
margin: 16px;
background: rgba(255, 255, 255, 0.3);
}
nz-header {
background: #fff;
padding: 0;
}
nz-content {
margin: 0 16px;
}
nz-breadcrumb {
margin: 16px 0;
}
.inner-content {
padding: 24px;
background: #fff;
min-height: 360px;
}
nz-footer {
text-align: center;
}
import { CommonModule } from '@angular/common';
import { ChangeDetectionStrategy, Component } from '@angular/core'; import { ChangeDetectionStrategy, Component } from '@angular/core';
import { NzCarouselModule } from 'ng-zorro-antd/carousel'; import { NzCarouselModule } from 'ng-zorro-antd/carousel';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
import { NzMenuModule } from 'ng-zorro-antd/menu';
import { NzIconModule } from 'ng-zorro-antd/icon';
@Component({ @Component({
selector: 'meu-home', selector: 'meu-home',
standalone: true, standalone: true,
imports: [NzCarouselModule], imports: [
NzCarouselModule,
CommonModule,
NzLayoutModule,
NzBreadCrumbModule,
NzMenuModule,
NzIconModule
],
templateUrl: './home.component.html', templateUrl: './home.component.html',
styleUrl: './home.component.scss', styleUrls: ['./home.component.scss'],
//styleUrl: './home.component.scss',
changeDetection: ChangeDetectionStrategy.OnPush, changeDetection: ChangeDetectionStrategy.OnPush,
}) })
export class HomeComponent {} export class HomeComponent {}
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { environment } from '../../../../environments/environment.development';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class AuthService { export class AuthService {
private apiUrl = 'http://localhost:3000/login'; private apiUrl = environment.API_DOMAIN + '/login';
constructor(private http: HttpClient) {} constructor(private http: HttpClient) {}
......
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router'; import { CanActivate, Router } from '@angular/router';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class AdminGuard implements CanActivate { export class AdminGuard implements CanActivate {
constructor() {} constructor(private router: Router) {}
canActivate(): boolean { canActivate(): boolean {
return true; const role = localStorage.getItem('role');
if (role === 'admin') {
return true;
} else {
this.router.navigate(['/login']); // chuyển đến trang lỗi không có quyền. cho bắt đăng nhập với quyên admin
return false;
}
} }
} }
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CanActivate } from '@angular/router'; import { CanActivate, Router } from '@angular/router';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class AuthGuard implements CanActivate { export class AuthGuard implements CanActivate {
constructor() {} constructor(private router: Router) {}
canActivate(): boolean { canActivate(): boolean {
return true; const token = localStorage.getItem('token');
if (token) {
this.router.navigate(['/home']);
return false;
} else {
return true;
}
} }
} }
<div class="tw-h-screen tw-bg-gradient-to-br tw-from-indigo-600 tw-to-purple-600 tw-flex tw-items-center tw-justify-center tw-p-6"> <div class="tw-h-screen tw-bg-gradient-to-br tw-from-indigo-600 tw-to-purple-600 tw-flex tw-items-center tw-justify-center tw-p-6">
<form nz-form [formGroup]="loginForm" class="tw-w-full tw-max-w-md tw-bg-white tw-rounded-lg tw-shadow-xl tw-p-8 tw-space-y-6 tw-backdrop-blur-sm" (ngSubmit)="onSubmit()"> <form nz-form [formGroup]="loginForm" class="tw-w-full tw-max-w-md tw-bg-white tw-rounded-lg tw-shadow-xl tw-p-8 tw-space-y-6 tw-backdrop-blur-sm" (ngSubmit)="onSubmit()">
<!-- Form Header --> <h2 class="tw-text-3xl tw-font-bold tw-text-center tw-text-gray-800">Welcome Back!</h2>
<h2 class="tw-text-2xl tw-font-bold tw-text-center tw-text-gray-800">Welcome Back!</h2> <p class="tw-text-center tw-text-1xl tw-text-gray-600">Please log in to your account</p>
<p class="tw-text-center tw-text-gray-600">Please log in to your account</p>
<!-- Username Field -->
<nz-form-item class="tw-mb-4"> <nz-form-item class="tw-mb-4">
<nz-form-label [nzFor]="'userName'" class="tw-text-gray-700 tw-font-medium">Username</nz-form-label> <nz-form-label [nzFor]="'userName'" class="tw-text-gray-700 tw-font-bold">Username :</nz-form-label>
<nz-form-control nzErrorTip="Please input your username!"> <nz-form-control nzErrorTip="Please input your username!">
<nz-input-group class="tw-w-full"> <nz-input-group class="tw-w-full">
<div class="tw-relative tw-w-full"> <div class="tw-relative tw-w-full">
...@@ -25,9 +23,9 @@ ...@@ -25,9 +23,9 @@
</nz-form-control> </nz-form-control>
</nz-form-item> </nz-form-item>
<!-- Password Field -->
<nz-form-item class="tw-mb-4"> <nz-form-item class="tw-mb-4">
<nz-form-label [nzFor]="'password'" class="tw-text-gray-700 tw-font-medium">Password</nz-form-label> <nz-form-label [nzFor]="'password'" class="tw-text-gray-700 tw-font-bold ">Password :</nz-form-label>
<nz-form-control nzErrorTip="Please input your password!"> <nz-form-control nzErrorTip="Please input your password!">
<nz-input-group class="tw-w-full"> <nz-input-group class="tw-w-full">
<div class="tw-relative tw-w-full"> <div class="tw-relative tw-w-full">
...@@ -45,17 +43,15 @@ ...@@ -45,17 +43,15 @@
</nz-input-group> </nz-input-group>
</nz-form-control> </nz-form-control>
</nz-form-item> </nz-form-item>
<p *ngIf="MgsError" class="text-error tw-mb-4">{{ MgsError }}</p>
<!-- Submit Button -->
<button <button
nz-button nz-button
class=" button tw-w-full tw-bg-indigo-600 tw-text-white tw-font-semibold tw-py-2 tw-rounded-md tw-hover:bg-indigo-700 tw-transition-colors" class=" tw-w-full tw-bg-indigo-600 tw-text-white tw-font-semibold tw-py-2 tw-rounded-md hover:bg-submit tw-transition-colors button-login"
[nzType]="'primary'" [nzType]="'primary'"
> >
Log in Log in
</button> </button>
<!-- Register Link -->
<p class="tw-text-center tw-text-gray-600 tw-mt-4"> <p class="tw-text-center tw-text-gray-600 tw-mt-4">
Or Or
<a class="tw-text-indigo-600 tw-font-semibold hover:tw-underline cursor-pointer">register now!</a> <a class="tw-text-indigo-600 tw-font-semibold hover:tw-underline cursor-pointer">register now!</a>
......
...@@ -12,6 +12,6 @@ ...@@ -12,6 +12,6 @@
.error-message{ .error-message{
color: red; color: red;
} }
.button { .button-login {
background-color: #3700ff; background-color: #714afd;
} }
...@@ -10,7 +10,7 @@ import { NzAlertModule } from 'ng-zorro-antd/alert'; ...@@ -10,7 +10,7 @@ import { NzAlertModule } from 'ng-zorro-antd/alert';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms'; import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule, Validators } from '@angular/forms';
import { catchError, finalize, Observable, tap } from 'rxjs'; import { catchError, finalize, Observable, of, tap } from 'rxjs';
import { AuthService } from '../data-access/Services/ApiService.service'; import { AuthService } from '../data-access/Services/ApiService.service';
import { response } from 'express'; import { response } from 'express';
@Component({ @Component({
...@@ -34,35 +34,42 @@ import { response } from 'express'; ...@@ -34,35 +34,42 @@ import { response } from 'express';
changeDetection: ChangeDetectionStrategy.OnPush, changeDetection: ChangeDetectionStrategy.OnPush,
}) })
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
MgsError: string = '';
loginForm: FormGroup; loginForm: FormGroup;
constructor(private fb: FormBuilder, private authService: AuthService) { constructor(private fb: FormBuilder, private authService: AuthService) {
this.loginForm = this.fb.group({ this.loginForm = this.fb.group({
username: ['', [Validators.required]], userName: ['', [Validators.required]],
password: ['', [Validators.required]], password: ['', [Validators.required]],
}); });
} }
ngOnInit() {} ngOnInit() {}
saveInfo(res: any) {
localStorage.setItem('token', res.responseData.token);
localStorage.setItem('expirationTime', res.responseData.expirationTime);
localStorage.setItem('role', res.responseData.role);
}
onSubmit() { onSubmit() {
const headers = new HttpHeaders()
if (!this.loginForm.valid) { if (!this.loginForm.valid) {
alert('vui lòng nhập đầy đủ thông tin'); this.MgsError = "Vui lòng nhập thống tin đăng nhập";
return; return;
} }
console.log(this.loginForm.value.username, this.loginForm.value.password); console.log(this.loginForm.value.userName, this.loginForm.value.password);
this.authService.login(this.loginForm.value.username, this.loginForm.value.password).pipe(
tap(() => {
this.authService.login(this.loginForm.value.userName, this.loginForm.value.password).pipe(
tap((res) => { // không ảnh hưởng đến data trên observable
this.saveInfo(res);
window.location.href = '/admin';
}), }),
catchError((error) => { catchError((err) => {
console.log(error); this.MgsError = err.message;
throw error; console.log(this.MgsError);
console.log(err);
return of(null);
}), }),
finalize(() => {}) finalize(() => { // gọi ra khi observable hoàn tất
).subscribe( console.log('Login request completed');
response => { })
alert(response.message); ).subscribe();
}
);
} }
} }
...@@ -13,4 +13,7 @@ ...@@ -13,4 +13,7 @@
color: white; color: white;
border: rgb(218, 141, 0); border: rgb(218, 141, 0);
} }
.bg-submit {
background-color: #3700ff;
}
} }
export const environment = { export const environment = {
API_DOMAIN: 'http/', //custom API_DOMAIN: 'http://localhost:3000', //custom
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment