คู่มือนักพัฒนา: ประตูสู่การยืนยันตัวตน
ตัวอย่างใช้งานแบบลงมือจริง เพื่อให้ flow การล็อกอินทำงานลื่นและปลอดภัย
1. เริ่มต้นการเชื่อมต่อระบบ
ตั้งค่า session auth wrapper สำหรับสภาพแวดล้อมปัจจุบัน:
import { initializeAuthClient } from '../lib/auth-client';
initializeAuthClient();2. พาผู้ใช้เข้าสู่ LINE
ส่งผู้ใช้ไปยังหน้าล็อกอิน LINE:
import { startLineLogin } from '../lib/auth-client';
startLineLogin();3. ติดตามสถานะการยืนยันตัวตน
เฝ้าดูการเปลี่ยนแปลง login/logout:
import { onAuthStateChange } from '../lib/auth-client';
onAuthStateChange((user) => { ... });4. ดึงข้อมูลผู้ใช้ปัจจุบัน
อ่านโปรไฟล์ผู้ใช้ที่ล็อกอินอยู่:
import { getUserProfile, isLoggedIn } from '../lib/auth-client';
if (await isLoggedIn()) { const profile = await getUserProfile(); }5. อ่าน session ผู้ใช้
ดึงข้อมูล session ที่เซิร์ฟเวอร์ออกให้:
import { getSessionSnapshot } from '../lib/auth-client';
const session = await getSessionSnapshot();6. ออกจากระบบอย่างปลอดภัย
ยุติเซสชันผู้ใช้ปัจจุบัน:
import { logout } from '../lib/auth-client';
await logout();7. สรุปคำสั่งอ้างอิง
initializeAuthClient
เริ่มต้น session auth wrapper
startLineLogin
พาผู้ใช้ไปหน้า LINE Login
logout
ออกจากระบบผู้ใช้
isLoggedIn
ตรวจว่าผู้ใช้ล็อกอินอยู่หรือไม่
getUserProfile
อ่านข้อมูลโปรไฟล์ผู้ใช้
getSessionSnapshot
อ่าน session ผู้ใช้ปัจจุบัน