-first commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
import { LoginService } from '../services/loginservice.js'
|
||||
import { sendResponse } from '../utils/response.js'
|
||||
// import { OftenError } from '../utils/oftenError.js'
|
||||
import { GeneralService } from '../share/generalservice.js';
|
||||
import { trim_all_array } from '../utils/trim.js'
|
||||
import { verifyToken, generateToken } from '../utils/token.js'
|
||||
|
||||
export class loginController {
|
||||
|
||||
constructor() { // contructor zone
|
||||
this.generalService = new GeneralService()
|
||||
this.loginService = new LoginService()
|
||||
}
|
||||
|
||||
|
||||
// ===================================================
|
||||
// 🔹 LOGIN ปกติ
|
||||
// ===================================================
|
||||
async onNavigate(req, res) {
|
||||
// Note: ตามที่ตกลงกันไว้ — ไม่ตรวจ organization ใน controller
|
||||
// (middleware จะตรวจค่า organization / request format ให้แล้ว)
|
||||
this.generalService.devhint(1, 'logincontroller.js', 'onNavigate() start')
|
||||
|
||||
// อ้างถึง organization จาก body เพื่อใช้ใน onLoginController (ครั้งแรกอาจว่าง)
|
||||
// แต่ไม่ต้อง return error ถ้าไม่มี — เพราะ middleware ทำหน้าที่ตรวจเบื้องต้นแล้ว
|
||||
let organization = req.body.organization
|
||||
|
||||
// เรียก logic controller แบบเดียว (ต้อง return value เท่านั้น)
|
||||
const prommis = await this.onLoginController(req, res, organization)
|
||||
return prommis // ห้ามเปลี่ยนตรงนี้ตาม pattern
|
||||
}
|
||||
|
||||
async onLoginController(req, res, database) {
|
||||
let idx = -1
|
||||
let result = []
|
||||
try {
|
||||
// const { username, password } = request // ห้ามทำแบบนี้อีกเด็ดขาด เราจะทำแบบด้านล่างแทน จดจำเลย
|
||||
|
||||
let username = req.body.request.username;
|
||||
let password = req.body.request.password;
|
||||
|
||||
|
||||
// if (!username || !password)
|
||||
// return sendResponse(res, 400, 'ข้อมูลไม่ครบ', 'Missing username or password')// เราจะไม่ทำแบบนี้กันอีกแล้ว
|
||||
result = await this.loginService.verifyLogin(database, username, password) // เช็คกับ db กลาง ส่ง jwttoken ออกมา
|
||||
// if (!result)
|
||||
// return sendResponse(res, 401, 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง', 'Invalid credentials')
|
||||
this.generalService.devhint(1, 'logincontroller.js', 'Login success')
|
||||
} catch (error) {
|
||||
idx = 1
|
||||
} finally { // สำคัญมากต้อง จดจำไม่มีดัดแปลง อัปเดทเลย เรื่อง idx
|
||||
if(result == 0){ return sendResponse(res, 400, 'ชื่อผู้ใช้หรือรหัสผ่านไม่ถูกต้อง', 'username or password is incorrect') }
|
||||
if(idx == 1){ return sendResponse(res, 400, 'เกิดข้อผิดพลาดไม่คาดคิดเกิดขึ้น', 'Unexpected error') }
|
||||
if(result) { return result }
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
async onBiometricLogin(req, res) {
|
||||
try {
|
||||
|
||||
const result = await this.loginService.loginWithBiometric(organization, biometric_id)
|
||||
} catch (error) {
|
||||
idx = 1
|
||||
} finally { // สำคัญมากต้อง จดจำไม่มีดัดแปลง อัปเดทเลย เรื่อง idx
|
||||
if(idx == 1){ return sendResponse(res, 400, 'เกิดข้อผิดพลาดไม่คาดคิดเกิดขึ้น', 'Unexpected error') }
|
||||
}
|
||||
return { result, timestamp: new Date().toISOString() }
|
||||
}
|
||||
|
||||
async onBiometricRegister(req, res) {
|
||||
const { organization, request } = req.body || {}
|
||||
const { biometric_id } = request || {}
|
||||
const userId = req.user.id
|
||||
|
||||
const result = await this.loginService.registerBiometric(organization, userId, biometric_id)
|
||||
return { result, timestamp: new Date().toISOString() }
|
||||
}
|
||||
|
||||
async onRenewToken(req, res) {
|
||||
const user = req.user
|
||||
const newToken = generateToken({ id: user.id, name: user.name })
|
||||
return { token: newToken, renewed_at: new Date().toISOString() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user