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
00aef74f
Commit
00aef74f
authored
1 month ago
by
65160381
Browse files
Options
Downloads
Patches
Plain Diff
7
parent
99df7982
Branches
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#588
passed with warnings
1 month ago
Stage: test
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
app.js
+97
-0
97 additions, 0 deletions
app.js
public/login.html
+20
-0
20 additions, 0 deletions
public/login.html
public/register.html
+33
-0
33 additions, 0 deletions
public/register.html
with
150 additions
and
0 deletions
app.js
+
97
−
0
View file @
00aef74f
...
@@ -57,6 +57,103 @@ app.get('/', (req, res) => {
...
@@ -57,6 +57,103 @@ app.get('/', (req, res) => {
}
}
});
});
app
.
post
(
'
/register
'
,
async
(
req
,
res
)
=>
{
const
{
email
,
password
}
=
req
.
body
;
if
(
!
email
||
!
password
)
{
return
res
.
status
(
400
).
json
({
error
:
'
All fields are required
'
});
}
try
{
// ตรวจสอบว่า email มีอยู่แล้วหรือไม่
const
[
existingUser
]
=
await
pool
.
query
(
'
SELECT * FROM users WHERE email = ?
'
,
[
email
]
);
if
(
existingUser
.
length
>
0
)
{
return
res
.
status
(
400
).
json
({
error
:
'
Email already exists
'
});
}
// เข้ารหัสรหัสผ่านด้วย bcryptjs
const
hashedPassword
=
await
bcrypt
.
hash
(
password
,
10
);
// เพิ่มข้อมูลลงฐานข้อมูล (ใช้แค่ email และ password)
await
pool
.
query
(
'
INSERT INTO users (email, password) VALUES (?, ?)
'
,
[
email
,
hashedPassword
]
);
res
.
status
(
201
).
json
({
message
:
'
User registered successfully
'
});
}
catch
(
error
)
{
console
.
error
(
'
❌ Registration failed:
'
,
error
);
res
.
status
(
500
).
json
({
error
:
'
Registration failed
'
});
}
});
app
.
post
(
'
/login
'
,
async
(
req
,
res
)
=>
{
const
{
email
,
password
}
=
req
.
body
;
if
(
!
email
||
!
password
)
{
return
res
.
status
(
400
).
json
({
error
:
'
All fields are required
'
});
}
try
{
// ค้นหาผู้ใช้จาก email
const
[
user
]
=
await
pool
.
query
(
'
SELECT * FROM users WHERE email = ?
'
,
[
email
]
);
if
(
user
.
length
===
0
)
{
return
res
.
status
(
404
).
json
({
error
:
'
User not found
'
});
}
// ตรวจสอบรหัสผ่าน
const
isMatch
=
await
bcrypt
.
compare
(
password
,
user
[
0
].
password
);
if
(
!
isMatch
)
{
return
res
.
status
(
401
).
json
({
error
:
'
Invalid password
'
});
}
// สร้าง session ให้กับผู้ใช้
req
.
session
.
user
=
{
id
:
user
[
0
].
user_id
,
email
:
user
[
0
].
email
};
res
.
status
(
200
).
json
({
message
:
'
Login successful
'
,
user
:
req
.
session
.
user
});
}
catch
(
error
)
{
console
.
error
(
'
❌ Login failed:
'
,
error
);
res
.
status
(
500
).
json
({
error
:
'
Login failed
'
});
}
});
app
.
post
(
'
/logout
'
,
(
req
,
res
)
=>
{
req
.
session
.
destroy
(
err
=>
{
if
(
err
)
{
console
.
error
(
'
❌ Logout failed:
'
,
err
);
return
res
.
status
(
500
).
json
({
error
:
'
Logout failed
'
});
}
res
.
clearCookie
(
'
connect.sid
'
);
res
.
status
(
200
).
json
({
message
:
'
Logout successful
'
});
});
});
const
isAuthenticated
=
(
req
,
res
,
next
)
=>
{
if
(
req
.
session
.
user
)
{
next
();
}
else
{
res
.
status
(
401
).
json
({
error
:
'
Unauthorized
'
});
}
};
app
.
get
(
'
/dashboard
'
,
isAuthenticated
,
(
req
,
res
)
=>
{
res
.
status
(
200
).
json
({
message
:
`Welcome
${
req
.
session
.
user
.
username
}
`
});
});
// Start the server
// Start the server
const
port
=
process
.
env
.
PORT
||
3000
;
const
port
=
process
.
env
.
PORT
||
3000
;
app
.
listen
(
port
,
()
=>
{
app
.
listen
(
port
,
()
=>
{
...
...
This diff is collapsed.
Click to expand it.
public/login.html
0 → 100644
+
20
−
0
View file @
00aef74f
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<title>
Login
</title>
<link
rel=
"stylesheet"
href=
"styles/login.css"
>
</head>
<body>
<div
class=
"login-container"
>
<h2>
Login
</h2>
<form
action=
"/login"
method=
"post"
>
<input
type=
"text"
placeholder=
"Username"
name=
"user"
required
>
<input
type=
"password"
placeholder=
"Password"
name=
"password"
required
>
<button
type=
"submit"
>
Login
</button>
<p>
don't have account
<a
href=
"register.html"
>
Register
</a></p>
</form>
</div>
</body>
</html>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
public/register.html
0 → 100644
+
33
−
0
View file @
00aef74f
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<title>
Register
</title>
<link
rel=
"stylesheet"
href=
"styles/register.css"
>
<script>
function
validatePassword
()
{
var
password
=
document
.
getElementById
(
'
password
'
).
value
;
var
confirmPassword
=
document
.
getElementById
(
'
confirm-password
'
).
value
;
if
(
password
!==
confirmPassword
)
{
alert
(
"
Passwords do not match!
"
);
return
false
;
}
return
true
;
}
</script>
</head>
<body>
<div
class=
"register-container"
>
<h2>
Register
</h2>
<form
onsubmit=
"return validatePassword()"
method=
"POST"
action=
"/register"
>
<input
type=
"text"
placeholder=
"Username"
name=
"username"
id=
"username"
required
>
<input
type=
"email"
placeholder=
"Email"
name=
"email"
required
>
<input
type=
"password"
placeholder=
"Password"
name=
"password"
id=
"password"
required
>
<input
type=
"password"
placeholder=
"Confirm Password"
name=
"confirm-password"
id=
"confirm-password"
required
>
<button
type=
"submit"
>
Register
</button>
</form>
</div>
</body>
</html>
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