-api เส้น register
-เครื่องมือต่างๆ (ไม่สมบูร)
This commit is contained in:
70
exthernal-login-api/src/interfaces/Interface.js
Normal file
70
exthernal-login-api/src/interfaces/Interface.js
Normal file
@@ -0,0 +1,70 @@
|
||||
import jwt from 'jsonwebtoken'
|
||||
import { BdgmstInterface } from './table/bdgmstInterface.js'
|
||||
import { ActmstInterface } from './table/actmstInterface.js'
|
||||
import { UsrmstInterface } from './table/usrmstInterface.js'
|
||||
import { sendError } from '../utils/response.js'
|
||||
|
||||
// -------------------------------
|
||||
// GLOBAL FILE
|
||||
// -----------------------------
|
||||
|
||||
export class Interface {
|
||||
|
||||
constructor() {
|
||||
this.map = {
|
||||
bdgmst: new BdgmstInterface(),
|
||||
actmst: new ActmstInterface(),
|
||||
usrmst: new UsrmstInterface(),
|
||||
}
|
||||
}
|
||||
|
||||
// ===============================================================
|
||||
// saveInterface → แกะ token เอง และ route ไปยัง interface เฉพาะ table
|
||||
// ===============================================================
|
||||
async saveInterface(tableName, data, req) {
|
||||
|
||||
// ------------------------------
|
||||
// 1) จับ Interface ที่ตรงกับ table
|
||||
// ------------------------------
|
||||
const handler = this.map[tableName.toLowerCase()]
|
||||
if (!handler) {
|
||||
return new sendError(`Interface not found for table: ${tableName}`)
|
||||
}
|
||||
|
||||
let schema;
|
||||
|
||||
// ------------------------------
|
||||
// 2) ตรวจสอบเงื่อนไข (Exception for usrmst)
|
||||
// ------------------------------
|
||||
if (tableName.toLowerCase() === 'usrmst') {
|
||||
// กรณี usrmst (เช่น Register/Login) ไม่บังคับ Token
|
||||
// เราต้องกำหนด schema เอง เพราะไม่มี token ให้แกะ
|
||||
schema = 'nuttakit'
|
||||
} else {
|
||||
// ------------------------------
|
||||
// 3) ตารางอื่น ๆ บังคับ Token ตามปกติ
|
||||
// ------------------------------
|
||||
const token = req.headers.authorization?.split(' ')[1]
|
||||
if (!token) {
|
||||
return new sendError('ไม่พบการยืนยันตัวตน' ,'Missing token in request header')
|
||||
}
|
||||
|
||||
let decoded
|
||||
try {
|
||||
decoded = jwt.verify(token, process.env.JWT_SECRET)
|
||||
} catch (err) {
|
||||
return new sendError('Invalid token: ' + err.message)
|
||||
}
|
||||
|
||||
schema = decoded.organization
|
||||
}
|
||||
|
||||
if (!schema) return new sendError("Token missing 'organization' field or Schema undefined")
|
||||
|
||||
// ------------------------------
|
||||
// ✔ 4) ส่งงานไปยัง interface ของ table นั้น ๆ
|
||||
// ------------------------------
|
||||
return await handler.saveInterface(schema, data)
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user