Gitlab@Informatics
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
project-melon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
65160381
project-melon
Commits
7cf8376e
Commit
7cf8376e
authored
2 months ago
by
65160381
Browse files
Options
Downloads
Patches
Plain Diff
7.8
parent
bdabf532
No related branches found
No related tags found
No related merge requests found
Pipeline
#597
passed with warnings
2 months ago
Stage: test
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
app.js
+43
-29
43 additions, 29 deletions
app.js
with
43 additions
and
29 deletions
app.js
+
43
−
29
View file @
7cf8376e
...
@@ -106,44 +106,58 @@ app.post('/register', async (req, res) => {
...
@@ -106,44 +106,58 @@ app.post('/register', async (req, res) => {
});
});
// Route สำหรับการล็อกอิน
app
.
post
(
'
/login
'
,
async
(
req
,
res
)
=>
{
app
.
post
(
'
/login
'
,
async
(
req
,
res
)
=>
{
const
{
email
,
password
}
=
req
.
body
;
const
{
email
,
password
}
=
req
.
body
;
// ใช้ email แทน user
if
(
!
email
||
!
password
)
{
return
res
.
status
(
400
).
json
({
error
:
'
All fields are required
'
});
}
try
{
try
{
const
connection
=
await
pool
.
getConnection
();
// ค้นหาผู้ใช้จาก email
// ค้นหาผู้ใช้จาก email
const
[
user
]
=
await
pool
.
query
(
const
[
rows
]
=
await
connection
.
query
(
'
SELECT * FROM users WHERE email = ?
'
,
'
SELECT * FROM users WHERE email = ?
'
,
[
email
]
[
email
]
);
);
connection
.
release
();
if
(
user
.
length
===
0
)
{
if
(
rows
.
length
>
0
)
{
return
res
.
status
(
404
).
json
({
error
:
'
User not found
'
});
}
// ตรวจสอบรหัสผ่าน
// ตรวจสอบรหัสผ่าน
const
isMatch
=
await
bcrypt
.
compare
(
password
,
user
[
0
].
password
);
const
match
=
await
bcrypt
.
compare
(
password
,
rows
[
0
].
password
);
if
(
match
)
{
if
(
!
isMatch
)
{
// เก็บข้อมูลผู้ใช้ใน session (แค่ user_id และ email)
return
res
.
status
(
401
).
json
({
error
:
'
Invalid password
'
});
}
// สร้าง session ให้กับผู้ใช้
req
.
session
.
user
=
{
req
.
session
.
user
=
{
id
:
user
[
0
].
user_id
,
id
:
rows
[
0
].
user_id
,
// user_id ของผู้ใช้
email
:
user
[
0
].
email
email
:
rows
[
0
].
email
};
};
console
.
log
(
"
User session:
"
,
req
.
session
.
user
);
// เพิ่ม console log เพื่อตรวจสอบ
res
.
redirect
(
'
/
'
);
// ไปที่หน้า home หรือหน้าแรกหลังจากล็อกอิน
return
;
}
else
{
res
.
status
(
400
).
send
(
'
Invalid password
'
);
return
;
}
}
else
{
res
.
status
(
400
).
send
(
'
User not found
'
);
return
;
}
}
catch
(
err
)
{
console
.
error
(
'
Login error:
'
,
err
);
res
.
status
(
500
).
send
(
'
Login failed
'
);
return
;
}
});
res
.
status
(
200
).
json
({
message
:
'
Login successful
'
,
user
:
req
.
session
.
user
});
// API สำหรับดึงข้อมูลผู้ใช้หลังจากล็อกอิน
}
catch
(
error
)
{
app
.
get
(
'
/api/getUser
'
,
(
req
,
res
)
=>
{
console
.
error
(
'
❌ Login failed:
'
,
error
);
if
(
req
.
session
.
user
)
{
res
.
status
(
500
).
json
({
error
:
'
Login failed
'
});
res
.
json
({
user_id
:
req
.
session
.
user
.
id
,
// ส่งข้อมูล user_id จาก session
email
:
req
.
session
.
user
.
email
// ส่งข้อมูล email จาก session
});
}
else
{
res
.
status
(
401
).
send
(
'
User not logged in
'
);
}
}
});
});
app
.
use
(
express
.
json
());
// สำหรับการ parse ข้อมูลแบบ JSON
app
.
use
(
express
.
json
());
// สำหรับการ parse ข้อมูลแบบ JSON
app
.
use
(
express
.
urlencoded
({
extended
:
true
}));
// สำหรับการ parse ข้อมูลแบบ URL-encoded
app
.
use
(
express
.
urlencoded
({
extended
:
true
}));
// สำหรับการ parse ข้อมูลแบบ URL-encoded
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment