[init] init version
This commit is contained in:
31
src/app/auth/auth-routing.module.ts
Normal file
31
src/app/auth/auth-routing.module.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import {NgModule} from '@angular/core';
|
||||
import {Routes, RouterModule} from '@angular/router';
|
||||
import {LoginComponent} from './login/login.component';
|
||||
|
||||
|
||||
import {AuthComponent} from './auth.component';
|
||||
|
||||
|
||||
const routes: Routes = [
|
||||
{
|
||||
path: '',
|
||||
component: AuthComponent,
|
||||
children: [
|
||||
{path: '', redirectTo: 'login', pathMatch: 'full'},
|
||||
{path: 'login', component: LoginComponent},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
@NgModule({
|
||||
imports: [RouterModule.forChild(routes)],
|
||||
exports: [RouterModule]
|
||||
})
|
||||
export class AuthRoutingModule {
|
||||
}
|
||||
|
||||
|
||||
export const AuthRoutingComponents = [
|
||||
AuthComponent,
|
||||
LoginComponent,
|
||||
];
|
||||
3
src/app/auth/auth.component.html
Normal file
3
src/app/auth/auth.component.html
Normal file
@@ -0,0 +1,3 @@
|
||||
<router-outlet></router-outlet>
|
||||
|
||||
|
||||
15
src/app/auth/auth.component.ts
Normal file
15
src/app/auth/auth.component.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { Component, OnInit } from '@angular/core';
|
||||
|
||||
@Component({
|
||||
selector: 'app-auth',
|
||||
templateUrl: './auth.component.html',
|
||||
styles: []
|
||||
})
|
||||
export class AuthComponent implements OnInit {
|
||||
|
||||
constructor() { }
|
||||
|
||||
ngOnInit() {
|
||||
}
|
||||
|
||||
}
|
||||
15
src/app/auth/auth.module.ts
Normal file
15
src/app/auth/auth.module.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { NgModule } from '@angular/core';
|
||||
|
||||
import {AppSharedModule} from '../app.shared';
|
||||
import {AuthRoutingModule, AuthRoutingComponents} from './auth-routing.module';
|
||||
|
||||
@NgModule({
|
||||
declarations: [
|
||||
...AuthRoutingComponents,
|
||||
],
|
||||
imports: [
|
||||
AppSharedModule,
|
||||
AuthRoutingModule
|
||||
]
|
||||
})
|
||||
export class AuthModule { }
|
||||
37
src/app/auth/login/login.component.html
Normal file
37
src/app/auth/login/login.component.html
Normal file
@@ -0,0 +1,37 @@
|
||||
<div class="auth">
|
||||
<div class="auth-wrap">
|
||||
<div class="auth-logo">
|
||||
<img src="./assets/images/logo-b.png" />
|
||||
</div>
|
||||
|
||||
<div class="auth-card">
|
||||
<div class="auth-heading">Sign in for Admin</div>
|
||||
<form #ngf="ngForm" (ngSubmit)="onSubmit(ngf)">
|
||||
<div class="auth-card-body">
|
||||
<mat-form-field>
|
||||
<input matInput type="text" name="username" [(ngModel)]="dataForm.username" #username="ngModel"
|
||||
placeholder="Username" required>
|
||||
<i matSuffix class="bi bi-person-circle"></i>
|
||||
<mat-error *ngIf="isFieldValid(ngf, username)">กรุณากรอกข้อมูล</mat-error>
|
||||
</mat-form-field>
|
||||
<div style="height: 10px;"></div>
|
||||
<mat-form-field>
|
||||
<input matInput type="password" name="password" [(ngModel)]="dataForm.password"
|
||||
#password="ngModel" placeholder="Password" required>
|
||||
<i matSuffix class="bi bi-key"></i>
|
||||
<mat-error *ngIf="isFieldValid(ngf, password)">กรุณากรอกข้อมูล</mat-error>
|
||||
</mat-form-field>
|
||||
</div>
|
||||
<div class="auth-card-footer">
|
||||
<button type="submit" class="btn btn-primary w-full"> เข้าสู่ระบบ</button>
|
||||
</div>
|
||||
<div class="auth-card-action flex items-center">
|
||||
<div class="ml-auto mt-1">
|
||||
<mat-checkbox name="remember" [(ngModel)]="dataForm.remember">บันทึกรหัสผ่าน</mat-checkbox>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
61
src/app/auth/login/login.component.ts
Normal file
61
src/app/auth/login/login.component.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import {Component, OnInit} from '@angular/core';
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {AppService} from '../../app.service';
|
||||
import {lastValueFrom} from "rxjs";
|
||||
import {API, EAction, EText} from "../../@config/app";
|
||||
import { environment } from "../../../environments/environment";
|
||||
|
||||
|
||||
@Component({
|
||||
selector: 'app-login',
|
||||
templateUrl: './login.component.html',
|
||||
styles: []
|
||||
})
|
||||
export class LoginComponent implements OnInit {
|
||||
apiUrl: string = API.login;
|
||||
dataForm: any = {};
|
||||
isLoading = false;
|
||||
|
||||
constructor(
|
||||
private router: Router,
|
||||
private appService: AppService,
|
||||
private route: ActivatedRoute
|
||||
) {
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
|
||||
if (!environment.production) {
|
||||
this.dataForm = {
|
||||
username : 'admin',
|
||||
password : 'password@1',
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async onSubmit(form: any) {
|
||||
if (!form.valid) return false;
|
||||
const dataForm = {
|
||||
username: this.dataForm.username,
|
||||
password: this.dataForm.password,
|
||||
userType: 'ADMIN'
|
||||
};
|
||||
|
||||
|
||||
try {
|
||||
console.log(this.apiUrl)
|
||||
const result = await lastValueFrom(this.appService.post(this.apiUrl, dataForm));
|
||||
this.appService.setAuth(result.data);
|
||||
this.appService.setToken(result.accessToken);
|
||||
return this.router.navigate(['/pages']);
|
||||
} catch (err) {
|
||||
return this.appService.message(EAction.ERROR, EText.NO_DATA);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public isFieldValid(form: any, field: any) {
|
||||
return field.errors && (field.dirty || field.touched || form.submitted);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user