Commit f40cbcd3 by Ooh-Ao

login

parent a77f8b77
......@@ -59,30 +59,30 @@ User.beforeUpdate(async (user, options) => {
}
});
(async () => {
try {
// sync() จะสร้างตารางตาม Model ทั้งหมดที่มีในโปรเจกต์ (ถ้ายังไม่มี)
// หากมีอยู่แล้วและต้องการ drop table เดิมให้ใช้ force: true (ไม่แนะนำบน production)
await sequelize.sync({ force: false });
// (async () => {
// try {
// // sync() จะสร้างตารางตาม Model ทั้งหมดที่มีในโปรเจกต์ (ถ้ายังไม่มี)
// // หากมีอยู่แล้วและต้องการ drop table เดิมให้ใช้ force: true (ไม่แนะนำบน production)
// await sequelize.sync({ force: false });
console.log("Table(s) created successfully!");
// console.log("Table(s) created successfully!");
// ตัวอย่างการทดสอบสร้าง User ทันที (ถ้าต้องการ)
const newUser = await User.create({
first_name: "Jane",
last_name: "Doe",
email: "jane@example.com",
password: "123456", // จะถูก hash จาก hook ใน model
role: "employee",
});
console.log("New user created:", newUser.user_id);
// // ตัวอย่างการทดสอบสร้าง User ทันที (ถ้าต้องการ)
// const newUser = await User.create({
// first_name: "Jane",
// last_name: "Doe",
// email: "jane@example.com",
// password: "123456", // จะถูก hash จาก hook ใน model
// role: "employee",
// });
// console.log("New user created:", newUser.user_id);
} catch (error) {
console.error("Error creating table(s):", error);
} finally {
// ปิด connection เมื่อเสร็จสิ้นงาน (ขึ้นอยู่กับว่าเราต้องการปิดหรือไม่)
await sequelize.close();
}
})();
// } catch (error) {
// console.error("Error creating table(s):", error);
// } finally {
// // ปิด connection เมื่อเสร็จสิ้นงาน (ขึ้นอยู่กับว่าเราต้องการปิดหรือไม่)
// await sequelize.close();
// }
// })();
module.exports = User;
......@@ -22,8 +22,8 @@ export class LoginPageComponent {
private renderer: Renderer2,
public tokenService: TokenService,
) {
const bodyElement = this.renderer.selectRootElement('body', true);
this.renderer.setAttribute(bodyElement, 'class', 'cover1 justify-center');
const bodyElement = this.renderer.selectRootElement('body', true);
this.renderer.setAttribute(bodyElement, 'class', 'cover1 justify-center');
}
ngOnInit(): void {
this.loginForm = this.formBuilder.group({
......@@ -66,23 +66,20 @@ export class LoginPageComponent {
body.username = this.email;
body.password = this.password;
this.authService.login(this.loginForm.controls['username'].value, this.loginForm.controls['password'].value).subscribe(result => {
this.tokenService.saveToken(result.access_token);
this.tokenService.saveToken(result.accessToken);
// this.tokenService.saveRefreshToken(result.refreshToken);
this.tokenService.saveUser(result);
if (result.member.status == 1) {
if (result.member.role == 99) {
this.router.navigate(['/admin/member-manage'])
} else {
this.router.navigate(['/admin/member-manage'])
}
if (result.user.role == 'admin') {
this.router.navigate(['/admin/home'])
} else {
this.error = 'ไม่สามารถใช้งานได้กรุณาติดต่อผู้ให้บริการ'
this.router.navigate(['/employee/home'])
}
// this.routes.navigate(['/admin/member-manage'])
}, (error) => {
this.disabled = '';
this.error = 'Username หรือ Password ไม่ถูกต้อง';
this.router.navigate(['/employee/home'])
// this.router.navigate(['/employee/home'])
})
}
......
import { TranslateService } from "@ngx-translate/core";
import { BaseModel } from "./base.model";
import { environment } from "src/environments/environment";
export class UserProfileModel extends BaseModel {
memberId: string;
username: string;
password: string;
user_id: string;
first_name: string;
last_name: string;
email: string;
firstName: string;
lastName: string;
phoneNumber: string;
role: number;
status: number;
picture: any;
createdAt: string;
updatedAt: string;
role: string;
constructor(
data?: Partial<UserProfileModel>,
translateService?: TranslateService
) {
super(data, translateService);
this.memberId = data?.memberId!;
this.username = data?.username!
this.password = data?.password!
this.phoneNumber = data?.phoneNumber || ""
this.firstName = data?.firstName || ""
this.lastName = data?.lastName || ""
this.status = data?.status!
this.user_id = data?.user_id!;
this.first_name = data?.first_name!
this.last_name = data?.last_name!
this.email = data?.email!
this.role = data?.role!
this.createdAt = data?.createdAt!
this.updatedAt = data?.updatedAt!
}
getRole(): string {
if (this.role == 99) {
return this.translateService.instant('Admin-System')
}
else if (this.role == 1) {
return this.translateService.instant('Admin-Company')
} else {
return this.translateService.instant('User')
}
}
getStatus(): string {
if (this.status == 1) {
return this.translateService.instant('Active')
} else {
return this.translateService.instant('Unactive')
}
}
getPicture(): string {
return this.picture ? environment.baseUrl + '/images/' + this.picture : './assets/images/faces/1.jpg'
}
getFullname(): string {
return this.firstName + " " + this.lastName
}
}
......@@ -791,7 +791,7 @@
<i class="ti ti-wallet text-lg"></i>
Bal: $7,12,950
</a>
<a routerLink="/signin/basic" class="ti-dropdown-item">
<a (click)="logOut()" class="ti-dropdown-item">
<i class="ti ti-logout text-lg"></i>
Log Out
</a>
......@@ -1025,7 +1025,7 @@
id="not3"
class="p-2 border dark:border-white/10 rounded-sm flex items-center text-gray-500 relative header-box"
>
<a
<a
routerLink="/tables/basictables" (click)="removetheModal()"
class="w-full my-auto items-center flex"
>
......
/* eslint-disable no-constant-condition */
import { Component, ElementRef } from '@angular/core';
import { NavService } from '../../services/navservice';
import { AuthService } from '../../services/auth.service';
import { TokenService } from '../../services/token.service';
@Component({
selector: 'app-header',
......@@ -9,7 +11,7 @@ import { NavService } from '../../services/navservice';
})
export class HeaderComponent {
constructor(public navServices: NavService,
constructor(public navServices: NavService, private tokenService: TokenService,
private elementRef: ElementRef) {
}
......@@ -37,11 +39,11 @@ export class HeaderComponent {
toggleSidebar() {
let html = this.elementRef.nativeElement.ownerDocument.documentElement;
if(window.innerWidth <= 992){
if (window.innerWidth <= 992) {
html?.setAttribute('toggled', html?.getAttribute('toggled') == 'open' ? 'close' : 'open');
if(html?.getAttribute('toggled') == 'open'){
if (html?.getAttribute('toggled') == 'open') {
document.querySelector('#responsive-overlay')?.classList.add('active');
}else{
} else {
document.querySelector('#responsive-overlay')?.classList.remove('active');
}
}
......@@ -106,4 +108,8 @@ export class HeaderComponent {
const element: any = document.querySelector('.serachmodal');
element.click();
}
logOut() {
this.tokenService.signOut();
}
}
......@@ -4,6 +4,7 @@ import { Subscription, fromEvent } from 'rxjs';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { NavigationEnd, Router } from '@angular/router';
import { checkHoriMenu, switcherArrowFn } from './sidebar';
import { TokenService } from '../../services/token.service';
@Component({
selector: 'app-sidebar',
......@@ -66,17 +67,18 @@ export class SidebarComponent {
private sanitizer: DomSanitizer,
public router: Router,
public renderer: Renderer2,
private elementRef: ElementRef
private elementRef: ElementRef,
private tokenService: TokenService,
) {
this.screenWidth = window.innerWidth;
}
ngOnInit() {
if(true){
if (this.tokenService.getUser().user.role == 'admin') {
this.menuitemsSubscribe$ = this.navServices.itemsAdmin.subscribe((items) => {
this.menuItems = items;
});
}else{
} else {
this.menuitemsSubscribe$ = this.navServices.itemsEmp.subscribe((items) => {
this.menuItems = items;
});
......@@ -89,7 +91,7 @@ export class SidebarComponent {
this.ParentActive();
}
});
if(window.innerWidth <= 992){
if (window.innerWidth <= 992) {
document.documentElement?.setAttribute('toggled', 'close');
}
const WindowResize = fromEvent(window, 'resize');
......
......@@ -2,11 +2,12 @@ import { Injectable, NgZone } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ProfileModel, UserModel } from '../user-auth.model';
import { UserProfileModel } from 'src/app/models/user.model';
import { environment } from 'src/environments/environment';
@Injectable({
providedIn: 'root',
})
export class AuthService {
apiBaseUrl = "/auth";
apiBaseUrl = environment.baseUrl + "/auth";
constructor(
private http: HttpClient,
) {
......@@ -14,7 +15,7 @@ export class AuthService {
login(username: string, password: string) {
const body = {
username: username,
email: username,
password: password,
};
return this.http.post<UserModel>(this.apiBaseUrl + '/login', body)
......
......@@ -31,8 +31,8 @@ export class TokenService {
window.localStorage.setItem(TOKEN_KEY, token);
const user = this.getUser();
if (user.access_token) {
this.saveUser({ ...user, access_token: token });
if (user.accessToken) {
this.saveUser({ ...user, accessToken: token });
}
}
......
......@@ -8,9 +8,9 @@ export class LoginModel {
export class UserModel {
public access_token: string = "";
public token_type: string = "";
public member : UserProfileModel = new UserProfileModel()
public accessToken: string = "";
public message: string = "";
public user : UserProfileModel = new UserProfileModel()
}
export interface ProfileModel {
......
......@@ -3,7 +3,7 @@
// The list of file replacements can be found in `angular.json`.
export const environment = {
production: true,
baseUrl: 'http://localhost:3000',
baseUrl: 'http://localhost:3000/api',
firebase: {
apiKey: '********************************',
authDomain: '********************************',
......
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